FANDOM


This is Dictionary class (as in the .NET one) for C++. It is open to any fixes.

DocumentationEdit

None yet.

CodeEdit

/*
Dictionary Class

This is released under Matthew's Code License (See Below)

MATTHEW'S CODE LICENSE

This license will be removed once implemented into your
code, it is relicensed under your program's one, be it
proprietary or free.

Note that std::map is vastly faster, provides iterator
and is part of the standard, there isn't any reason to
ever use this class.

*/

#ifndef DICTIONARY_H
#define DICTIONARY_H

#include <vector>

namespace std{	
	template<typename _key, typename _value>
	class Dictionary
	{
		protected:
		
		vector<_key> keyList;
		vector<_value> valueList;
		
		template<typename _search>
		unsigned int tFindKey(vector<_search> searchList, _search keyName)
		{
			for(unsigned int i=0;i<searchList.size();i++)
			{
				if (searchList[i] == keyName)
					return i;
			}
			return KeyNotFound();
		}
		
		template<typename _search>
		vector<unsigned int> tFindKeys(vector<_search> searchList, vector<_search> keyNames)
		{
			unsigned int j;
			vector<unsigned int> returnValue;
			for(unsigned int i=0;i<searchList.size();i++)
			{
				for(j=0;j<keyNames.size();j++)
				{
					if (keyNames[j] == searchList[i])
					{
						returnValue.push_back(i);
						break;
					}
				}
			}
			return returnValue;
		}
		
		unsigned int pFindKey(_key keyName)
		{
			return tFindKey<_key>(keyList, keyName);
		}
		vector<unsigned int> pFindKeys(vector<_key> keyNames)
		{
			return tFindKeys<_key>(keyList, keyNames);
		}
		
		unsigned int pFindValue(_value dataName)
		{
			return tFindKey<_value>(valueList, dataName);
		}
		vector<unsigned int> pFindValues(vector<_value> dataNames)
		{
			return tFindKeys<_value>(valueList, dataNames);
		}
		
		_value& pReadKey(_key keyName)
		{
			unsigned int i = pFindKey(keyName);
			if (i == KeyNotFound())
				throw KeyNotFoundException;
			else
				return valueList[i];
		}
		
		bool pAddKey(_key keyName, _value keyValue)
		{
			unsigned int i = pFindKey(keyName);
			if (i == KeyNotFound())
			{
				keyList.push_back(keyName);
				valueList.push_back(keyValue);
				return true;
			}
			else
			{
				throw KeyAlreadyExistsException;
			}
		}
		
		public:
		
		const static unsigned int KeyNotFoundException = 32767, KeyAlreadyExistsException = 32768;
		
		Dictionary()
		{
			
		}
		
		_value& Item(_key keyName)
		{
			return pReadKey(keyName);
		}
		_value& operator[](_key keyName)
		{
			return pReadKey(keyName);
		}
		
		bool ContainsKey(_key keyName)
		{
			return pFindKey(keyName) != KeyNotFound();
		}
		bool ContainsValue(_value dataName)
		{
			return pFindValue(dataName) != KeyNotFound();
		}
		
		bool AddKey(_key keyName, _value keyData)
		{
			return pAddKey(keyName, keyData);
		}
		
		vector<_key> Items(void)
		{
			return keyList;
		}
		
		bool RemoveKey(_key keyName)
		{
			unsigned int f = pFindKey(keyName);
			vector<_key> newKey;
			vector<_value> newValue;
			if (f == KeyNotFound())
				throw KeyNotFoundException;
			else
			{
				for(unsigned int i=0;i<keyList.size();i++)
				{
					if (i != f)
					{
						newKey.push_back(keyList[i]);
						newValue.push_back(valueList[i]);
					}
				}
			}
			keyList = newKey;
			valueList = newValue;
			return true;
		}
		bool RemoveKeys(vector<_key> keyNames)
		{
			vector<unsigned int> keyIndexes;
			vector<_key> newKey;
			vector<_value> newValue;
			keyIndexes = tFindKeys<_key>(keyList, keyNames);
			unsigned int i, j;
			bool addItem;
			for(i=0;i<keyList.size();i++)
			{
				addItem = true;
				for(j=0;j<keyIndexes.size();j++)
				{
					if (keyList[i] == keyIndexes[j])
					{
						addItem = false;
						break;
					}
				}
				if (addItem)
				{
					newKey.push_back(keyList[i]);
					newValue.push_back(valueList[i]);
				}
			}
			keyList = newKey;
			valueList = newValue;
			return true;
		}
		
		unsigned int KeyNotFound(void)
		{
			return keyList.max_size()+1;
		}
	};
}

#endif

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.