123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- #pragma once
- #include "CharMap.h"
- namespace Framework
- {
- template<class T, class M> struct TrieIteratorData
- {
- M* map;
- TrieIteratorData* parent;
- TrieIteratorData()
- {
- map = 0;
- parent = 0;
- }
- TrieIteratorData(M* zMap, TrieIteratorData parent)
- {
- map = zMap;
- this->parent = new TrieIteratorData(parent);
- }
- TrieIteratorData(const TrieIteratorData& data)
- {
- map = data.map;
- if (data.parent)
- parent = new TrieIteratorData(*data.parent);
- else
- parent = 0;
- }
- ~TrieIteratorData()
- {
- delete parent;
- }
- TrieIteratorData& operator=(const TrieIteratorData& data)
- {
- map = data.map;
- TrieIteratorData* tmp = parent;
- if (data.parent)
- parent = new TrieIteratorData(*data.parent);
- else
- parent = 0;
- delete tmp;
- return *this;
- }
- void set(M* zMap, TrieIteratorData parent)
- {
- map = zMap;
- parent = new TrieIteratorData(parent);
- }
- };
- template<class T, class M> class TrieIterator
- {
- private:
- TrieIteratorData<T, M> data;
- public:
- TrieIterator(TrieIteratorData<T, M> data)
- {
- this->data = data;
- }
- TrieIterator next()
- {
- for (unsigned char i = 0; true; i++)
- {
- if (data.map->z(i))
- return TrieIterator(
- TrieIteratorData<T, M>(data.map->z(i)->map, data));
- if (i == 255) break;
- }
- if (!data.parent || !data.parent->map)
- return TrieIterator(
- TrieIteratorData<T, M>(0, TrieIteratorData<T, M>()));
- TrieIteratorData<T, M> d = data;
- do
- {
- for (unsigned char i = 0; true; i++)
- {
- if (d.parent->map->z(i)
- && d.parent->map->z(i)->map == d.map)
- {
- if (i < 256)
- {
- for (unsigned char j = (unsigned char)(i + 1); true;
- j++)
- {
- if (d.parent->map->z(j))
- return TrieIterator(TrieIteratorData<T, M>(
- d.parent->map->z(j)->map, *d.parent));
- if (j == 255) break;
- }
- }
- d = *d.parent;
- break;
- }
- if (i == 255) break;
- }
- } while (d.parent && d.parent->map);
- return TrieIterator(TrieIteratorData<T, M>(0, TrieIteratorData<T, M>()));
- }
- TrieIterator& operator++() //! prefix
- {
- data = next().data;
- return *this;
- }
- TrieIterator operator++(int) //! postfix
- {
- TrieIterator temp(*this);
- data = next().data;
- return temp;
- }
- operator bool()
- {
- return data.map != 0;
- }
- operator T*()
- {
- return data.map->zValue();
- }
- T* operator->()
- {
- return data.map->zValue();
- }
- T* val()
- {
- return data.map->zValue();
- }
- };
- template<class T> class Trie : public virtual ReferenceCounter
- {
- private:
- CharMap<Trie<T>, T>* map;
- public:
- Trie()
- : ReferenceCounter()
- {
- map = new CharMap<Trie<T>, T>();
- }
- ~Trie()
- {
- map->release();
- }
- void set(const char* addr, int addrLen, T value)
- {
- if (!addrLen)
- map->setValue(value);
- else
- {
- if (!map->z(*addr)) map->set(*addr, new Trie<T>());
- map->z(*addr)->set(addr + 1, addrLen - 1, value);
- }
- }
- void remove(const char* addr, int addrLen)
- {
- if (!addrLen)
- map->setValue(0);
- else
- {
- if (!map->z(*addr)) return;
- map->z(*addr)->remove(addr + 1, addrLen - 1);
- }
- }
- T get(const char* addr, int addrLen) const
- {
- if (!addrLen)
- return map->getValue();
- else
- {
- if (!map->z(*addr)) return 0;
- return map->z(*addr)->get(addr + 1, addrLen - 1);
- }
- }
- void leeren()
- {
- map->release();
- map = new CharMap<Trie<T>, T>();
- }
- TrieIterator<T, CharMap<Trie<T>, T>> getIterator()
- {
- return TrieIterator<T, CharMap<Trie<T>, T>>(
- TrieIteratorData<T, CharMap<Trie<T>, T>>(
- map, TrieIteratorData<T, CharMap<Trie<T>, T>>()));
- }
- friend TrieIterator<T, CharMap<Trie<T>, T>>;
- };
- template<class T> class RCTrie : public virtual ReferenceCounter
- {
- private:
- RCCharMap<RCTrie<T>, T>* map;
- public:
- RCTrie()
- : ReferenceCounter()
- {
- map = new RCCharMap<RCTrie<T>, T>();
- }
- ~RCTrie()
- {
- map->release();
- }
- void set(const char* addr, int addrLen, T* value)
- {
- if (!addrLen)
- map->setValue(value);
- else
- {
- if (!map->z(*addr)) map->set(*addr, new RCTrie<T>());
- map->z(*addr)->set(addr + 1, addrLen - 1, value);
- }
- }
- void remove(const char* addr, int addrLen)
- {
- if (!addrLen)
- map->setValue(0);
- else
- {
- if (!map->z(*addr)) return;
- map->z(*addr)->remove(addr + 1, addrLen - 1);
- }
- }
- T* get(const char* addr, int addrLen)
- {
- if (!addrLen)
- return map->getValue();
- else
- {
- if (!map->z(*addr)) return 0;
- return map->z(*addr)->get(addr + 1, addrLen - 1);
- }
- }
- T* z(const char* addr, int addrLen) const
- {
- if (!addrLen)
- return map->zValue();
- else
- {
- if (!map->z(*addr)) return 0;
- return map->z(*addr)->z(addr + 1, addrLen - 1);
- }
- }
- void leeren()
- {
- map->release();
- map = new RCCharMap<RCTrie<T>, T>();
- }
- TrieIterator<T, RCCharMap<RCTrie<T>, T>> getIterator() const
- {
- return TrieIterator<T, RCCharMap<RCTrie<T>, T>>(
- TrieIteratorData<T, RCCharMap<RCTrie<T>, T>>(
- map, TrieIteratorData<T, RCCharMap<RCTrie<T>, T>>()));
- }
- friend TrieIterator<T, RCCharMap<RCTrie<T>, T>>;
- };
- } // namespace Framework
|