GUIDOLib  1.7.7
Guido Engine Internal Documentation
kr_hash.h
1 /*
2  GUIDO Library
3  Copyright (C) 2002 Holger Hoos, Juergen Kilian, Kai Renz
4  Copyright (C) 2002-2017 Grame
5 
6  This Source Code Form is subject to the terms of the Mozilla Public
7  License, v. 2.0. If a copy of the MPL was not distributed with this
8  file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
11  research@grame.fr
12 
13 */
14 
15 #ifndef __KR_HASHTABLE__
16 #define __KR_HASHTABLE__
17 
18 // this saves the entries ...
19 #include "kf_ilist.h"
20 
21 template<class KEY,class TYPE>
23 {
24 public:
25  class KeyType
26  {
27  public:
28  KeyType(const KEY &parkey,
29  const TYPE &partype)
30  {
31  key = parkey;
32  val = partype;
33  }
34  KEY key;
35  TYPE val;
36  int ValGreater(const KeyType &kt) const
37  {
38  return val>kt.val;
39  }
40  int KeyGreater(const KeyType &kt) const
41  {
42  return key>kt.key;
43  }
44  };
45 
46 private:
47 
48  KF_IPointerList<KeyType> *entrylist;
49 
50 public:
51  static int compVal(const KeyType *t1, const KeyType *t2);
52  static int compKey(const KeyType *t1, const KeyType *t2);
53 
54 
55 public:
57  {
58  entrylist = new KF_IPointerList<KeyType>(1);
59  }
60 
61  virtual ~KR_HashTable()
62  {
63  delete entrylist;
64  }
65 
66 
67  GuidoPos Lookup(const KEY &key,
68  TYPE **value)
69  {
70  GuidoPos pos = entrylist->GetHeadPosition();
71  while (pos)
72  {
73  KeyType *kt = entrylist->GetAt(pos);
74  if (kt->key == key)
75  {
76  *value = &kt->val;
77  return pos;
78  }
79  entrylist->GetNext(pos);
80  }
81  *value = NULL;
82  return NULL;
83  }
84  // returns
85  // pos in the hashtable
86  // NULL -> not successfull
87  GuidoPos Lookup(const KEY &key,
88  TYPE &value) const
89  {
90 
91  GuidoPos pos = entrylist->GetHeadPosition();
92  while (pos)
93  {
94  KeyType *kt = entrylist->GetAt(pos);
95  if (kt->key == key)
96  {
97  value = kt->val;
98  return pos;
99  }
100  entrylist->GetNext(pos);
101  }
102  return NULL;
103  }
104 
105  // returns
106  // 1: if replaced with something else
107  // 0: if new entry
108  int Set(const KEY &key,const TYPE &value)
109  {
110  GuidoPos pos = entrylist->GetHeadPosition();
111  while (pos)
112  {
113  KeyType *kt = entrylist->GetAt(pos);
114  if (kt->key == key)
115  {
116  kt->val = value;
117  return 1;
118  }
119  entrylist->GetNext(pos);
120  }
121 
122  // now we have to create a new entry
123  KeyType *kt = new KeyType(key,value);
124  entrylist->AddTail(kt);
125  return 0;
126  }
127 
128  int Set(GuidoPos pos,const KEY &key,const TYPE &value)
129  {
130 
131  KeyType *kt = entrylist->GetAt(pos);
132  kt->key = key;
133  kt->val = value;
134 
135  // alway replacing something ...
136  return 1;
137  }
138 
139  void Delete(const KEY &key)
140  {
141  TYPE *val = NULL;
142  GuidoPos pos = NULL;
143  if ((pos = Lookup(key,&val) ) != NULL)
144  {
145  // We found it ... then
146  // we remove it ....
147 
148  entrylist->RemoveElementAt(pos);
149 
150  }
151  }
152 
153  void DeleteAll()
154  {
155  entrylist->RemoveAll();
156  }
157 
158  int GetCount()
159  {
160  return entrylist->GetCount();
161  }
162  GuidoPos GetHeadPosition()
163  {
164  return entrylist->GetHeadPosition();
165  }
166 
167  // 1 if successfull
168  // 0 if not.
169  int GetNext(GuidoPos &pos,TYPE &val)
170  {
171  if (pos)
172  {
173  KeyType *kt = entrylist->GetAt(pos);
174  val = kt->val;
175  entrylist->GetNext(pos);
176  return 1;
177  }
178  return 0;
179  }
180 
181  // 1 if successfull
182  // 0 if not.
183  int GetNext(GuidoPos &pos,KeyType **kt)
184  {
185  if (pos)
186  {
187  *kt = entrylist->GetAt(pos);
188  entrylist->GetNext(pos);
189  return 1;
190  }
191  return 0;
192  }
193 
194  // sorts the Entries in the
195  // HashTable according to the VALUES
196  void sortValues()
197  {
198  // the sort-function for
199  // the entries.... this
200  // sorts hash-table according
201  // to the entries ...
202  if (entrylist->GetCount()>1)
203  entrylist->sort( &(KR_HashTable<KEY,TYPE>::compVal) );
204 
205  }
206 
207  // sorts the Entries of the
208  // HashTable accoring to the Keys
209  void sortKeys()
210  {
211  if (entrylist->GetCount()>1)
212  entrylist->sort( &(KR_HashTable<KEY,TYPE>::compKey) );
213  }
214 
215 
216 };
217 
218 template<class KEY,class TYPE>
219 int KR_HashTable<KEY,TYPE>::compVal(const KeyType *t1, const KeyType *t2)
220 {
221  if (t1->ValGreater(*t2) ) return 1;
222  return 0;
223 }
224 
225 template<class KEY,class TYPE>
226 int KR_HashTable<KEY,TYPE>::compKey(const KeyType *t1, const KeyType *t2)
227 {
228  if (t1->KeyGreater(*t2) ) return 1;
229  return 0;
230 }
231 
232 
233 #endif // __KR_HASHTABLE__
KR_HashTable::KeyType
Definition: kr_hash.h:25
KF_List< TYPE * >::AddTail
GuidoPos AddTail(TYPE * data)
Definition: kf_list.h:78
KR_HashTable::Lookup
GuidoPos Lookup(const KEY &key, TYPE **value)
Definition: kr_hash.h:67
KF_IPointerList::RemoveAll
virtual void RemoveAll()
Definition: kf_ilist.h:94
KR_HashTable::GetCount
int GetCount()
Definition: kr_hash.h:158
KF_List< TYPE * >::GetCount
int GetCount() const
Definition: kf_list.h:94
KR_HashTable::Set
int Set(const KEY &key, const TYPE &value)
Definition: kr_hash.h:108
KF_List< TYPE * >::GetNext
TYPE * GetNext(GuidoPos &pos) const
Definition: kf_list.h:105
KR_HashTable::compKey
static int compKey(const KeyType *t1, const KeyType *t2)
Definition: kr_hash.h:226
KR_HashTable::GetNext
int GetNext(GuidoPos &pos, TYPE &val)
Definition: kr_hash.h:169
KR_HashTable::Set
int Set(GuidoPos pos, const KEY &key, const TYPE &value)
Definition: kr_hash.h:128
KR_HashTable::KeyType::KeyGreater
int KeyGreater(const KeyType &kt) const
Definition: kr_hash.h:40
KR_HashTable::KeyType::KeyType
KeyType(const KEY &parkey, const TYPE &partype)
Definition: kr_hash.h:28
KR_HashTable::KeyType::val
TYPE val
Definition: kr_hash.h:35
KR_HashTable::sortValues
void sortValues()
Definition: kr_hash.h:196
KR_HashTable::KeyType::key
KEY key
Definition: kr_hash.h:34
KF_List< TYPE * >::GetAt
TYPE * GetAt(GuidoPos pos) const
Definition: kf_list.h:119
KF_IPointerList
Definition: ARMusicalVoiceState.h:33
KR_HashTable::GetHeadPosition
GuidoPos GetHeadPosition()
Definition: kr_hash.h:162
KR_HashTable::~KR_HashTable
virtual ~KR_HashTable()
Definition: kr_hash.h:61
KR_HashTable
Definition: kr_hash.h:22
KR_HashTable::compVal
static int compVal(const KeyType *t1, const KeyType *t2)
Definition: kr_hash.h:219
KR_HashTable::KeyType::ValGreater
int ValGreater(const KeyType &kt) const
Definition: kr_hash.h:36
KR_HashTable::KR_HashTable
KR_HashTable()
Definition: kr_hash.h:56
KR_HashTable::Delete
void Delete(const KEY &key)
Definition: kr_hash.h:139
KR_HashTable::Lookup
GuidoPos Lookup(const KEY &key, TYPE &value) const
Definition: kr_hash.h:87
KF_IPointerList::sort
virtual void sort(int comp(const TYPE *, const TYPE *))
Definition: kf_ilist.h:135
KR_HashTable::DeleteAll
void DeleteAll()
Definition: kr_hash.h:153
KR_HashTable::sortKeys
void sortKeys()
Definition: kr_hash.h:209
KR_HashTable::GetNext
int GetNext(GuidoPos &pos, KeyType **kt)
Definition: kr_hash.h:183
KF_List< TYPE * >::GetHeadPosition
GuidoPos GetHeadPosition(void) const
Definition: kf_list.h:102
KF_IPointerList::RemoveElementAt
virtual void RemoveElementAt(GuidoPos pos)
Definition: kf_ilist.h:126

Guido Project Copyright © 2019 Grame-CNCM