00001 /****************************************************************************** 00002 * 00003 * $Id: sortdict.h,v 1.3 2001/03/19 19:27:41 root Exp $ 00004 * 00005 * 00006 * Copyright (C) 1997-2008 by Dimitri van Heesch. 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation under the terms of the GNU General Public License is hereby 00010 * granted. No representations are made about the suitability of this software 00011 * for any purpose. It is provided "as is" without express or implied warranty. 00012 * See the GNU General Public License for more details. 00013 * 00014 * Documents produced by Doxygen are derivative works derived from the 00015 * input used in their production; they are not affected by this license. 00016 * 00017 */ 00018 00019 #ifndef _SORTDICT_H 00020 #define _SORTDICT_H 00021 00022 #include "qtbc.h" 00023 #include <qlist.h> 00024 #include <qdict.h> 00025 #include <qintdict.h> 00026 00027 #define AUTORESIZE 1 00028 00029 #if AUTORESIZE 00030 const uint SDict_primes[] = 00031 { 00032 17, 00033 29, 00034 47, 00035 71, 00036 113, 00037 179, 00038 293, 00039 457, 00040 733, 00041 1171, 00042 1871, 00043 2999, 00044 4787, 00045 7669, 00046 12251, 00047 19603, 00048 31379, 00049 50177, 00050 80287, 00051 128449, 00052 205519, 00053 328829, 00054 526139, 00055 841801, 00056 1346881, 00057 2155007, 00058 3448033, 00059 5516827, 00060 8826919, 00061 14123059, 00062 23538433, 00063 39230771, 00064 65384537, 00065 108974231, 00066 181623707, 00067 302706181, 00068 504510283, 00069 840850487, 00070 0xffffffff 00071 }; 00072 #endif 00073 00074 template<class T> class SDict; 00075 template<class T> class SIntDict; 00076 00080 template<class T> 00081 class SList : public QList<T> 00082 { 00083 public: 00084 SList(SDict<T> *owner) : m_owner(owner) {} 00085 virtual ~SList() {} 00086 int compareItems(GCI item1,GCI item2) 00087 { 00088 return m_owner->compareItems(item1,item2); 00089 } 00090 private: 00091 SDict<T> *m_owner; 00092 }; 00093 00097 template<class T> 00098 class SDict 00099 { 00100 private: 00101 SList<T> *m_list; 00102 QDict<T> *m_dict; 00103 int m_sizeIndex; 00104 00105 public: 00110 SDict(int size) : m_sizeIndex(0) 00111 { 00112 m_list = new SList<T>(this); 00113 #if AUTORESIZE 00114 while ((uint)size>SDict_primes[m_sizeIndex]) m_sizeIndex++; 00115 m_dict = new QDict<T>(SDict_primes[m_sizeIndex]); 00116 #else 00117 m_dict = new QDict<T>(size); 00118 #endif 00119 } 00120 00122 virtual ~SDict() 00123 { 00124 delete m_list; 00125 delete m_dict; 00126 } 00127 00134 void append(const char *key,const T *d) 00135 { 00136 m_list->append(d); 00137 m_dict->insert(key,d); 00138 #if AUTORESIZE 00139 if (m_dict->size()>SDict_primes[m_sizeIndex]) 00140 { 00141 m_dict->resize(SDict_primes[++m_sizeIndex]); 00142 } 00143 #endif 00144 } 00145 00152 void prepend(const char *key,const T *d) 00153 { 00154 m_list->prepend(d); 00155 m_dict->insert(key,d); 00156 #if AUTORESIZE 00157 if (m_dict->size()>SDict_primes[m_sizeIndex]) 00158 { 00159 m_dict->resize(SDict_primes[++m_sizeIndex]); 00160 } 00161 #endif 00162 } 00163 00165 bool remove(const char *key) 00166 { 00167 T *item = m_dict->take(key); 00168 return item ? m_list->remove(item) : FALSE; 00169 } 00170 00172 T *take(const char *key) 00173 { 00174 T *item = m_dict->take(key); 00175 if (item) 00176 { 00177 int i = m_list->find(item); 00178 m_list->take(i); 00179 } 00180 return item; 00181 } 00182 00187 void sort() 00188 { 00189 m_list->sort(); 00190 } 00196 void inSort(const char *key,const T *d) 00197 { 00198 m_list->inSort(d); 00199 m_dict->insert(key,d); 00200 #if AUTORESIZE 00201 if (m_dict->size()>SDict_primes[m_sizeIndex]) 00202 { 00203 m_dict->resize(SDict_primes[++m_sizeIndex]); 00204 } 00205 #endif 00206 } 00207 00209 void setAutoDelete(bool val) 00210 { 00211 m_list->setAutoDelete(val); 00212 } 00213 00219 T *find(const char *key) 00220 { 00221 return m_dict->find(key); 00222 } 00223 T *find(const QCString &key) 00224 { 00225 return m_dict->find(key); 00226 } 00227 T *find(const QString &key) 00228 { 00229 return m_dict->find(key); 00230 } 00231 00233 T *operator[](const char *key) const 00234 { 00235 return m_dict->find(key); 00236 } 00237 00239 T *at(uint i) 00240 { 00241 return m_list->at(i); 00242 } 00243 00248 virtual int compareItems(GCI item1,GCI item2) 00249 { 00250 return item1!=item2; 00251 } 00252 00257 void clear() 00258 { 00259 m_list->clear(); 00260 m_dict->clear(); 00261 } 00262 00265 int count() const 00266 { 00267 return m_list->count(); 00268 } 00269 00270 class Iterator; // first forward declare 00271 friend class Iterator; // then make it a friend 00275 class Iterator 00276 { 00277 public: 00279 Iterator(const SDict<T> &dict) 00280 { 00281 m_li = new QListIterator<T>(*dict.m_list); 00282 } 00283 00285 virtual ~Iterator() 00286 { 00287 delete m_li; 00288 } 00289 00293 T *toFirst() const 00294 { 00295 return m_li->toFirst(); 00296 } 00297 00301 T *toLast() const 00302 { 00303 return m_li->toLast(); 00304 } 00305 00307 T *current() const 00308 { 00309 return m_li->current(); 00310 } 00311 00316 T *operator++() 00317 { 00318 return m_li->operator++(); 00319 } 00320 00325 T *operator--() 00326 { 00327 return m_li->operator--(); 00328 } 00329 00330 private: 00331 QListIterator<T> *m_li; 00332 }; 00333 00334 class IteratorDict; // first forward declare 00335 friend class IteratorDict; // then make it a friend 00339 class IteratorDict 00340 { 00341 public: 00343 IteratorDict(const SDict<T> &dict) 00344 { 00345 m_di = new QDictIterator<T>(*dict.m_dict); 00346 } 00347 00349 virtual ~IteratorDict() 00350 { 00351 delete m_di; 00352 } 00353 00357 T *toFirst() const 00358 { 00359 return m_di->toFirst(); 00360 } 00361 00365 T *toLast() const 00366 { 00367 return m_di->toLast(); 00368 } 00369 00371 T *current() const 00372 { 00373 return m_di->current(); 00374 } 00375 00377 QCString currentKey() const 00378 { 00379 return m_di->currentKey(); 00380 } 00381 00386 T *operator++() 00387 { 00388 return m_di->operator++(); 00389 } 00390 00395 T *operator--() 00396 { 00397 return m_di->operator--(); 00398 } 00399 00400 private: 00401 QDictIterator<T> *m_di; 00402 }; 00403 }; 00404 00408 template<class T> 00409 class SIntList : public QList<T> 00410 { 00411 public: 00412 SIntList(SIntDict<T> *owner) : m_owner(owner) {} 00413 virtual ~SIntList() {} 00414 int compareItems(GCI item1,GCI item2) 00415 { 00416 return m_owner->compareItems(item1,item2); 00417 } 00418 private: 00419 SIntDict<T> *m_owner; 00420 }; 00421 00425 template<class T> 00426 class SIntDict 00427 { 00428 private: 00429 SIntList<T> *m_list; 00430 QIntDict<T> *m_dict; 00431 int m_sizeIndex; 00432 00433 public: 00438 SIntDict(int size) : m_sizeIndex(0) 00439 { 00440 m_list = new SIntList<T>(this); 00441 #if AUTORESIZE 00442 while ((uint)size>SDict_primes[m_sizeIndex]) m_sizeIndex++; 00443 m_dict = new QIntDict<T>(SDict_primes[m_sizeIndex]); 00444 #else 00445 m_dict = new QIntDict<T>(size); 00446 #endif 00447 } 00448 00450 virtual ~SIntDict() 00451 { 00452 delete m_list; 00453 delete m_dict; 00454 } 00455 00462 void append(int key,const T *d) 00463 { 00464 m_list->append(d); 00465 m_dict->insert(key,d); 00466 #if AUTORESIZE 00467 if (m_dict->size()>SDict_primes[m_sizeIndex]) 00468 { 00469 m_dict->resize(SDict_primes[++m_sizeIndex]); 00470 } 00471 #endif 00472 } 00473 00480 void prepend(int key,const T *d) 00481 { 00482 m_list->prepend(d); 00483 m_dict->insert(key,d); 00484 #if AUTORESIZE 00485 if (m_dict->size()>SDict_primes[m_sizeIndex]) 00486 { 00487 m_dict->resize(SDict_primes[++m_sizeIndex]); 00488 } 00489 #endif 00490 } 00491 00493 bool remove(int key) 00494 { 00495 T *item = m_dict->take(key); 00496 return item ? m_list->remove(item) : FALSE; 00497 } 00498 00503 void sort() 00504 { 00505 m_list->sort(); 00506 } 00507 00513 void inSort(int key,const T *d) 00514 { 00515 m_list->inSort(d); 00516 m_dict->insert(key,d); 00517 #if AUTORESIZE 00518 if (m_dict->size()>SDict_primes[m_sizeIndex]) 00519 { 00520 m_dict->resize(SDict_primes[++m_sizeIndex]); 00521 } 00522 #endif 00523 } 00524 00526 void setAutoDelete(bool val) 00527 { 00528 m_list->setAutoDelete(val); 00529 } 00530 00536 T *find(int key) 00537 { 00538 return m_dict->find(key); 00539 } 00540 00542 T *operator[](int key) const 00543 { 00544 return m_dict->find(key); 00545 } 00546 00548 T *at(uint i) 00549 { 00550 return m_list->at(i); 00551 } 00552 00557 virtual int compareItems(GCI item1,GCI item2) 00558 { 00559 return item1!=item2; 00560 } 00561 00566 void clear() 00567 { 00568 m_list->clear(); 00569 m_dict->clear(); 00570 } 00571 00574 int count() 00575 { 00576 return m_list->count(); 00577 } 00578 00579 class Iterator; // first forward declare 00580 friend class Iterator; // then make it a friend 00584 class Iterator 00585 { 00586 public: 00588 Iterator(const SIntDict<T> &dict) 00589 { 00590 m_li = new QListIterator<T>(*dict.m_list); 00591 } 00592 00594 virtual ~Iterator() 00595 { 00596 delete m_li; 00597 } 00598 00602 T *toFirst() const 00603 { 00604 return m_li->toFirst(); 00605 } 00606 00610 T *toLast() const 00611 { 00612 return m_li->toLast(); 00613 } 00614 00616 T *current() const 00617 { 00618 return m_li->current(); 00619 } 00620 00625 T *operator++() 00626 { 00627 return m_li->operator++(); 00628 } 00629 00634 T *operator--() 00635 { 00636 return m_li->operator--(); 00637 } 00638 00639 private: 00640 QListIterator<T> *m_li; 00641 }; 00642 00643 }; 00644 00645 #endif