Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting of containers. #471

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ if(AREG_BUILD_TESTS)
# 'gtest' libraries are built
add_dependencies(areg-unit-tests areg-dummy)
endif()
else()
option(AREG_GTEST_PACKAGE "Use GTest package" FALSE)
option(AREG_GTEST_FOUND "GTest package found flag" FALSE)
endif()

if (AREG_INSTALL)
Expand Down
4 changes: 0 additions & 4 deletions conf/cmake/setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,3 @@ if (NOT "${AREG_PACKAGES}" STREQUAL "")
else()
set(AREG_PACKAGES "${FETCHCONTENT_BASE_DIR}")
endif()

if (NOT AREG_BUILD_TESTS)
set(AREG_GTEST_PACKAGE OFF)
endif()
82 changes: 43 additions & 39 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,49 @@
# List of examples to build
# ###########################################################################

# Examples with specific design must be included one by one.
include(${AREG_EXAMPLES}/00_helloservice/CMakeLists.txt)
include(${AREG_EXAMPLES}/01_hello/CMakeLists.txt)
include(${AREG_EXAMPLES}/02_buffer/CMakeLists.txt)
include(${AREG_EXAMPLES}/03_file/CMakeLists.txt)
include(${AREG_EXAMPLES}/04_logging/CMakeLists.txt)
include(${AREG_EXAMPLES}/05_timer/CMakeLists.txt)
include(${AREG_EXAMPLES}/06_threads/CMakeLists.txt)
include(${AREG_EXAMPLES}/07_synch/CMakeLists.txt)
include(${AREG_EXAMPLES}/08_service/CMakeLists.txt)
include(${AREG_EXAMPLES}/09_svcmulti/CMakeLists.txt)
include(${AREG_EXAMPLES}/10_locsvc/CMakeLists.txt)
include(${AREG_EXAMPLES}/11_locmesh/CMakeLists.txt)
include(${AREG_EXAMPLES}/12_pubsvc/CMakeLists.txt)
include(${AREG_EXAMPLES}/13_pubmesh/CMakeLists.txt)
include(${AREG_EXAMPLES}/14_pubtraffic/CMakeLists.txt)
include(${AREG_EXAMPLES}/15_pubworker/CMakeLists.txt)
include(${AREG_EXAMPLES}/16_pubfsm/CMakeLists.txt)
include(${AREG_EXAMPLES}/18_locwatchdog/CMakeLists.txt)
include(${AREG_EXAMPLES}/19_pubwatchdog/CMakeLists.txt)
include(${AREG_EXAMPLES}/20_pubdatarate/CMakeLists.txt)
include(${AREG_EXAMPLES}/21_pubunblock/CMakeLists.txt)
include(${AREG_EXAMPLES}/22_pubsub/CMakeLists.txt)
include(${AREG_EXAMPLES}/23_pubsubmix/CMakeLists.txt)
include(${AREG_EXAMPLES}/24_pubsubmulti/CMakeLists.txt)
if(AREG_BUILD_EXAMPLES)

if (AREG_DEVELOP_ENV MATCHES "Win32")
# This projects are based either on MFC (Microsoft Foundation Classes)
# or are compiled only under Windows. So that, they are available
# only when compile with Microsoft Visual Studio.
# Examples with specific design must be included one by one.
include(${AREG_EXAMPLES}/00_helloservice/CMakeLists.txt)
include(${AREG_EXAMPLES}/01_hello/CMakeLists.txt)
include(${AREG_EXAMPLES}/02_buffer/CMakeLists.txt)
include(${AREG_EXAMPLES}/03_file/CMakeLists.txt)
include(${AREG_EXAMPLES}/04_logging/CMakeLists.txt)
include(${AREG_EXAMPLES}/05_timer/CMakeLists.txt)
include(${AREG_EXAMPLES}/06_threads/CMakeLists.txt)
include(${AREG_EXAMPLES}/07_synch/CMakeLists.txt)
include(${AREG_EXAMPLES}/08_service/CMakeLists.txt)
include(${AREG_EXAMPLES}/09_svcmulti/CMakeLists.txt)
include(${AREG_EXAMPLES}/10_locsvc/CMakeLists.txt)
include(${AREG_EXAMPLES}/11_locmesh/CMakeLists.txt)
include(${AREG_EXAMPLES}/12_pubsvc/CMakeLists.txt)
include(${AREG_EXAMPLES}/13_pubmesh/CMakeLists.txt)
include(${AREG_EXAMPLES}/14_pubtraffic/CMakeLists.txt)
include(${AREG_EXAMPLES}/15_pubworker/CMakeLists.txt)
include(${AREG_EXAMPLES}/16_pubfsm/CMakeLists.txt)
include(${AREG_EXAMPLES}/18_locwatchdog/CMakeLists.txt)
include(${AREG_EXAMPLES}/19_pubwatchdog/CMakeLists.txt)
include(${AREG_EXAMPLES}/20_pubdatarate/CMakeLists.txt)
include(${AREG_EXAMPLES}/21_pubunblock/CMakeLists.txt)
include(${AREG_EXAMPLES}/22_pubsub/CMakeLists.txt)
include(${AREG_EXAMPLES}/23_pubsubmix/CMakeLists.txt)
include(${AREG_EXAMPLES}/24_pubsubmulti/CMakeLists.txt)

find_package(MFC)
if (MFC_FOUND)
# MFC specific projects. Can be compiled only if target machine has installed MFC.
# Otherwise, output warning message and exclude from the build.
include(${AREG_EXAMPLES}/17_winchat/CMakeLists.txt)
else(MFC_FOUND)
message(NOTICE " AREG Examples: >>> Due to missing installed MFC library, escaping compile projects:")
message(NOTICE " AREG Examples: >>> ${AREG_EXAMPLES}/17_winchat")
endif(MFC_FOUND)
if (AREG_DEVELOP_ENV MATCHES "Win32")
# This projects are based either on MFC (Microsoft Foundation Classes)
# or are compiled only under Windows. So that, they are available
# only when compile with Microsoft Visual Studio.

endif()
find_package(MFC)
if (MFC_FOUND)
# MFC specific projects. Can be compiled only if target machine has installed MFC.
# Otherwise, output warning message and exclude from the build.
include(${AREG_EXAMPLES}/17_winchat/CMakeLists.txt)
else(MFC_FOUND)
message(NOTICE " AREG Examples: >>> Due to missing installed MFC library, escaping compile projects:")
message(NOTICE " AREG Examples: >>> ${AREG_EXAMPLES}/17_winchat")
endif(MFC_FOUND)

endif()

endif(AREG_BUILD_EXAMPLES)
59 changes: 43 additions & 16 deletions framework/areg/base/TEArrayList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ class TEArrayList : private Constless<std::vector<VALUE>>
template <typename V>
friend IEOutStream & operator << (IEOutStream & stream, const TEArrayList< V > & output);

/**
* \brief Sorts the array, compares the elements by given Compare functionality.
* \param list Array object to sort.
* \param comp The comparing method, similar to the method std::greater()
* \return Sorts and returns the 'list' object.
**/
template <typename V, class Compare>
friend TEArrayList< V >& sortArray(TEArrayList< V >& list, Compare comp);

//////////////////////////////////////////////////////////////////////////
// Attributes
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -424,6 +415,28 @@ class TEArrayList : private Constless<std::vector<VALUE>>
inline const VALUE & lastEntry( void ) const;
inline VALUE & lastEntry( void );

/**
* \brief Sorts the array, compares the elements by given Compare functionality.
* \param comp The comparing method, similar to the method std::greater()
* \return Sorts and returns the array object.
**/
template <class Compare>
inline TEArrayList< VALUE >& sort(Compare comp);

/**
* \brief Copies elements from the array into the provided pre-allocated buffer.
* If `elemCount` is less than the number of elements in the array,
* only the first `elemCount` elements are copied. Otherwise, all elements
* in the array are copied. No elements are copied if `elemCount` is 0.
* \param list [in, out] A pre-allocated buffer where the array elements
* will be copied. Must be large enough to hold at least
* `elemCount` elements.
* \param elemCount [in] The maximum number of elements to copy into the `list` buffer.
* If set to 0, no elements are copied.
* \return The number of elements successfully copied into the `list` buffer.
**/
inline uint32_t getElements(VALUE* list, uint32_t elemCount);

