00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if !defined(_OBJECTTABLE_H_)
00028 #define _OBJECTTABLE_H_
00029
00030 #if _MSC_VER > 1000
00031 #pragma once
00032 #endif // _MSC_VER > 1000
00033
00034 #include "Object.h"
00035
00036 namespace cmlabs {
00037
00038 class CollectionEntry;
00039
00040 struct HashEntry {
00041 unsigned long hash;
00042 CollectionEntry *entry;
00043 struct HashEntry *next;
00044 };
00045
00046 struct HashTable {
00047 unsigned int length;
00048 unsigned int count;
00049 unsigned int limit;
00050 unsigned int prime;
00051 struct HashEntry **table;
00052 };
00053
00054 static inline unsigned int hash2index(unsigned int length, unsigned long hash) {
00055 return (hash%length);
00056 };
00057
00058 struct HashTable *create_hashtable(unsigned int minsize);
00059 unsigned long hash(unsigned long hashkey);
00060 static int hashtable_expand(struct HashTable *h);
00061 unsigned int hashtable_count(const struct HashTable *h);
00062 bool hashtable_insert_key(struct HashTable *h, CollectionEntry* entry);
00063 bool hashtable_insert_value(struct HashTable *h, CollectionEntry* entry);
00064 CollectionEntry* hashtable_search_key(const struct HashTable *h, const Object* key);
00065 CollectionEntry* hashtable_search_value(const struct HashTable *h, const Object* value);
00066 ObjectCollection* hashtable_search_values(const struct HashTable *h, const Object* value);
00067 bool hashtable_remove_key(struct HashTable *h, const CollectionEntry* entry);
00068 bool hashtable_remove_value(struct HashTable *h, const CollectionEntry* entry);
00069 void hashtable_destroy(struct HashTable *h);
00070
00071 class XMLNode;
00072
00073 class CollectionEntry : public Object
00074 {
00075 public:
00076 CollectionEntry() {
00077 key = value = NULL;
00078 next = prev = NULL;
00079 keyhash = valuehash = 0;
00080 }
00081 CollectionEntry(Object* key, Object* value) {
00082 this->key = key;
00083 this->value = value;
00084 next = prev = NULL;
00085 keyhash = valuehash = 0;
00086 }
00087 Object* clone() const {return new CollectionEntry(key, value);}
00088 Object* key;
00089 Object* value;
00090 CollectionEntry* next;
00091 CollectionEntry* prev;
00092 unsigned long keyhash;
00093 unsigned long valuehash;
00094 };
00095
00096 class ObjectTable;
00097 class SortedEntry : public Object {
00098 public:
00099 SortedEntry(double val, Object* obj, ObjectTable* table) {
00100 this->value = val;
00101 this->object = obj;
00102 this->table = table;
00103 }
00104 bool equals(const Object* o2) const {
00105 if ((o2 == NULL) || (!isSameClass(o2))) return false;
00106 return (this->value == ((SortedEntry*)o2)->value);
00107 }
00108 int compare(const Object* o2) const {
00109 if ((o2 == NULL) || (!isSameClass(o2))) return 0;
00110 if (value == ((SortedEntry*)o2)->value) return 0;
00111 return (value > ((SortedEntry*)o2)->value ? 1 : -1);
00112 }
00113 unsigned long getHash() const {
00114 unsigned long key = (unsigned long) (value*1000);
00115 return (key >> 3) * 2654435761u;
00116 }
00117 Object* clone() const {return NULL;}
00118 double value;
00119 Object* object;
00120 ObjectTable* table;
00121 };
00122
00123
00124 #define SORTNONE 0
00125 #define SORTBYKEY 1
00126 #define SORTBYVALUE 2
00127 #define SORTREVERSE 4
00128 #define FINDREVERSE 8
00129
00130 class Collection;
00131 class ObjectCollection;
00132 class Dictionary;
00133 class ObjectDictionary;
00134
00135 class JMutex;
00136 class ObjectTable : public Object
00137 {
00138 public:
00139 ObjectTable();
00140 ObjectTable(const ObjectTable &Coll);
00141 ObjectTable(const JString& xml);
00142 ObjectTable(XMLNode* node);
00143 virtual ~ObjectTable();
00144
00145 JString type;
00146 bool deleteOnDestroy;
00147 int count;
00148 int currentPos;
00149 CollectionEntry* current;
00150 CollectionEntry* first;
00151 CollectionEntry* last;
00152 int sorting;
00153
00154 Object* clone() const;
00155 bool isCollection();
00156 bool unitTest();
00157
00158 virtual unsigned long getPayloadSize() const;
00159
00160 bool addAll(const ObjectTable* c);
00161 bool copyAll(const ObjectTable* c);
00162 bool takeAll(ObjectTable* c);
00163 bool addAll(const ObjectTable& c);
00164 bool copyAll(const ObjectTable& c);
00165 bool takeAll(ObjectTable& c);
00166 bool addAll(const CollectionEntry* f, bool copy = false);
00167
00168 const ObjectTable& operator =(const ObjectTable& c);
00169 bool equals(const Object* o2) const;
00170 bool operator ==(ObjectTable& c);
00171 bool operator !=(ObjectTable& c);
00172
00173 int getCount() const;
00174 int getPos(const Object* obj) const;
00175 int getPosKey(const Object* key) const;
00176
00177 bool containsKey(const Object* key) const;
00178 bool contains(const Object* value) const;
00179
00180 bool removeFirst();
00181 bool removeLast();
00182 bool remove(int pos);
00183 bool removeAll();
00184 bool removeAllNoDelete();
00185 bool removeNoDelete(int pos);
00186 bool removeNoDelete(const Object* key);
00187 int removeNoDelete(const Object* value, bool removeAll);
00188
00189 ObjectCollection* getAllKeysCopy();
00190
00191 JString toHTML() ;
00192 JString toXML() ;
00193 bool fromXML(const JString& xml);
00194 bool fromXML(XMLNode* node);
00195
00196 JString print() const;
00197 JString printListLine(const JString& separator, const JString& equalsign = " = ", const JString& quotes = "") const;
00198
00199 bool sortKeys();
00200 bool sortValues();
00201 bool sortKeysReverse();
00202 bool sortValuesReverse();
00203 bool sortRandomMix();
00204
00205 void noDelete();
00206 void doDelete();
00207
00208
00209
00210 int addAndReturnPosition(Object* key, Object* value);
00211 ObjectCollection* getAllBetweenKeys(const Object* key1, const Object* key2);
00212 ObjectCollection* getAllBetween(const Object* value1, const Object* value2);
00213 Object* getFirstAfterKey(const Object* key);
00214 Object* getFirstAfter(const Object* value);
00215 Object* getLastBeforeKey(const Object* key);
00216 Object* getLastBefore(const Object* value);
00217 int removeAllBetweenKeys(const Object* key1, const Object* key2);
00218 int removeAllBetween(const Object* value1, const Object* value2);
00219
00220
00221
00222 ObjectCollection* getAllEntriesBetweenKeys(const Object* key1, const Object* key2);
00223 ObjectCollection* getAllEntriesBetween(const Object* value1, const Object* value2);
00224 CollectionEntry* getFirstEntryAfterKey(const Object* key);
00225 CollectionEntry* getFirstEntryAfter(const Object* value);
00226 CollectionEntry* getLastEntryBeforeKey(const Object* key);
00227 CollectionEntry* getLastEntryBefore(const Object* value);
00228 int removeAllEntriesBetweenKeys(const Object* key1, const Object* key2);
00229 int removeAllEntriesBetween(const Object* value1, const Object* value2);
00230 int removeAllBeforeKey(const Object* key);
00231 int removeAllBefore(const Object* value);
00232 int removeAllAfterKey(const Object* key);
00233 int removeAllAfter(const Object* value);
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 long getBinarySize(int chunk);
00244
00245 int getBinaryChunkCount();
00246
00247 long toBinaryBuffer(int chunk, char* buffer, int maxlen);
00248
00249 bool fromBinaryBuffer(int chunk, char* buffer, long len);
00250
00251
00252
00253
00254 bool add(Object* key, Object* value);
00255 bool addLast(Object* key, Object* value);
00256 bool addFirst(Object* key, Object* value);
00257 bool addAfter(const Object* obj, Object* key, Object* value);
00258 bool addBefore(const Object* obj, Object* key, Object* value);
00259 bool addBefore(int pos, Object* key, Object* value);
00260 bool addAfter(int pos, Object* key, Object* value);
00261 bool addBeforeEntry(CollectionEntry* entry, Object* key, Object* value);
00262 bool addAfterEntry(CollectionEntry* entry, Object* key, Object* value);
00263 bool addBetweenEntries(CollectionEntry* prevItem, CollectionEntry* nextItem, Object* key, Object* value);
00264
00265
00266 bool replace(const Object* obj, Object* key, Object* value);
00267 bool replace(int pos, Object* key, Object* value);
00268 bool replaceEntry(CollectionEntry* entry, Object* key, Object* value);
00269
00270 bool remove(const Object* key);
00271 int remove(const Object* value, bool removeAll);
00272 bool removeEntry(CollectionEntry* entry);
00273
00274 Object* get(const Object* key) const;
00275 Object* get(int pos) const;
00276 Object* get(int pos);
00277 Object* getNext();
00278 Object* getPrevious();
00279 Object* getFirst();
00280 Object* getLast();
00281 Object* take(const Object* key);
00282 Object* take(int pos);
00283
00284 Object* getKey(int pos) const;
00285 Object* getKey(int pos);
00286 Object* getFirstKey();
00287 Object* getLastKey();
00288 Object* getNextKey();
00289 Object* getPreviousKey();
00290
00291 CollectionEntry* getEntryKey(const Object* key) const;
00292 CollectionEntry* getEntryValue(const Object* key) const;
00293 CollectionEntry* getEntry(int pos) const;
00294 CollectionEntry* getEntry(int pos);
00295
00296 CollectionEntry* getNextEntry();
00297 CollectionEntry* getPreviousEntry();
00298 CollectionEntry* getFirstEntry();
00299 CollectionEntry* getLastEntry();
00300 int getEntryPos(CollectionEntry* entry) const;
00301
00302 CollectionEntry** findClosestKeys(const Object* key) const;
00303 CollectionEntry** findClosestValues(const Object* value) const;
00304
00305
00306 JString print(const Object* key) const;
00307 JString printEntry(const CollectionEntry* item, const JString& separator = " = ", const JString& quotes = "") const;
00308
00309 bool isKeyBigger(CollectionEntry* entry1, CollectionEntry* entry2);
00310 bool isValueBigger(CollectionEntry* entry1, CollectionEntry* entry2);
00311
00312 CollectionEntry* createEntry(Object* key, Object* value);
00313 JMutex* accessMutex;
00314 HashTable* hashTable;
00315
00316 };
00317
00318 }
00319
00320 #endif // !defined(_OBJECTTABLE_H_)