Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created June 26, 2021 10:40
Show Gist options
  • Select an option

  • Save DreamVB/1c22d0163ec641ea3bff6641b03a824c to your computer and use it in GitHub Desktop.

Select an option

Save DreamVB/1c22d0163ec641ea3bff6641b03a824c to your computer and use it in GitHub Desktop.
Custom map data structure using arrays and classes
//A Custom map data structure using arrays and classes
//Features
// Add new keys and values
// Edit a key and set a new value
// Delete a key and it's value
// Remove all keys to clear up the class
// Get key from an index usfull it you don't know the key
#include <iostream>
using namespace std;
template <typename Key, typename Value>
class TMap{
protected:
//This is a helper class for the map class
class TNode{
public:
//Key for map
Key m_key;
//Keys value
Value m_value;
};
//Holds the TNodes
TNode *m_nodes = NULL;
int get_key_idx(Key k){
//Locate a key in the nodes list
int x = 0;
//Bad key not found
int idx = -1;
//Loop tho the nodes
while (x < this->index){
//Check if nodes key equals input key k
if (m_nodes[x].m_key == k){
//Set the key found index
idx = x;
//Out of here
break;
}
x++;
}
//Return key index
return idx;
}
private:
int m_size = 0;
int index = 0;
public:
TMap(int size){
//Set the map size
this->m_size = size;
//Resize the nodes list
m_nodes = new TNode[size];
}
~TMap(){
//If size is not zero clear up the class
if (this->m_size != 0)
this->remove_all();
}
void add(Key key, Value val){
//Add key and value to node
m_nodes[index].m_key = key;
m_nodes[index].m_value = val;
index++;
}
void set(Key index, Value val){
//Get key index
int idx = this->get_key_idx(index);
//Check for vaild key
if (idx != -1){
//Set the node value
m_nodes[idx].m_value = val;
}
}
Value get(Key index){
//Get key index
int idx = this->get_key_idx(index);
//Check for vaild key index
if (idx == -1){
std::cout << "Cannot find the key";
}
else{
//Return the keys value
return m_nodes[idx].m_value;
}
}
void remove(Key key){
//Get the key index to remove
int idx = this->get_key_idx(key);
//Check for vaild key
if (idx != -1){
//Start at key into and go tho the nodes
for (int x = idx; x < index; x++){
//Shift the nodes
m_nodes[x] = m_nodes[x + 1];
}
}
//DEC counters
index--;
m_size--;
}
void remove_all(void){
//Remove all the nodes from the map.
m_size = 0;
index = 0;
//Delete the nodes array
delete[]m_nodes;
}
Key get_key(int index){
//Return a key from the nodes list by using index
return m_nodes[index].m_key;
}
int size(){
//Return the number of nodes on the map
return index;
}
};
//Declare map class size 100 should be ok for our demo
TMap<char, int>chars(100);
void print_map(void){
char c;
for (int x = 0; x < chars.size(); x++){
//Get key
c = chars.get_key(x);
//Print out key and it's value
std::cout << chars.get_key(x) <<
"==" << chars.get(c) << std::endl;
}
}
int main(){
//Add char values onto map, Use char as key
//For example A=65
for (char c = 'A'; c <= 'L'; c++){
//Add new key and value
chars.add(c, (int)c);
}
//Print map
print_map();
//Delete A and G keys
chars.remove('A');
chars.remove('G');
//Reprint out
std::cout << "Printing Data After Delete." << std::endl;
//print map
print_map();
//Clear all nodes in the map
chars.remove_all();
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment