283 lines
7.6 KiB
C++
283 lines
7.6 KiB
C++
/*
|
|
* File: Map.h
|
|
* Description: Provides a generic key-value map container implementation.
|
|
* Author: Trenser
|
|
* Created: 18-May-2026
|
|
*/
|
|
|
|
#pragma once
|
|
#include "Vector.h"
|
|
|
|
namespace util
|
|
{
|
|
template<typename K, typename V>
|
|
class Map
|
|
{
|
|
private:
|
|
class Entry
|
|
{
|
|
public:
|
|
K key;
|
|
V value;
|
|
Entry() : key(K{}), value(V{}) {}
|
|
Entry(const K& key, const V& value)
|
|
{
|
|
this->key = key;
|
|
this->value = value;
|
|
}
|
|
bool operator==(const Entry& other) const
|
|
{
|
|
return key == other.key;
|
|
}
|
|
};
|
|
Vector<Entry> entries;
|
|
public:
|
|
/*
|
|
* Function: Map
|
|
* Description: Default constructor for Map.
|
|
* Parameters: None
|
|
* Returns:
|
|
* Map object
|
|
*/
|
|
Map() {}
|
|
|
|
/*
|
|
* Function: insert
|
|
* Description: Inserts a key-value pair into the map.
|
|
* Updates the value if the key already exists.
|
|
* Parameters:
|
|
* key - key to insert
|
|
* value - value associated with the key
|
|
* Returns:
|
|
* void - no return value
|
|
*/
|
|
void insert(const K& key, const V& value)
|
|
{
|
|
int index = find(key);
|
|
if (index != -1)
|
|
{
|
|
entries[index].value = value;
|
|
return;
|
|
}
|
|
entries.push_back(Entry(key, value));
|
|
}
|
|
|
|
/*
|
|
* Function: remove
|
|
* Description: Removes an entry with the given key from the map.
|
|
* Parameters:
|
|
* key - key of the entry to remove
|
|
* Returns:
|
|
* bool - true if the entry was removed, false otherwise
|
|
*/
|
|
bool remove(const K& key)
|
|
{
|
|
int index = find(key);
|
|
if (index == -1)
|
|
{
|
|
return false;
|
|
}
|
|
entries.removeAt(index);
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Function: containsKey
|
|
* Description: Checks whether the map contains a given key.
|
|
* Parameters:
|
|
* key - key to search for
|
|
* Returns:
|
|
* bool - true if the key exists, false otherwise
|
|
*/
|
|
bool containsKey(const K& key) const
|
|
{
|
|
return find(key) != -1;
|
|
}
|
|
|
|
/*
|
|
* Function: find
|
|
* Description: Finds the index of an entry with the given key.
|
|
* Parameters:
|
|
* key - key to search for
|
|
* Returns:
|
|
* int - index of the entry if found, otherwise -1
|
|
*/
|
|
int find(const K& key) const
|
|
{
|
|
for (int index = 0; index < entries.getSize(); index++)
|
|
{
|
|
if (entries[index].key == key)
|
|
{
|
|
return index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* Function: findIf
|
|
* Description: Finds the index of an entry matching a custom predicate.
|
|
* Parameters:
|
|
* predicate - callable object used for matching entries
|
|
* Returns:
|
|
* int - index of the matching entry if found, otherwise -1
|
|
*/
|
|
template<typename Predicate>
|
|
int findIf(Predicate predicate) const
|
|
{
|
|
for (int index = 0; index < entries.getSize(); index++)
|
|
{
|
|
if (predicate(entries[index].key, entries[index].value))
|
|
{
|
|
return index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
* Function: operator[]
|
|
* Description: Returns a reference to the value associated with the key.
|
|
* Inserts a default value if the key does not exist.
|
|
* Parameters:
|
|
* key - key associated with the value
|
|
* Returns:
|
|
* V& - reference to the value associated with the key
|
|
*/
|
|
V& operator[](const K& key)
|
|
{
|
|
int index = find(key);
|
|
if (index == -1)
|
|
{
|
|
insert(key, V());
|
|
index = find(key);
|
|
}
|
|
return entries[index].value;
|
|
}
|
|
|
|
/*
|
|
* Function: operator[]
|
|
* Description: Returns a constant reference to the value associated with the key.
|
|
* Parameters:
|
|
* key - key associated with the value
|
|
* Returns:
|
|
* const V& - constant reference to the value associated with the key
|
|
*/
|
|
const V& operator[](const K& key) const
|
|
{
|
|
int index = find(key);
|
|
return entries[index].value;
|
|
}
|
|
|
|
/*
|
|
* Function: getSize
|
|
* Description: Returns the number of entries in the map.
|
|
* Parameters: None
|
|
* Returns:
|
|
* int - number of entries in the map
|
|
*/
|
|
int getSize() const
|
|
{
|
|
return entries.getSize();
|
|
}
|
|
|
|
/*
|
|
* Function: isEmpty
|
|
* Description: Checks whether the map is empty.
|
|
* Parameters: None
|
|
* Returns:
|
|
* bool - true if the map is empty, false otherwise
|
|
*/
|
|
bool isEmpty() const
|
|
{
|
|
return entries.isEmpty();
|
|
}
|
|
|
|
/*
|
|
* Function: clear
|
|
* Description: Removes all entries from the map.
|
|
* Parameters: None
|
|
* Returns:
|
|
* void - no return value
|
|
*/
|
|
void clear()
|
|
{
|
|
entries.clear();
|
|
}
|
|
|
|
/*
|
|
* Function: getKeys
|
|
* Description: Returns a vector containing all keys in the map.
|
|
* Parameters: None
|
|
* Returns:
|
|
* Vector<K> - vector containing all keys
|
|
*/
|
|
Vector<K> getKeys() const
|
|
{
|
|
Vector<K> keys;
|
|
for (int index = 0; index < entries.getSize(); index++)
|
|
{
|
|
keys.push_back(entries[index].key);
|
|
}
|
|
return keys;
|
|
}
|
|
|
|
/*
|
|
* Function: getValues
|
|
* Description: Returns a vector containing all values in the map.
|
|
* Parameters: None
|
|
* Returns:
|
|
* Vector<V> - vector containing all values
|
|
*/
|
|
Vector<V> getValues() const
|
|
{
|
|
Vector<V> values;
|
|
|
|
for (int index = 0; index < entries.getSize(); index++)
|
|
{
|
|
values.push_back(entries[index].value);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
/*
|
|
* Function: getKeyAt
|
|
* Description: Returns the key at the specified internal index.
|
|
* Parameters:
|
|
* index - internal index of the entry
|
|
* Returns:
|
|
* const K& - constant reference to the key
|
|
*/
|
|
const K& getKeyAt(int index) const
|
|
{
|
|
return entries[index].key;
|
|
}
|
|
|
|
/*
|
|
* Function: getValueAt
|
|
* Description: Returns the value at the specified internal index.
|
|
* Parameters:
|
|
* index - internal index of the entry
|
|
* Returns:
|
|
* V& - reference to the value
|
|
*/
|
|
V& getValueAt(int index)
|
|
{
|
|
return entries[index].value;
|
|
}
|
|
|
|
/*
|
|
* Function: getValueAt
|
|
* Description: Returns a constant reference to the value
|
|
* at the specified internal index.
|
|
* Parameters:
|
|
* index - internal index of the entry
|
|
* Returns:
|
|
* const V& - constant reference to the value
|
|
*/
|
|
const V& getValueAt(int index) const
|
|
{
|
|
return entries[index].value;
|
|
}
|
|
};
|
|
} |