-
Notifications
You must be signed in to change notification settings - Fork 1
🛠️ Collections
The Collections module provides generic container classes designed to manage dynamic collections of data efficiently. It offers custom implementations of lists, pairs, and maps that provide intuitive methods for insertion, removal, iteration, and element access. These structures aim to simplify working with collections while ensuring type safety and clean syntax.
The module introduces three main template-based classes: List<T>, Pair<K, V>, and Map<K, V>.
Each class encapsulates logic commonly found in standard containers, but with custom handling for exceptions, iterator support, functional operations, and simplified access.
The List<T> class wraps around a dynamic container for storing ordered elements. It supports iteration, filtering, sorting, insertion, removal, and functional operations such as ForEach and Any.
The Pair<K, V> class holds a lightweight key–value pair, typically used internally in the Map<K, V> class.
The Map<K, V> class maintains a list of Pair<K, V> objects, providing key-based access, addition, deletion, search, and iteration.
A dynamic array structure that stores a sequence of elements of type T, offering access, manipulation, and utility methods.
List()
List(std::initializer_list<T> values)Creates an empty list or initializes it with given elements.
List<T>& Add(const T& value)Appends an element to the end of the list. Returns a reference to the list.
List<T>& AddFirst(const T& value)Inserts an element at the beginning of the list.
List<T>& SetFirst(size_t index)Moves the element at the specified index to the beginning of the list. Throws if index is out of bounds.
List<T>& PopFirst()Removes the first element from the list. Throws if the list is empty.
List<T>& PopLast()Removes the last element from the list. Throws if the list is empty.
T& First()Returns a reference to the first element. Throws if the list is empty.
T& Last()Returns a reference to the last element. Throws if the list is empty.
T& Get(size_t index)Returns a reference to the element at the specified index. Throws if the index is out of bounds.
List<T>& Set(size_t index, const T& value)Replaces the element at the given index with the provided value. Throws if index is out of bounds.
size_t Length() constReturns the number of elements in the list.
bool Empty() constReturns true if the list has no elements.
List<T>& Clear()Removes all elements from the list.
bool Contains(const T& value) constChecks whether an element exists in the list.
int FindIndex(const T& value) constReturns the index of the first occurrence of the element. Returns -1 if not found.
List<T>& FindAndRemove(const T& value)Finds and removes the first occurrence of the given element. Returns the list.
List<T>& RemoveIndex(size_t index)Removes the element at the specified index. Throws if the index is out of bounds.
template<typename Predicate>
List<T>& RemoveIf(Predicate condition)Removes all elements that satisfy the given condition.
template<typename Predicate>
List<T> Filter(Predicate condition) constReturns a new list containing elements that satisfy the given condition.
List<T>& Sort(CompareFunction compare)Sorts elements using a custom comparator function.
List<T>& Merge(List<T> other)Appends elements from another list that are not already present.
template<typename Func>
List<T>& ForEach(Func action)Applies a function to each element of the list.
std::vector<T> ToStdVector() constReturns a copy of the internal std::vector.
std::vector<T>* ToStdVectorPtr()Returns a pointer to the internal std::vector.
A lightweight structure that stores a pair of values, typically representing a key-value relationship or any two associated elements.
Pair()
Pair(F first, S second)Creates an empty pair or initializes it with the given first and second values.
-
F first: The first element of the pair. -
S second: The second element of the pair.
A key-value collection that maps unique keys of type K to corresponding values of type V.
Implemented internally using a List<Pair<K, V>>.
Dictionary()
Dictionary(std::initializer_list<std::pair<K, V>> map)Creates an empty dictionary or initializes it with a list of key-value pairs.
size_t Length() constReturns the number of key-value pairs stored in the dictionary.
bool Empty() constReturns true if the dictionary contains no elements.
V Get(K key) constReturns the value associated with the specified key. Throws if the key does not exist.
void Set(K key, V value)If the key already exists, updates its associated value. If the key does not exist, inserts a new key-value pair.
void RemoveByKey(K key)Removes the entry with the specified key, if it exists.
void RemoveByValue(V value)Removes all entries that contain the specified value.
void Clear()Removes all key-value pairs from the dictionary.
bool HasKey(const K& key) constChecks whether the given key exists in the dictionary.
List<K> GetKeyList() constReturns a List containing all keys in the dictionary.
List<V> GetValueList() constReturns a List containing all values in the dictionary.
Dictionary<K, V> FilterByValue(const V& value) constReturns a new dictionary containing only entries with the specified value.
std::vector<Pair<K, V>> std_vector() constReturns a copy of the underlying list as a std::vector of pairs.