Skip to content

Instantly share code, notes, and snippets.

@thosakwe
Created April 21, 2016 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thosakwe/892785ebb001ff9997b5cd78afc68be1 to your computer and use it in GitHub Desktop.
Save thosakwe/892785ebb001ff9997b5cd78afc68be1 to your computer and use it in GitHub Desktop.
FunctionalDict<TKey, TValue>
cmake_minimum_required(VERSION 3.5)
project(FunctionalDict)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11")
set(SOURCE_FILES main.cpp FunctionalDict.h)
add_executable(FunctionalDict ${SOURCE_FILES})
//
// Created by tobe on 4/20/2016.
//
#ifndef FUNCTIONALDICT_FUNCTIONALDICT_H
#define FUNCTIONALDICT_FUNCTIONALDICT_H
#include <vector>
using namespace std;
/**
* My attempt at a function-based dictionary implementation in C++.
*/
template<class TKey, class TValue>
class FunctionalDict {
private:
vector<TKey> keySet;
vector<TValue> valueSet;
public:
/**
* Determines if this dictionary has an entry for the given key.
*/
bool containsKey(TKey);
/**
* Retrieves the value stored for the provided key.
* If no value is found, an error will be thrown.
*/
TValue get(TKey);
/**
* Associates the provided value with a given key.
* If there is already an entry for the given key, it will be overwritten.
*/
int put(TKey, TValue);
/**
* Locates the given key within the dictionary, and returns its index.
* Returns -1 if the key is not found.
*/
int indexOfKey(TKey);
/**
* Locates the given value within the dictionary, and returns its index.
* Returns -1 if the value is not found.
*/
int indexOfValue(TValue);
/**
* Retrieves the value stored for the provided key.
* If no value is found, an error will be thrown.
*/
TValue operator[](TKey);
};
template<class TKey, class TValue>
bool FunctionalDict<TKey, TValue>::containsKey(TKey key) {
for (int i = 0; i < this->keySet.size(); i++) {
if (this->keySet[i] == key) {
return true;
}
}
return false;
}
template<class TKey, class TValue>
int FunctionalDict<TKey, TValue>::indexOfKey(TKey key) {
for (int i = 0; i < this->keySet.size(); i++) {
if (this->keySet[i] == key)
return i;
}
return -1;
}
template<class TKey, class TValue>
int FunctionalDict<TKey, TValue>::indexOfValue(TValue value) {
for (int i = 0; i < this->valueSet.size(); i++) {
if (this->valueSet[i] == value)
return i;
}
return -1;
}
template<class TKey, class TValue>
TValue FunctionalDict<TKey, TValue>::get(TKey key) {
int index = this->indexOfKey(key);
if (index == -1)
throw "Key does not exist in key set.";
return this->valueSet[index];
}
template<class TKey, class TValue>
TValue FunctionalDict<TKey, TValue>::operator[](TKey key) {
return this->get(key);
}
template<class TKey, class TValue>
int FunctionalDict<TKey, TValue>::put(TKey key, TValue value) {
int index = this->indexOfKey(key);
if (index != -1) {
// Overwrite values for existing keys
this->valueSet[index] = value;
return index;
}
this->keySet.push_back(key);
this->valueSet.push_back(value);
return this->keySet.size() - 1;
}
#endif //FUNCTIONALDICT_FUNCTIONALDICT_H
#include <iostream>
#include <cstring>
#include "FunctionalDict.h"
using namespace std;
void assertNumEquals(int actual, int expected, const char *errorMessage) {
if (actual != expected) {
cerr << errorMessage << " fails." << endl;
throw "Invalid value";
}
}
void assertStrEquals(const char* actual, const char* expected, const char *errorMessage) {
if (strcmp(actual, expected) != 0) {
cerr << errorMessage << " fails." << endl;
throw "Invalid value";
}
}
/**
* Tests and shiz
*/
int main() {
FunctionalDict<int, string> dict;
try {
dict.put(420, "blaze it");
dict.put(1337, "H4X0R");
assertStrEquals(dict.get(420).c_str(), "blaze it", "dict.get(key)");
assertStrEquals(dict[420].c_str(), "blaze it", "Array operator");
assertNumEquals(dict.indexOfKey(1337), 1, "indexOfKey");
assertNumEquals(dict.indexOfKey(69), -1, "indexOfKey");
assertNumEquals(dict.indexOfValue("H4X0R"), 1, "indexOfValue");
dict.put(420, "devil's lettuce");
assertStrEquals(dict[420].c_str(), "devil's lettuce", "Reassigning");
cout << "Everything works :)" << endl;
return 0;
} catch (const char *err) {
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment