TDE personal information management applications
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

118 Zeilen
2.7KB

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