Skip to content

Commit

Permalink
Added sorting method for containers. Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
aregtech committed Dec 10, 2024
1 parent 3708776 commit d7ab208
Show file tree
Hide file tree
Showing 13 changed files with 602 additions and 26 deletions.
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

60 changes: 60 additions & 0 deletions framework/areg/base/TEFixedArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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 +295,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 +363,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 +591,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
64 changes: 64 additions & 0 deletions framework/areg/base/TELinkedList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class TELinkedList : private Constless<std::list<VALUE>>
**/
TELinkedList( TELinkedList<VALUE> && src ) noexcept = default;

/**
* \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.
**/
TELinkedList(const VALUE* list, uint32_t count);

/**
* \brief Destructor.
**/
Expand Down Expand Up @@ -579,6 +586,27 @@ class TELinkedList : private Constless<std::list<VALUE>>
inline void merge(TELinkedList<VALUE> & source);
inline void merge(TELinkedList<VALUE> && source);

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

/**
* \brief Copies elements from the linked list into the provided pre-allocated buffer.
* If `elemCount` is less than the number of elements in the linked list,
* only the first `elemCount` elements are copied. Otherwise, all elements
* in the linked list are copied. No elements are copied if `elemCount` is 0.
* \param list [in, out] A pre-allocated buffer where the linked list 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 methods
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -616,6 +644,17 @@ class TELinkedList : private Constless<std::list<VALUE>>
// TELinkedList<VALUE> class template implementation
//////////////////////////////////////////////////////////////////////////

template<typename VALUE>
TELinkedList<VALUE>::TELinkedList(const VALUE* list, uint32_t count)
: Constless<std::list<VALUE>>()
, mValueList()
{
for (uint32_t i = 0; i < count; ++i)
{
mValueList.push_back(list[i]);
}
}

template <typename VALUE >
inline TELinkedList<VALUE>& TELinkedList<VALUE>::operator = (const TELinkedList<VALUE>& src)
{
Expand Down Expand Up @@ -1218,6 +1257,23 @@ inline void TELinkedList<VALUE>::merge(TELinkedList<VALUE>&& source)
mValueList.merge(std::move(source.mValueList));
}

template<typename VALUE>
inline uint32_t TELinkedList<VALUE>::getElements(VALUE* list, uint32_t elemCount)
{
uint32_t result{ MACRO_MIN(static_cast<uint32_t>(mValueList.size()), elemCount) };
uint32_t i = 0;
for (const auto& entry : mValueList)
{
list[i++] = entry;
if (i == result)
{
break;
}
}

return result;
}

template <typename VALUE >
inline typename TELinkedList<VALUE>::LISTPOS TELinkedList<VALUE>::getPosition(uint32_t index) const
{
Expand All @@ -1230,6 +1286,14 @@ inline typename TELinkedList<VALUE>::LISTPOS TELinkedList<VALUE>::getPosition(ui
return _citer2pos(pos);
}

template<typename VALUE>
template<class Compare>
inline TELinkedList<VALUE>& TELinkedList<VALUE>::sort(Compare comp)
{
mValueList.sort(comp);
return (*this);
}

template<typename VALUE>
inline typename TELinkedList<VALUE>::LISTPOS TELinkedList<VALUE>::_citer2pos(typename std::list<VALUE>::const_iterator cit) const
{
Expand Down
Loading

0 comments on commit d7ab208

Please sign in to comment.