kmail

kmdict.cpp
1 /* simple hash table for kmail. inspired by TQDict */
2 /* Author: Ronen Tzur <rtzur@shani.net> */
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #include "kmdict.h"
9 #include "kmglobal.h"
10 #include <kdebug.h>
11 
12 #include <string.h>
13 //-----------------------------------------------------------------------------
14 
15 KMDict::KMDict( int size )
16 {
17  init( ( int ) KMail::nextPrime( size ) );
18  //kdDebug( 5006 ) << "KMMDict::KMDict Size: " << mSize << endl;
19 }
20 
21 //-----------------------------------------------------------------------------
22 
24 {
25  clear();
26 }
27 
28 //-----------------------------------------------------------------------------
29 
30 void KMDict::init(int size)
31 {
32  mSize = size;
33  mVecs = new KMDictItem *[mSize];
34  memset(mVecs, 0, mSize * sizeof(KMDictItem *));
35 }
36 
37 //-----------------------------------------------------------------------------
38 
40 {
41  if (!mVecs)
42  return;
43  for (int i = 0; i < mSize; i++) {
44  KMDictItem *item = mVecs[i];
45  while (item) {
46  KMDictItem *nextItem = item->next;
47  delete item;
48  item = nextItem;
49  }
50  }
51  delete [] mVecs;
52  mVecs = 0;
53 }
54 
55 //-----------------------------------------------------------------------------
56 
57 void KMDict::replace( long key, KMDictItem *item )
58 {
59  insert( key, item );
60  removeFollowing( item, key ); // remove other items with same key
61 }
62 
63 //-----------------------------------------------------------------------------
64 
65 
66 void KMDict::insert( long key, KMDictItem *item )
67 {
68  item->key = key;
69  int idx = (unsigned long)key % mSize; // insert in
70  item->next = mVecs[idx]; // appropriate
71  mVecs[idx] = item; // column
72 }
73 
74 //-----------------------------------------------------------------------------
75 
76 void KMDict::remove(long key)
77 {
78  int idx = (unsigned long)key % mSize;
79  KMDictItem *item = mVecs[idx];
80 
81  if (item) {
82  if (item->key == key) { // if first in the column
83  mVecs[idx] = item->next;
84  delete item;
85  } else
86  removeFollowing(item, key); // if deep in the column
87  }
88 }
89 
90 //-----------------------------------------------------------------------------
91 
92 void KMDict::removeFollowing(KMDictItem *item, long key)
93 {
94  while (item) {
95  KMDictItem *itemNext = item->next;
96  if (itemNext && itemNext->key == key) {
97  KMDictItem *itemNextNext = itemNext->next;
98  delete itemNext;
99  item->next = itemNextNext;
100  } else
101  item = itemNext;
102  }
103 }
104 
105 //-----------------------------------------------------------------------------
106 
108 {
109  int idx = (unsigned long)key % mSize;
110  KMDictItem *item = mVecs[idx];
111  while (item) {
112  if (item->key == key)
113  break;
114  item = item->next;
115  }
116  return item;
117 }
Class representing items in a KMDict.
Definition: kmdict.h:12
void remove(long key)
Removes an item.
Definition: kmdict.cpp:76
void clear()
Clears the hash table, removing all items.
Definition: kmdict.cpp:39
KMDict(int size=17)
Creates a hash table with size columns.
Definition: kmdict.cpp:15
~KMDict()
Destroys the hash table object.
Definition: kmdict.cpp:23
KMDictItem * find(long key)
Find an item by key.
Definition: kmdict.cpp:107
int size()
Returns the size of the hash table.
Definition: kmdict.h:40
void insert(long key, KMDictItem *item)
Inserts an item without replacing ones with the same key.
Definition: kmdict.cpp:66
void replace(long key, KMDictItem *item)
Inserts an item, replacing old ones with the same key.
Definition: kmdict.cpp:57