//////////////////////////////////Dict.h///////////////////////////////////*===========================================================================Copyright (C) 2007 ice (icefragment@gmail.com)Purpose: Dictionary (single and multiple value types)This file is part of the Open Game Library.The Open Game Library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General Public License aspublished by the Free Software Foundation; either version 3 of theLicense, or (at your option) any later version.Open Game Library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.===========================================================================*/#ifndef __OG_DICT_H__#define __OG_DICT_H__/*============================================================================== ogSingleDict==============================================================================*/template<typename valType>class ogSingleDict{public: OG_INLINE ogSingleDict(int _size = 64) : size(_size) { vals = new valType[_size]; inUse = new bool[_size]; } OG_INLINE ~ogSingleDict() { delete[] vals; } valType &operator[](const char *key); bool ValueExists(const char *key);private: unsigned int makeHash(const char *key); valType &insert(const char *index, valType key); valType *vals; bool *inUse; int size;};/*============================================================================== ogDict==============================================================================*/class ogDict{public: ogDict(int _size = 64); ~ogDict(); OG_INLINE void SetString(const char *key, ogString &str) { stringDict[key] = str; } OG_INLINE void SetInt(const char *key, int i) { intDict[key] = i; } OG_INLINE void SetBool(const char *key, bool b) { boolDict[key] = b; } OG_INLINE void SetFloat(const char *key, float f) { floatDict[key] = f; } OG_INLINE void SetVec2(const char *key, ogVec2 &vec) { vec2Dict[key] = vec; } OG_INLINE void SetVec3(const char *key, ogVec3 &vec) { vec3Dict[key] = vec; } OG_INLINE void SetVec4(const char *key, ogVec4 &vec) { vec4Dict[key] = vec; } OG_INLINE void GetString(const char *key, const char *def, ogString &str) { str = stringDict[key]; } OG_INLINE void GetInt(const char *key, int def, int &i) { i = intDict[key]; } OG_INLINE void GetBool(const char *key, bool def, bool &b) { b = boolDict[key]; } OG_INLINE void GetFloat(const char *key, float def, float &f) { f = floatDict[key]; } OG_INLINE void GetVec2(const char *key, ogVec2 &vec) { vec = vec2Dict[key]; } OG_INLINE void GetVec3(const char *key, ogVec3 &vec) { vec = vec3Dict[key]; } OG_INLINE void GetVec4(const char *key, ogVec4 &vec) { vec = vec4Dict[key]; }private: ogSingleDict<ogString> stringDict; ogSingleDict<int> intDict; ogSingleDict<bool> boolDict; ogSingleDict<float> floatDict; ogSingleDict<ogVec2> vec2Dict; ogSingleDict<ogVec3> vec3Dict; ogSingleDict<ogVec4> vec4Dict; int size;};#include "Dict.inl"#endif//////////////////////////////////Dict.inl///////////////////////////////////*===========================================================================Copyright (C) 2007 ice (icefragment@gmail.com)Purpose: Dictionary (single and multiple value types)This file is part of the Open Game Library.The Open Game Library is free software; you can redistribute it and/ormodify it under the terms of the GNU Lesser General Public License aspublished by the Free Software Foundation; either version 3 of theLicense, or (at your option) any later version.Open Game Library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.===========================================================================*//*============================================================================== ogSingleDict==============================================================================*/template<typename valType>OG_INLINE valType &ogSingleDict<valType>::operator[](const char *key){ unsigned int hash = makeHash(key); if(ValueExists(key)) return vals[hash]; else return insert(key, valType());}template<typename valType>OG_INLINE valType &ogSingleDict<valType>::insert(const char *key, valType val){ int hash = makeHash(key); vals[hash] = val; inUse[hash] = true; return vals[hash];}// Daniel J. Bernstein's hash// http://www.partow.net/programming/hashfunctions/index.htmltemplate<typename valType>OG_INLINE unsigned int ogSingleDict<valType>::makeHash(const char *key){ int hash = 5381; // prime int i; for(i = 0; key[i] != '\0'; i++) hash = ((hash << 5) + hash) + key[i]; return (hash & size) & 0x7fffffff;}template<typename valType>OG_INLINE bool ogSingleDict<valType>::ValueExists(const char *key){ int hash = makeHash(key); return inUse[hash];}/*============================================================================== ogDict==============================================================================*/OG_INLINE ogDict::ogDict(int _size) : size(_size){/* stringDict = new ogSingleDict<ogString>(64); intDict = new ogSingleDict<int>(64); boolDict = new ogSingleDict<bool>(64); floatDict = new ogSingleDict<float>(64) vec2Dict = new ogSingleDict<ogVec2>; vec3Dict = new ogSingleDict<ogVec3>; vec4Dict = new ogSingleDict<ogVec4>;*/}OG_INLINE ogDict::~ogDict(){/* delete stringDict; delete intDict; delete boolDict; delete floatDict; delete vec2Dict; delete vec3Dict; delete vec4Dict;*/}//////////////////////////////////GameLocal.cpp///////////////////////////////////*================ogGameLocal::DictTest================*/void ogGameLocal::DictTest( void ) { common->Printf("\n--------- Dictionary test ---------\n"); ogDict d1; d1.SetVec3("key1", ogVec3(23, 47, 61)); d1.SetBool("key 2", false); d1.SetString("string", ogString("QWERTY")); ogVec3 v3Res; bool bRes; ogString strRes; d1.GetVec3("key1", v3Res); d1.GetBool("key 2", true, bRes); d1.GetString("string", "default", strRes); common->Printf("key1: %s\n", v3Res.ToString()); common->Printf("key 2: %i\n", bRes); common->Printf("string: %s\n", strRes.c_str());}