//////////////////////////////////////////////////////////////////////////
// Protected operations
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1068,6 +1081,26 @@ inline VALUE & TEArrayList<VALUE>::lastEntry( void )
return mValueList[ mValueList.size( ) - 1 ];
}

template<typename VALUE>
template<class Compare>
inline TEArrayList<VALUE>& TEArrayList<VALUE>::sort(Compare comp)
{
std::sort(mValueList.begin(), mValueList.end(), comp);
return (*this);
}

template<typename VALUE>
inline uint32_t TEArrayList<VALUE>::getElements(VALUE* list, uint32_t elemCount)
{
uint32_t result{ MACRO_MIN(static_cast<uint32_t>(mValueList.size()), elemCount) };
for (uint32_t i = 0; i < result; ++i)
{
list[i] = mValueList[i];
}

return result;
}

template<typename VALUE >
inline void TEArrayList< VALUE >::setSize(uint32_t elemCount)
{
Expand Down Expand Up @@ -1119,11 +1152,5 @@ IEOutStream & operator << ( IEOutStream& stream, const TEArrayList< V >& output
return stream;
}

template <typename V, class Compare>
TEArrayList< V >& sortArray(TEArrayList< V >& list, Compare comp)
{
std::sort(list.mValueList.begin(), list.mValueList.end(), comp);
return list;
}

#endif // AREG_BASE_TEARRAYLIST_HPP

62 changes: 62 additions & 0 deletions framework/areg/base/TEFixedArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "areg/base/NEMemory.hpp"
#include "areg/base/IEIOStream.hpp"

#include <algorithm>

//////////////////////////////////////////////////////////////////////////
// TEFixedArray<VALUE> class template declaration.
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -89,6 +91,12 @@ class TEFixedArray
* \param src The source to move data.
**/
TEFixedArray( TEFixedArray<VALUE> && src ) noexcept;
/**
* \brief Compiles entries from the given array of objects.
* \param list The list of entries to copy.
* \param count The number of entries in the array.
**/
TEFixedArray(const VALUE* list, uint32_t count);
/**
* \brief Destructor.
**/
Expand Down Expand Up @@ -289,6 +297,28 @@ class TEFixedArray
inline const VALUE & lastEntry( void ) const;
inline VALUE & lastEntry( void );

/**
* \brief Sorts the array, compares the elements by given Compare functionality.
* \param comp The comparing method, similar to the method std::greater()
* \return Sorts and returns the fixed array object.
**/
template <class Compare>
inline TEFixedArray< VALUE >& sort(Compare comp);

/**
* \brief Copies elements from the array into the provided pre-allocated buffer.
* If `elemCount` is less than the number of elements in the array,
* only the first `elemCount` elements are copied. Otherwise, all elements
* in the array are copied. No elements are copied if `elemCount` is 0
* \param list [in, out] A pre-allocated buffer where the array elements
* will be copied. Must be large enough to hold at least
* `elemCount` elements.
* \param elemCount [in] The maximum number of elements to copy into the `list` buffer.
* If set to 0, no elements are copied.
* \return The number of elements successfully copied into the `list` buffer.
**/
inline uint32_t getElements(VALUE* list, uint32_t elemCount);

//////////////////////////////////////////////////////////////////////////
// Protected member variables
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -335,6 +365,14 @@ TEFixedArray<VALUE>::TEFixedArray( TEFixedArray<VALUE> && src ) noexcept
src.mElemCount = 0;
}

template<typename VALUE>
TEFixedArray<VALUE>::TEFixedArray(const VALUE* list, uint32_t count)
: mValueList(count ? DEBUG_NEW VALUE[count] : nullptr)
, mElemCount(mValueList != nullptr ? count : 0)
{
NEMemory::copyElems<VALUE>(mValueList, list, mElemCount);
}

template< typename VALUE >
TEFixedArray<VALUE>::~TEFixedArray( void )
{
Expand Down Expand Up @@ -555,6 +593,30 @@ inline VALUE & TEFixedArray<VALUE>::lastEntry( void )
return mValueList[ mElemCount - 1 ];
}

template<typename VALUE>
template<class Compare>
inline TEFixedArray<VALUE>& TEFixedArray<VALUE>::sort(Compare comp)
{
if (mValueList != nullptr)
{
std::sort(mValueList, mValueList + mElemCount, comp);
}

return (*this);
}

