kmail Library API Documentation

kmdict.cpp

00001 /* simple hash table for kmail.  inspired by QDict */
00002 /* Author: Ronen Tzur <rtzur@shani.net> */
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include "kmdict.h"
00009 #include <kdebug.h>
00010 
00011 #include <string.h>
00012 //-----------------------------------------------------------------------------
00013 
00014 KMDict::KMDict( int size )
00015 {
00016   init( ( int ) KMail::nextPrime( size ) );
00017   //kdDebug( 5006 ) << "KMMDict::KMDict Size: " << mSize << endl;
00018 }
00019 
00020 //-----------------------------------------------------------------------------
00021 
00022 KMDict::~KMDict()
00023 {
00024   clear();
00025 }
00026 
00027 //-----------------------------------------------------------------------------
00028 
00029 void KMDict::init(int size)
00030 {
00031   mSize = size;
00032   mVecs = new KMDictItem *[mSize];
00033   memset(mVecs, 0, mSize * sizeof(KMDictItem *));
00034 }
00035 
00036 //-----------------------------------------------------------------------------
00037 
00038 void KMDict::clear()
00039 {
00040   if (!mVecs)
00041     return;
00042   for (int i = 0; i < mSize; i++) {
00043     KMDictItem *item = mVecs[i];
00044     while (item) {
00045       KMDictItem *nextItem = item->next;
00046       delete item;
00047       item = nextItem;
00048     }
00049   }
00050   delete [] mVecs;
00051   mVecs = 0;
00052 }
00053 
00054 //-----------------------------------------------------------------------------
00055 
00056 void KMDict::replace( long key, KMDictItem *item )
00057 {
00058   insert( key, item );
00059   removeFollowing( item, key );           // remove other items with same key
00060 }
00061 
00062 //-----------------------------------------------------------------------------
00063 
00064 
00065 void KMDict::insert( long key, KMDictItem *item )
00066 {
00067   item->key = key;
00068   int idx = (unsigned long)key % mSize; // insert in
00069   item->next = mVecs[idx];              // appropriate
00070   mVecs[idx] = item;                    // column
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 
00075 void KMDict::remove(long key)
00076 {
00077   int idx = (unsigned long)key % mSize;
00078   KMDictItem *item = mVecs[idx];
00079 
00080   if (item) {
00081     if (item->key == key) {             // if first in the column
00082       mVecs[idx] = item->next;
00083       delete item;
00084     } else
00085       removeFollowing(item, key);       // if deep in the column
00086   }
00087 }
00088 
00089 //-----------------------------------------------------------------------------
00090 
00091 void KMDict::removeFollowing(KMDictItem *item, long key)
00092 {
00093   while (item) {
00094     KMDictItem *itemNext = item->next;
00095     if (itemNext && itemNext->key == key) {
00096       KMDictItem *itemNextNext = itemNext->next;
00097       delete itemNext;
00098       item->next = itemNextNext;
00099     } else
00100       item = itemNext;
00101   }
00102 }
00103 
00104 //-----------------------------------------------------------------------------
00105 
00106 KMDictItem *KMDict::find(long key)
00107 {
00108   int idx = (unsigned long)key % mSize;
00109   KMDictItem *item = mVecs[idx];
00110   while (item) {
00111     if (item->key == key)
00112       break;
00113     item = item->next;
00114   }
00115   return item;
00116 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:20:05 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003