14 #ifndef ULCONTAINERS_H
15 #define ULCONTAINERS_H
23 template <
class K,
class V>
36 template <
class T,
class U>
43 ULPair(
const T& firstItem,
const U& secondItem);
49 bool operator<(const ULPair<T,U>& p)
const;
64 template <
class T,
class U>
72 template <
class T,
class U>
75 this->first = firstItem;
76 this->second = secondItem;
82 template <
class T,
class U>
91 template <
class T,
class U>
95 this->first = p.
first;
107 template <
class T,
class U>
110 return (this->first == p.
first && this->second == p.
second);
119 template <
class T,
class U>
122 return !(*
this == p);
134 template <
class T,
class U>
137 if (!(this->first == p.
first)) {
138 return this->first < p.
first;
141 return this->second < p.
second;
147 template <
class T,
class U>
156 template <
class T,
class U>
166 template <
class T,
class U>
176 template <
class T,
class U>
241 this->data = node.
data;
242 this->next = node.
next;
267 operator bool()
const;
329 this->current = it.current;
330 this->list = it.list;
346 return (this->current == 0);
357 return (this->list == 0 || this->current == this->list->frontNode);
369 if (this->list == 0 || this->current == 0) {
375 for (nodePtr = this->current; nodePtr != 0; nodePtr = nodePtr->
previous) {
400 return !(this->isAtEnd());
409 return this->current == it.current && this->list == it.list;
418 return !(*
this == it);
430 if (this->current != 0) {
431 this->current = this->current->next;
461 UL_ASSERT(this->list != 0);
463 if (this->current == this->list->frontNode) {
464 }
else if (this->current != 0) {
465 this->current = this->current->previous;
466 }
else if (this->list != 0) {
467 this->current = this->list->backNode;
518 UL_ASSERT(this->current != 0);
519 return this->current->data;
549 operator bool()
const;
611 this->current = it.current;
612 this->list = it.list;
628 return (this->current == 0);
639 return (this->list == 0 || this->current == this->list->frontNode);
651 if (this->list == 0 || this->current == 0) {
657 for (nodePtr = this->current; nodePtr != 0; nodePtr = nodePtr->
previous) {
682 return !(this->isAtEnd());
691 return this->current == it.current && this->list == it.list;
700 return !(*
this == it);
712 if (this->current != 0) {
713 this->current = this->current->next;
743 UL_ASSERT(this->list != 0);
745 if (this->current == this->list->frontNode) {
746 }
else if (this->current != 0) {
747 this->current = this->current->previous;
748 }
else if (this->list != 0) {
749 this->current = this->list->backNode;
802 UL_ASSERT(this->current != 0);
803 return this->current->data;
848 void erase(uluint32 n);
853 uluint32
size()
const;
858 void append(
const T& data);
860 void add(
const T& data);
862 void insert(
const T& data);
881 void sort(
int (*func)(T&, T&,
void *),
void *context);
882 void mergeSort(
int (*func)(T&, T&,
void *),
void *context);
898 this->listLength = 0;
909 this->listLength = 0;
933 while (current != 0) {
935 current = current->
next;
939 this->listLength = 0;
956 this->frontNode = this->backNode = 0;
957 this->listLength = 0;
958 for (current=list.frontNode; current != 0; current = current->
next) {
965 if (this->backNode == 0) {
966 this->frontNode = tmp;
968 this->backNode->
next = tmp;
970 this->backNode = tmp;
971 (this->listLength)++;
985 if (
this == &other) {
989 if (this->length() != other.
length()) {
995 while (current != 0 && otherCurrent != 0) {
996 if (!(current->
data == otherCurrent->
data)) {
999 current = current->
next;
1000 otherCurrent = otherCurrent->
next;
1003 return current == 0 && otherCurrent == 0;
1015 UL_ASSERT(n < this->listLength);
1017 for (uluint32 k=0; current != 0 && k < n; k++) {
1018 current = current->
next;
1021 UL_ASSERT(current != 0);
1022 return current->
data;
1034 UL_ASSERT(n < this->listLength);
1036 for (uluint32 k=0; current != 0 && k < n; k++) {
1037 current = current->
next;
1040 UL_ASSERT(current != 0);
1041 return current->
data;
1050 return this->listLength;
1059 return this->listLength;
1070 this->frontNode = this->backNode = 0;
1071 this->listLength = 0;
1100 return this->frontNode == 0;
1113 for (uluint32 k=0; current != 0 && k < n; k++) {
1114 current = current->
next;
1117 UL_ASSERT(current != 0);
1121 it.current = current;
1158 if (it.current == stop.current) {
1168 while (!it.
isAtEnd() && it.current != stop.current) {
1190 newNode->
data = data;
1191 newNode->
next = this->frontNode;
1193 if (this->frontNode == 0) {
1194 this->backNode = newNode;
1196 this->frontNode->
previous = newNode;
1198 this->frontNode = newNode;
1199 (this->listLength)++;
1215 newNode->
data = data;
1217 newNode->
previous = this->backNode;
1219 if (this->backNode == 0) {
1220 this->frontNode = newNode;
1222 this->backNode->
next = newNode;
1225 this->backNode = newNode;
1226 (this->listLength)++;
1237 this->push_back(data);
1248 this->push_front(data);
1285 this->insertNode(it, node);
1301 while (!it.
isAtEnd() && *it < data) {
1305 if (it.
isAtEnd() || !(*it == data)) {
1330 UL_ASSERT(!empty());
1331 return this->backNode->data;
1342 it.current = this->frontNode;
1354 it.current = this->frontNode;
1397 it.current = this->backNode;
1410 it.current = this->backNode;
1430 UL_ASSERT(it.list ==
this);
1431 if (it.current != 0 && it.list ==
this) {
1432 if (it.current == this->frontNode) {
1433 this->frontNode = this->frontNode->next;
1436 if (it.current == this->backNode) {
1437 this->backNode = this->backNode->previous;
1440 if (it.current->next != 0) {
1441 it.current->next->previous = it.current->previous;
1444 if (it.current->previous != 0) {
1445 it.current->previous->next = it.current->next;
1450 it.current = it.current->next;
1453 (this->listLength)--;
1466 return removeNode(it);
1481 UL_ASSERT(node != 0);
1482 UL_ASSERT(it.list ==
this);
1483 if (node == 0 || it.list !=
this) {
1487 node->
next = it.current;
1488 if (it.current == 0) {
1490 if (this->backNode != 0) {
1491 this->backNode->
next = node;
1493 this->backNode = node;
1495 node->
previous = it.current->previous;
1496 if (it.current->previous != 0) {
1497 it.current->previous->next = node;
1500 it.current->previous = node;
1503 if (it.current == this->frontNode) {
1504 this->frontNode = node;
1522 if (this->listLength == 0) {
1523 this->frontNode = tail.frontNode;
1525 this->backNode->next = tail.frontNode;
1528 if (tail.frontNode != 0) {
1529 tail.frontNode->previous = this->backNode;
1532 this->listLength += tail.listLength;
1533 if (tail.listLength != 0) {
1534 this->backNode = tail.backNode;
1538 tail.listLength = 0;
1562 if (this->frontNode == this->backNode) {
1575 func(cur->
data, pivot->
data, context) <= 0
1584 lteqList.
sort(func, context);
1585 gtList.
sort(func, context);
1590 while (!lteqList.
empty()) {
1593 this->insertNode(it, node);
1598 this->insertNode(it, pivot);
1601 while (!gtList.
empty()) {
1604 this->insertNode(it, node);
1628 if (this->frontNode == this->backNode) {
1635 for (
bool odd =
false; !this->empty(); odd = !odd) {
1637 ULList<T>& list = odd ? oddList : evenList;
1647 while (!evenList.
empty() && !oddList.
empty()) {
1649 func(evenList.frontNode->data, oddList.frontNode->data, context) <= 0
1657 while (!evenList.
empty()) {
1661 while (!oddList.
empty()) {
1676 template <
class K,
class V>
1691 operator bool()
const;
1705 template <
class K,
class V>
1707 : hashTable(0), hashIndex(0)
1714 template <
class K,
class V>
1716 : hashTable(0), hashIndex(0)
1724 template <
class K,
class V>
1732 template <
class K,
class V>
1736 this->hashTable = it.hashTable;
1737 this->hashIndex = it.hashIndex;
1738 this->listIterator = it.listIterator;
1750 template <
class K,
class V>
1753 UL_ASSERT(!isAtEnd());
1754 return *(this->listIterator);
1762 template <
class K,
class V>
1765 if (this->hashTable == 0) {
1769 ++(this->listIterator);
1770 if (this->listIterator.isAtEnd()) {
1772 this->listIterator = this->hashTable->table[this->hashIndex].cbegin();
1773 while (this->hashIndex < this->hashTable->tableSize && this->listIterator.isAtEnd()) {
1775 this->listIterator = this->hashTable->table[this->hashIndex].cbegin();
1785 template <
class K,
class V>
1798 template <
class K,
class V>
1801 return !(this->isAtEnd());
1811 template <
class K,
class V>
1814 return this->hashTable == 0 || this->hashIndex >= this->hashTable->tableSize;
1820 template <
class K,
class V>
1823 return this->hashIndex;
1838 template <
class K,
class V>
1854 bool resize(uluint32 tableSize);
1856 void add(
const K& key,
const V& value);
1859 bool find(
const K& key, V& value)
const;
1860 bool find(
const K& key)
const;
1865 void getStats(
double& avgListLength, uluint32& maxListLength);
1878 template <
class K,
class V>
1880 : table(0), tableSize(0), keyCount(0)
1888 template <
class K,
class V>
1892 this->tableSize = 0;
1894 if (initialTableSize > 0) {
1896 if (this->table != 0) {
1897 this->tableSize = initialTableSize;
1905 template <
class K,
class V>
1908 delete [] this->table;
1915 template <
class K,
class V>
1918 if (this->table != 0) {
1919 for (uluint32 k=0; k < this->tableSize; k++) {
1920 this->table[k].clear();
1931 template <
class K,
class V>
1934 delete [] this->table;
1936 this->tableSize = 0;
1938 if (otherTable.tableSize > 0) {
1942 if (this->table != 0) {
1943 this->tableSize = otherTable.tableSize;
1944 for (uluint32 k=0; k < this->tableSize; k++) {
1945 this->table[k] = otherTable.table[k];
1947 this->keyCount = otherTable.keyCount;
1957 template <
class K,
class V>
1960 return this->tableSize;
1966 template <
class K,
class V>
1969 return this->keyCount;
1980 template <
class K,
class V>
1983 if (tableSize == 0 || tableSize == this->tableSize) {
1988 if (newTable == 0) {
1992 if (this->tableSize != 0) {
1996 for (uluint32 k=0; k < this->tableSize; k++) {
1997 for (it = this->table[k].cbegin(); it; ++it) {
1998 hashValue = (*it).getFirst().hash(tableSize);
1999 UL_ASSERT(hashValue < tableSize);
2006 delete [] this->table;
2007 this->table = newTable;
2008 this->tableSize = tableSize;
2018 template <
class K,
class V>
2024 this->add(keyValuePair);
2034 template <
class K,
class V>
2037 uluint32 hashValue = keyValuePair.
getFirst().hash(this->tableSize);
2038 UL_ASSERT(hashValue < this->tableSize);
2039 if (hashValue < this->tableSize) {
2041 for (it = this->table[hashValue].begin(); it; ++it) {
2042 if (keyValuePair.
getFirst() == (*it).getFirst()) {
2043 (*it).setSecond(keyValuePair.
getSecond());
2048 this->table[hashValue].push_back(keyValuePair);
2060 template <
class K,
class V>
2063 uluint32 hashValue = key.hash(this->tableSize);
2064 UL_ASSERT(hashValue < this->tableSize);
2065 if (hashValue < this->tableSize) {
2067 for (it = this->table[hashValue].cbegin(); it; ++it) {
2068 if (key == (*it).getFirst()) {
2069 value = (*it).getSecond();
2081 template <
class K,
class V>
2084 uluint32 hashValue = key.hash(this->tableSize);
2085 UL_ASSERT(hashValue < this->tableSize);
2087 for (it = this->table[hashValue].cbegin(); it; ++it) {
2088 if (key == (*it).getFirst()) {
2103 template <
class K,
class V>
2107 it.hashTable =
this;
2108 it.hashIndex = this->tableSize;
2116 template <
class K,
class V>
2120 it.hashTable =
this;
2122 it.listIterator = this->table[it.hashIndex].cbegin();
2123 while (it.hashIndex < this->tableSize && it.listIterator.
isAtEnd()) {
2125 it.listIterator = this->table[it.hashIndex].cbegin();
2140 template <
class K,
class V>
2144 uluint32 chainLengthTotal = 0;
2145 uluint32 nNonemptyChains = 0;
2146 for (uluint32 k=0; k < this->tableSize; k++) {
2147 uluint32 chainLength = this->table[k].size();
2148 chainLengthTotal += chainLength;
2149 if (chainLength > maxChainLength) {
2150 maxChainLength = chainLength;
2152 if (chainLength > 0) {
2157 if (nNonemptyChains > 0) {
2158 avgChainLength = double(chainLengthTotal) / double(nNonemptyChains);
2189 uluint32
size()
const;
2190 void resize(uluint32 newSize);
2191 void append(
const T& item);
2195 uluint32 vectorSize;
2205 this->vectorSize = 0;
2216 this->vectorSize = 0;
2227 this->vectorSize = 0;
2247 if (this->vector != 0) {
2248 for (uluint32 k=0; k < this->vectorSize; k++) {
2249 delete this->vector[k];
2251 delete [] this->vector;
2255 this->vectorSize = 0;
2271 this->resize(v.
size());
2272 if(this->vector != 0) {
2273 for (uluint32 k=0; k < this->vectorSize; k++) {
2288 UL_ASSERT(n < (1 << 30));
2289 if (n >= this->vectorSize) {
2290 uluint32 newSize = (this->vectorSize == 0 ? 16 : 2 * this->vectorSize);
2291 while (newSize <= n) {
2294 UL_ASSERT(newSize <= (1 << 30));
2296 this->resize(newSize);
2299 UL_ASSERT(n < this->vectorSize);
2300 if (this->vector[n] == 0) {
2301 this->vector[n] =
new T;
2304 return *(this->vector[n]);
2313 return this->vectorSize;
2327 if (newSize != this->vectorSize) {
2332 T **newVector =
new TPtr[newSize];
2333 if (newVector != 0) {
2335 for (k=0; k < newSize && k < this->vectorSize; k++) {
2336 newVector[k] = this->vector[k];
2337 this->vector[k] = 0;
2340 for ( ; k < newSize; k++) {
2345 this->vector = newVector;
2346 this->vectorSize = newSize;