summaryrefslogtreecommitdiffstats
path: root/kmail/kmdict.cpp
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commit460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 (patch)
tree67208f7c145782a7e90b123b982ca78d88cc2c87 /kmail/kmdict.cpp
downloadtdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.tar.gz
tdepim-460c52653ab0dcca6f19a4f492ed2c5e4e963ab0.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kmail/kmdict.cpp')
-rw-r--r--kmail/kmdict.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/kmail/kmdict.cpp b/kmail/kmdict.cpp
new file mode 100644
index 000000000..3386f16ad
--- /dev/null
+++ b/kmail/kmdict.cpp
@@ -0,0 +1,117 @@
+/* simple hash table for kmail. inspired by QDict */
+/* Author: Ronen Tzur <rtzur@shani.net> */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "kmdict.h"
+#include "kmglobal.h"
+#include <kdebug.h>
+
+#include <string.h>
+//-----------------------------------------------------------------------------
+
+KMDict::KMDict( int size )
+{
+ init( ( int ) KMail::nextPrime( size ) );
+ //kdDebug( 5006 ) << "KMMDict::KMDict Size: " << mSize << endl;
+}
+
+//-----------------------------------------------------------------------------
+
+KMDict::~KMDict()
+{
+ clear();
+}
+
+//-----------------------------------------------------------------------------
+
+void KMDict::init(int size)
+{
+ mSize = size;
+ mVecs = new KMDictItem *[mSize];
+ memset(mVecs, 0, mSize * sizeof(KMDictItem *));
+}
+
+//-----------------------------------------------------------------------------
+
+void KMDict::clear()
+{
+ if (!mVecs)
+ return;
+ for (int i = 0; i < mSize; i++) {
+ KMDictItem *item = mVecs[i];
+ while (item) {
+ KMDictItem *nextItem = item->next;
+ delete item;
+ item = nextItem;
+ }
+ }
+ delete [] mVecs;
+ mVecs = 0;
+}
+
+//-----------------------------------------------------------------------------
+
+void KMDict::replace( long key, KMDictItem *item )
+{
+ insert( key, item );
+ removeFollowing( item, key ); // remove other items with same key
+}
+
+//-----------------------------------------------------------------------------
+
+
+void KMDict::insert( long key, KMDictItem *item )
+{
+ item->key = key;
+ int idx = (unsigned long)key % mSize; // insert in
+ item->next = mVecs[idx]; // appropriate
+ mVecs[idx] = item; // column
+}
+
+//-----------------------------------------------------------------------------
+
+void KMDict::remove(long key)
+{
+ int idx = (unsigned long)key % mSize;
+ KMDictItem *item = mVecs[idx];
+
+ if (item) {
+ if (item->key == key) { // if first in the column
+ mVecs[idx] = item->next;
+ delete item;
+ } else
+ removeFollowing(item, key); // if deep in the column
+ }
+}
+
+//-----------------------------------------------------------------------------
+
+void KMDict::removeFollowing(KMDictItem *item, long key)
+{
+ while (item) {
+ KMDictItem *itemNext = item->next;
+ if (itemNext && itemNext->key == key) {
+ KMDictItem *itemNextNext = itemNext->next;
+ delete itemNext;
+ item->next = itemNextNext;
+ } else
+ item = itemNext;
+ }
+}
+
+//-----------------------------------------------------------------------------
+
+KMDictItem *KMDict::find(long key)
+{
+ int idx = (unsigned long)key % mSize;
+ KMDictItem *item = mVecs[idx];
+ while (item) {
+ if (item->key == key)
+ break;
+ item = item->next;
+ }
+ return item;
+}