template<typename VALUE>
inline uint32_t TEFixedArray<VALUE>::getElements(VALUE* list, uint32_t elemCount)
{
uint32_t result{ MACRO_MIN(mElemCount, elemCount) };
for (uint32_t i = 0; i < result; ++i)
{
list[i] = mValueList[i];
}

return result;
}

//////////////////////////////////////////////////////////////////////////
// Friend function implementation
//////////////////////////////////////////////////////////////////////////
Expand Down
60 changes: 60 additions & 0 deletions framework/areg/base/TEHashMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ class TEHashMap : protected Constless<std::unordered_map<KEY, VALUE>>
**/
TEHashMap( TEHashMap<KEY, VALUE> && src ) noexcept = default;

/**
* \brief Compiles entries from the given array of keys and values,
* where the amount of key and value entries are equal.
* If any key is repeating in the list, it will be replaced by new value.
* The number of entries in the hash-map is equal to 'count' only if all keys are unique.
* \param keys The list of keys to copy.
* \param values The list of values to pair with keys.
* \param count The number of entries in the key and value entries.
**/
TEHashMap(const KEY* keys, const VALUE * values, uint32_t count);

/**
* \brief Destructor.
**/
Expand Down Expand Up @@ -487,6 +498,21 @@ class TEHashMap : protected Constless<std::unordered_map<KEY, VALUE>>
**/
inline bool nextEntry(MAPPOS & IN OUT in_out_NextPosition, KEY & OUT out_NextKey, VALUE & OUT out_NextValue ) const;

/**
* \brief Copies elements from the hash-map into the provided pre-allocated buffer of keys and values.
* If `elemCount` is less than the number of elements in the hash-map,
* only the first `elemCount` elements are copied. Otherwise, all elements
* in the hash-map are copied. No elements are copied if `elemCount` is 0.
* \param keys [in, out] A pre-allocated buffer where the keys of the hash-map elements will be copied.
* Must be large enough to hold at least `elemCount` elements.
* \param values [in, out] A pre-allocated buffer where the values of the hash-map elements will be copied.
* Must be large enough to hold at least `elemCount` elements.
* \param elemCount [in] The maximum number of elements to copy into the keys and values buffer.
* If set to 0, no elements are copied.
* \return The number of elements successfully copied.
**/
inline uint32_t getElements(KEY * keys, VALUE * values, uint32_t elemCount);

//////////////////////////////////////////////////////////////////////////
//Hidden methods
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -524,6 +550,18 @@ TEHashMap<KEY, VALUE>::TEHashMap(uint32_t hashSize /* = NECommon::MAP_DEFAULT_HA
{
}

template<typename KEY, typename VALUE>
TEHashMap<KEY, VALUE>::TEHashMap(const KEY* keys, const VALUE* values, uint32_t count)
: Constless<std::unordered_map<KEY, VALUE>>()
, mValueList()
{
mValueList.reserve(count);
for (uint32_t i = 0; i < count; ++i)
{
mValueList[keys[i]] = values[i];
}
}

template < typename KEY, typename VALUE >
inline bool TEHashMap<KEY, VALUE>::operator == (const TEHashMap<KEY, VALUE>& other) const
{
Expand Down Expand Up @@ -941,6 +979,28 @@ inline bool TEHashMap<KEY, VALUE>::nextEntry(TEHashMap<KEY, VALUE>::MAPPOS & IN
return result;
}

template<typename KEY, typename VALUE>
inline uint32_t TEHashMap<KEY, VALUE>::getElements(KEY* keys, VALUE* values, uint32_t elemCount)
{
uint32_t result{ MACRO_MIN(static_cast<uint32_t>(mValueList.size()), elemCount)};
if (result > 0)
{
uint32_t i = 0;
for (const auto & elem : mValueList)
{
keys[i] = elem.first;
values[i] = elem.second;

if (++i == result)
{
break;
}
}
}

return result;
}

template<typename KEY, typename VALUE>
inline typename TEHashMap<KEY, VALUE>::MAPPOS TEHashMap<KEY, VALUE>::_citer2pos(typename std::unordered_map<KEY, VALUE>::const_iterator cit) const
{
Expand Down
Loading
Loading