//////////////////////////////////
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/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 3 of the
License, 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
===========================================================================
*/
#ifndef __OG_DICT_H__
#define __OG_DICT_H__
/*
==============================================================================
ogSingleDict
==============================================================================
*/
template
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 stringDict;
ogSingleDict intDict;
ogSingleDict boolDict;
ogSingleDict floatDict;
ogSingleDict vec2Dict;
ogSingleDict vec3Dict;
ogSingleDict 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/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 3 of the
License, 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
===========================================================================
*/
/*
==============================================================================
ogSingleDict
==============================================================================
*/
template
OG_INLINE valType &ogSingleDict::operator[](const char *key)
{
unsigned int hash = makeHash(key);
if(ValueExists(key))
return vals[hash];
else
return insert(key, valType());
}
template
OG_INLINE valType &ogSingleDict::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.html
template
OG_INLINE unsigned int ogSingleDict::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
OG_INLINE bool ogSingleDict::ValueExists(const char *key)
{
int hash = makeHash(key);
return inUse[hash];
}
/*
==============================================================================
ogDict
==============================================================================
*/
OG_INLINE ogDict::ogDict(int _size) : size(_size)
{
/* stringDict = new ogSingleDict(64);
intDict = new ogSingleDict(64);
boolDict = new ogSingleDict(64);
floatDict = new ogSingleDict(64)
vec2Dict = new ogSingleDict;
vec3Dict = new ogSingleDict;
vec4Dict = new ogSingleDict;*/
}
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());
}