From ca8956366ce7a3bb769e4375af0c01ffaa0fd0e0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 27 Feb 2015 07:26:51 +0000 Subject: [PATCH] removed MPOOL from some template classes --- qse/include/qse/cmn/HashList.hpp | 45 +++++++- qse/include/qse/cmn/HashTable.hpp | 167 ++++++++++++----------------- qse/include/qse/cmn/LinkedList.hpp | 34 +++--- 3 files changed, 128 insertions(+), 118 deletions(-) diff --git a/qse/include/qse/cmn/HashList.hpp b/qse/include/qse/cmn/HashList.hpp index 6e5d406a..35cf704d 100644 --- a/qse/include/qse/cmn/HashList.hpp +++ b/qse/include/qse/cmn/HashList.hpp @@ -76,15 +76,15 @@ struct HashListResizer /// For a hash value of hc, this->nodes[hc * 2] points to the first node and /// this->nodes[hc * 2 + 1] ponits to the last node. -template , typename COMPARATOR = HashListComparator, typename RESIZER = HashListResizer > +template , typename COMPARATOR = HashListComparator, typename RESIZER = HashListResizer > class HashList: public Mmged { public: - typedef LinkedList DatumList; + typedef LinkedList DatumList; typedef typename DatumList::Node Node; typedef typename DatumList::Iterator Iterator; typedef typename DatumList::Visiter Visiter; - typedef HashList SelfType; + typedef HashList SelfType; typedef HashListHasher DefaultHasher; typedef HashListComparator DefaultComparator; @@ -171,7 +171,7 @@ public: // placement new this->datum_list = new(list.getMmgr()) - DatumList (list.getMmgr(), list.datum_list->getMPBlockSize()); + DatumList (list.getMmgr(), list.getMpool().getBlockSize()); } catch (...) { @@ -223,6 +223,11 @@ public: return this->datum_list->getMpool (); } + const Mpool& getMpool() const + { + return this->datum_list->getMpool (); + } + SelfType& operator= (const SelfType& list) { this->clear (); @@ -395,7 +400,23 @@ public: } template - Node* heterofindValue(const MT& datum) + const Node* heterofindNode (const MT& datum) const + { + MHASHER hash; + return this->heterofind_node (datum, hash(datum) % this->node_capacity); + } + + template + T* heterofindValue(const MT& datum) + { + MHASHER hash; + Node* b = this->heterofind_node (datum, hash(datum) % this->node_capacity); + if (!b) return QSE_NULL; + return &b->value; + } + + template + const T* heterofindValue(const MT& datum) const { MHASHER hash; Node* b = this->heterofind_node (datum, hash(datum) % this->node_capacity); @@ -419,6 +440,20 @@ public: return this->find_node (datum); } + template + Node* heterosearch (const MT& datum) + { + MHASHER hash; + return this->heterofind_node (datum, hash(datum) % this->node_capacity); + } + + template + const Node* heterosearch (const MT& datum) const + { + MHASHER hash; + return this->heterofind_node (datum, hash(datum) % this->node_capacity); + } + Node* insert (const T& datum) { return this->insert_value (datum, false); diff --git a/qse/include/qse/cmn/HashTable.hpp b/qse/include/qse/cmn/HashTable.hpp index b70acd61..4b5013a2 100644 --- a/qse/include/qse/cmn/HashTable.hpp +++ b/qse/include/qse/cmn/HashTable.hpp @@ -72,12 +72,12 @@ struct HashTableResizer typedef HashListResizer HashTableResizer; -template , typename COMPARATOR = HashTableComparator, typename RESIZER = HashTableResizer> +template , typename COMPARATOR = HashTableComparator, typename RESIZER = HashTableResizer> class HashTable: public Mmged { public: typedef Couple Pair; - typedef HashTable SelfType; + typedef HashTable SelfType; typedef HashTableHasher DefaultHasher; typedef HashTableComparator DefaultComparator; @@ -85,7 +85,7 @@ public: struct PairHasher { - qse_size_t operator() (const Pair& p) + qse_size_t operator() (const Pair& p) const { HASHER hasher; return hasher (p.key); @@ -94,7 +94,7 @@ public: struct PairComparator { - qse_size_t operator() (const Pair& p1, const Pair& p2) + qse_size_t operator() (const Pair& p1, const Pair& p2) const { COMPARATOR comparator; return comparator (p1.key, p2.key); @@ -103,15 +103,26 @@ public: struct PairHeteroComparator { - qse_size_t operator() (const K& p1, const Pair& p2) + qse_size_t operator() (const K& p1, const Pair& p2) const { COMPARATOR is_equal; return is_equal (p1, p2.key); } }; - typedef HashList PairList; + template + struct MHeteroComparator + { + qse_size_t operator() (const MK& p1, const Pair& p2) const + { + MCOMPARATOR is_equal; + return is_equal (p1, p2.key); + } + }; + + typedef HashList PairList; typedef typename PairList::Node PairNode; + typedef typename PairList::Iterator Iterator; enum { @@ -140,6 +151,16 @@ public: return *this; } + Mpool& getMpool () + { + return this->datum_list->getMpool(); + } + + const Mpool& getMpool () const + { + return this->datum_list->getMpool(); + } + Pair* insert (const K& key, const V& value) { PairNode* node = this->pair_list.insert (Pair(key, value)); @@ -156,15 +177,22 @@ public: Pair* update (const K& key, const V& value) { - PairNode* node = this->pair_list.update (Pair(key, value)); + + //PairNode* node = this->pair_list.update (Pair(key, value)); + //if (!node) return QSE_NULL; + //return &node->value; + + PairNode* node = this->pair_list.template heterofindNode (key); if (!node) return QSE_NULL; - return &node->value; + Pair& pair = node->value; + pair.value = value; + return &pair; } Pair* search (const K& key) { //PairNode* node = this->pair_list.update (Pair(key)); - PairNode* node = this->pair_list.template heterofindNode (key); + PairNode* node = this->pair_list.template heterofindNode (key); if (!node) return QSE_NULL; return &node->value; } @@ -172,7 +200,32 @@ public: int remove (const K& key) { //return this->pair_list.remove (Pair(key)); - return this->pair_list.template heteroremove (key); + return this->pair_list.template heteroremove (key); + } + + template + Pair* heterosearch (const MK& key) + { + typedef MHeteroComparator MComparator; + PairNode* node = this->pair_list.template heterosearch (key); + if (!node) return QSE_NULL; + return &node->value; + } + + template + const Pair* heterosearch (const MK& key) const + { + typedef MHeteroComparator MComparator; + PairNode* node = this->pair_list.template heterosearch (key); + if (!node) return QSE_NULL; + return &node->value; + } + + template + int heteroremove (const MK& key) + { + typedef MHeteroComparator MComparator; + return this->pair_list.template heteroremove (key); } void clear () @@ -186,6 +239,12 @@ public: return this->pair_list.getSize (); } + + Iterator getIterator (qse_size_t index = 0) + { + return this->pair_list.getIterator (index); + } + protected: PairList pair_list; }; @@ -213,90 +272,6 @@ public: MIN_LOAD_FACTOR = 20 }; -#if 0 - class Iterator - { - public: - Iterator (): bucket_index(0), bucket_node(QSE_NULL) {} - Iterator (qse_size_t index, BucketNode* node): bucket_index(index), bucket_node(node) {} - Iterator (const Iterator& it): bucket_index (it.bucket_index), bucket_node(it.bucket_node) {} - - Iterator& operator= (const Iterator& it) - { - this->bucket_index = it.bucket_index; - this->bucket_node = it.bucket_node; - return *this; - } - - Iterator& operator++ () // prefix increment - { - QSE_ASSERT (this->isLegit()); - this->bucket_node = this->bucket_node->getNext(); - if (!this->bucket_node) - { - while (this->bucket_index - } - return *this; - } - - Iterator operator++ (int) // postfix increment - { - QSE_ASSERT (this->isLegit()); - Iterator saved (*this); - this->current = this->current->getNext(); //++(*this); - return saved; - } - - Iterator& operator-- () // prefix decrement - { - QSE_ASSERT (this->isLegit()); - this->current = this->current->getPrev(); - return *this; - } - - Iterator operator-- (int) // postfix decrement - { - QSE_ASSERT (this->isLegit()); - Iterator saved (*this); - this->current = this->current->getPrev(); //--(*this); - return saved; - } - - bool operator== (const Iterator& it) const - { - return this->bucket_index == it.bucket_index && - this->bucket_node == it.bucket_node; - } - - bool operator!= (const Iterator& it) const - { - return this->bucket_index != it.bucket_index || - this->bucket_node != it.bucket_node; - } - - bool isLegit () const - { - // TODO: change this - return this->bucket_node != QSE_NULL; - } - - T& operator* () // dereference - { - return this->bucket_node->getValue(); - } - - const T& operator* () const // dereference - { - return this->bucket_node->getValue(); - } - - protected: - SelfType* table; - qse_size_t bucket_index; - BucketNode* bucket_node; - }; -#endif - protected: Bucket** allocate_bucket (Mmgr* mm, qse_size_t bs, qse_size_t mpb_size) const { @@ -816,10 +791,6 @@ public: return 0; } - //Iterator getIterator () - //{ - //} - protected: mutable qse_size_t pair_count; mutable qse_size_t bucket_size; diff --git a/qse/include/qse/cmn/LinkedList.hpp b/qse/include/qse/cmn/LinkedList.hpp index c1bf98dd..2aa242ec 100644 --- a/qse/include/qse/cmn/LinkedList.hpp +++ b/qse/include/qse/cmn/LinkedList.hpp @@ -34,14 +34,14 @@ QSE_BEGIN_NAMESPACE(QSE) ///////////////////////////////// -template class LinkedList; +template class LinkedList; -template +template class LinkedListNode { public: - friend class LinkedList; - typedef LinkedListNode SelfType; + friend class LinkedList; + typedef LinkedListNode SelfType; T value; // you can use this variable or accessor functions below @@ -79,12 +79,12 @@ protected: } }; -template +template class LinkedListIterator { public: - friend class LinkedList; - typedef LinkedListNode Node; - typedef LinkedListIterator SelfType; + friend class LinkedList; + typedef LinkedListNode Node; + typedef LinkedListIterator SelfType; LinkedListIterator (): current(QSE_NULL) {} LinkedListIterator (Node* node): current(node) {} @@ -199,16 +199,15 @@ struct LinkedListComparator }; /// -/// The LinkedList class provides a template for a doubly-linked list. +/// The LinkedList class provides a template for a doubly-linked list. /// -template > class LinkedList: public Mmged +template > class LinkedList: public Mmged { public: - typedef LinkedList SelfType; - typedef LinkedListNode Node; - typedef LinkedListIterator Iterator; + typedef LinkedList SelfType; + typedef LinkedListNode Node; + typedef LinkedListIterator Iterator; - typedef Mpool DefaultMpool; typedef LinkedListComparator DefaultComparator; struct Visiter @@ -276,6 +275,11 @@ public: return this->mp; } + const Mpool& getMpool () const + { + return this->mp; + } + qse_size_t getSize () const { return this->node_count; @@ -696,7 +700,7 @@ public: } protected: - MPOOL mp; + Mpool mp; COMPARATOR comparator; Node* head_node; Node* tail_node;