| BPatch_Set.h | | BPatch_Set.h | |
| | | | |
| skipping to change at line 44 | | skipping to change at line 44 | |
| #if defined(external_templates) | | #if defined(external_templates) | |
| #pragma interface | | #pragma interface | |
| #endif | | #endif | |
| | | | |
| /*******************************************************/ | | /*******************************************************/ | |
| /* header files */ | | /* header files */ | |
| /*******************************************************/ | | /*******************************************************/ | |
| | | | |
| #include <assert.h> | | #include <assert.h> | |
| #include <stdlib.h> | | #include <stdlib.h> | |
|
| | | #include <set> | |
| #include "BPatch_dll.h" | | #include "BPatch_dll.h" | |
| #include "BPatch_Vector.h" | | #include "BPatch_Vector.h" | |
| #include <iostream> | | #include <iostream> | |
|
| | | #include <algorithm> | |
| | | #include <iterator> | |
| | | #include <functional> | |
| #if !defined(DO_INLINE_P) | | #if !defined(DO_INLINE_P) | |
| #define DO_INLINE_P | | #define DO_INLINE_P | |
| #endif | | #endif | |
| | | | |
| #if !defined(DO_INLINE_F) | | #if !defined(DO_INLINE_F) | |
| #define DO_INLINE_F | | #define DO_INLINE_F | |
| #endif | | #endif | |
| | | | |
| /** template struct that will be used for default compare | | /** template struct that will be used for default compare | |
| * class for BPatch_Set operations. | | * class for BPatch_Set operations. | |
| */ | | */ | |
| | | | |
|
| // VG: shouldn't this be a dll export? | | | |
| template<class T> | | | |
| struct comparison { | | | |
| /** default comparison operator | | | |
| * returns -1 if the first arg < second arg | | | |
| * returns 1 if the first arg > second arg | | | |
| * returns 0 if the first arg == second arg | | | |
| * @param x first argument to be compared | | | |
| * @param y second argument to be compared | | | |
| */ | | | |
| int operator()(const T& x, const T& y) const { | | | |
| if(x<y) return -1; | | | |
| if(x>y) return 1; | | | |
| return 0; | | | |
| } | | | |
| }; | | | |
| | | | |
| /** template class for BPatch_Set. The implementation is based on red black | | /** template class for BPatch_Set. The implementation is based on red black | |
| * tree implementation for efficiency concerns and for getting sorted | | * tree implementation for efficiency concerns and for getting sorted | |
| * elements easier. The template depends on two types. The first one is th
e | | * elements easier. The template depends on two types. The first one is th
e | |
| * the type of the elements in the BPatch_Set and the second one is the te
mplate | | * the type of the elements in the BPatch_Set and the second one is the te
mplate | |
| * structure that is used to compare the elements of the BPatch_Set. The t
emplate | | * structure that is used to compare the elements of the BPatch_Set. The t
emplate | |
| * structure has to overload () for comparison of two elements as explaine
d above | | * structure has to overload () for comparison of two elements as explaine
d above | |
| */ | | */ | |
| | | | |
| typedef enum { RED, BLACK } bpatch_entry_color_type; | | typedef enum { RED, BLACK } bpatch_entry_color_type; | |
| | | | |
|
| template<class T,class Compare = comparison<T> > | | template <class T> | |
| class BPATCH_DLL_EXPORT BPatch_Set { | | struct comparison { | |
| | | bool operator() (const T&l, const T&r) const { return l < r; } | |
| | | }; | |
| | | | |
|
| private: | | template<class Key, class Compare = comparison<Key> > | |
| | | class BPATCH_DLL_EXPORT BPatch_Set { | |
| | | typedef std::set<Key, Compare> int_t; | |
| | | int_t int_set; | |
| | | | |
|
| /** tree implementation structure. Used to implement the RB tree */ | | public: | |
| typedef struct entry { | | typedef typename int_t::iterator iterator; | |
| T data; /* data element */ | | typedef typename int_t::const_iterator const_iterator; | |
| bpatch_entry_color_type color; /* color of the node */ | | | |
| struct entry* left; /* left child */ | | typedef typename int_t::reverse_iterator reverse_iterator; | |
| struct entry* right; /* right child */ | | typedef typename int_t::const_reverse_iterator const_reverse_iterator; | |
| struct entry* parent; /* parent of the node */ | | | |
| | | iterator begin() { return int_set.begin(); } | |
| /** constructor for structure */ | | const_iterator begin() const { return int_set.begin(); } | |
| entry() | | iterator end() { return int_set.end(); } | |
| : color(BLACK),left(NULL),right(NULL),parent(NULL) { | | const_iterator end() const { return int_set.end(); } | |
| } | | | |
| | | reverse_iterator rbegin() { return int_set.rbegin(); } | |
| /** constructor used for non-nil elements | | const_reverse_iterator rbegin() const { return int_set.rbegin(); } | |
| * @param e nil entry | | reverse_iterator rend() { return int_set.rend(); } | |
| */ | | const_reverse_iterator rend() const { return int_set.rend(); } | |
| entry(entry* e) //constructor with nil entry | | | |
| : color(RED),left(e),right(e),parent(NULL) {} | | DO_INLINE_F BPatch_Set() {}; | |
| | | | |
| /** constructor | | /** copy constructor. | |
| * @param d data element | | * @param newBPatch_Set the BPatch_Set which will be copied | |
| * @param e nill entry | | */ | |
| */ | | DO_INLINE_F BPatch_Set(const BPatch_Set<Key,Compare>& rhs){ | |
| entry(const T& d,entry* e) | | int_set = rhs.int_set; | |
| : data(d),color(RED),left(e),right(e),parent(NULL){} | | } | |
| | | | |
| /** constrcutor | | /** returns the cardinality of the tree , number of elements */ | |
| * @param e the entry structure that will be copied | | DO_INLINE_F unsigned int size() const { return int_set.size(); } | |
| */ | | | |
| entry(const entry& e) : data(e.data),color(e.color), | | /** returns true if tree is empty */ | |
| left(NULL),right(NULL),parent(NULL) {} | | DO_INLINE_F bool empty() const { return int_set.empty(); } | |
| } entry; | | | |
| | | /** inserts the element in the tree | |
| /** pointer to define the nil element of the tree NULL is not used | | * @param 1 element that will be inserted | |
| * since some operations need sentinel nil which may have non-nil | | */ | |
| * parent. | | DO_INLINE_F void insert(const Key &k) { int_set.insert(k); } | |
| */ | | | |
| entry* nil; | | /** removes the element in the tree | |
| | | * @param 1 element that will be removed | |
| /** size of the BPatch_Set */ | | */ | |
| unsigned int setSize; | | DO_INLINE_F void remove(const Key &k) { int_set.erase(k); } | |
| | | DO_INLINE_F void erase(const Key &k) { int_set.erase(k); } | |
| /** pointer to the tree structure */ | | | |
| entry* setData; | | /** returns true if the argument is member of the BPatch_Set | |
| | | * @param e the element that will be searched for | |
| /** the structure that will be used for comparisons. Default is the | | */ | |
| * the comparsion structure explained above */ | | DO_INLINE_F bool contains(const Key &key) const { return int_set.find(ke | |
| Compare compareFunc; | | y) != int_set.end(); } | |
| | | | |
| // method that replicates the tree structure of this tree | | /** fill an buffer array with the sorted | |
| DO_INLINE_P entry* replicateTree(entry*,entry*,entry*,entry*); | | * elements of the BPatch_Set in ascending order according to comparison | |
| | | function | |
| // method that implements left rotattion used by RB tree for balance | | * if the BPatch_Set is empty it retuns NULL, other wise it returns | |
| d | | * the input argument. | |
| // tree construction and keeps the RBtree properties. | | */ | |
| DO_INLINE_P void leftRotate(entry*); | | DO_INLINE_F Key* elements(Key *a) const { | |
| | | std::copy(begin(), end(), a); | |
| // method that implements right rotattion used by RB tree for balanc | | return a; | |
| ed | | } | |
| // tree construction and keeps the RBtree properties. | | | |
| DO_INLINE_P void rightRotate(entry*); | | /** Like the above, but put things in a vector. | |
| | | */ | |
| // method that modifies the tree structure after deletion for keepin | | DO_INLINE_F void elements(BPatch_Vector<Key> &v) { | |
| g | | std::copy(begin(), end(), std::back_inserter(v)); | |
| // the RBtree properties. | | } | |
| DO_INLINE_P void deleteFixup(entry*); | | | |
| | | /** returns the minimum valued member in the BPatch_Set according to the | |
| // insertion to a binary search tree. It returns the new element poi | | * comparison function supplied. If the BPatch_Set is empty it retuns | |
| nter | | * any number. Not safe to use for empty sets | |
| // that is inserted. If element is already there it returns NULL | | */ | |
| DO_INLINE_P entry* treeInsert(const T&); | | DO_INLINE_F Key minimum() const { | |
| | | if (empty()) return Key(); | |
| // finds the elemnts in the tree that will be replaced with the elem | | return *begin(); | |
| ent | | } | |
| // being deleted in the deletion. That is the element with the larg | | | |
| ets | | /** returns the maximum valued member in the BPatch_Set according to the | |
| // smallest value than the element being deleted. | | * comparison function supplied. If the BPatch_Set is empty it retuns | |
| DO_INLINE_P entry* treeSuccessor(entry* ) const; | | * any number. Not safe to use for empty sets | |
| | | */ | |
| // method that returns the entry pointer for the element that is sea | | DO_INLINE_F Key maximum() const { | |
| rched | | if (empty()) return Key(); | |
| //for. If the entry is not found then it retuns NULL | | return *(--end()); | |
| DO_INLINE_P entry* find(const T&) const; | | } | |
| | | | |
| // infix traverse of the RB tree. It traverses the tree in ascending | | /** assignment operator for BPatch_Set. It replicate sthe tree | |
| order | | * structure into the new BPatch_Set. | |
| DO_INLINE_P void traverse(T*,entry*,int&) const; | | * @param 1 BPatch_Set that will be used in assignment | |
| | | */ | |
| // And vectorized | | DO_INLINE_F BPatch_Set<Key,Compare>& operator= (const BPatch_Set<Key,Com | |
| DO_INLINE_P void traverse(BPatch_Vector<T> &, entry *) const; | | pare>&rhs) { | |
| | | int_set = rhs.int_set; | |
| // deletes the tree structure for deconstructor. | | return *this; | |
| DO_INLINE_P void destroy(entry*); | | } | |
| | | | |
| public: | | /** equality comparison for the BPatch_Set | |
| | | * @param 1 BPatch_Set that will be used equality check | |
| class iterator | | */ | |
| { | | DO_INLINE_F bool operator== (const BPatch_Set<Key,Compare>&rhs) const { | |
| private: | | return int_set == rhs.int_set; | |
| entry* ent; | | } | |
| entry* nil; | | | |
| public: | | /** inequality comparison for the BPatch_Set | |
| iterator(): ent( NULL ), nil( NULL ){} | | * @param 1 BPatch_Set that will be used inequality check | |
| iterator( entry* e, BPatch_Set<T, Compare >* bs ) | | */ | |
| : ent( e ), nil( bs->nil ){} | | DO_INLINE_F bool operator!= (const BPatch_Set<Key,Compare>&rhs) const { | |
| ~iterator(){} | | return int_set != rhs.int_set; | |
| | | } | |
| void operator++( int ) | | | |
| { | | /** insertion in to the BPatch_Set | |
| if( ent == nil || ent == NULL ) | | * @param 1 element that will be inserted | |
| return; | | */ | |
| | | DO_INLINE_F BPatch_Set<Key,Compare>& operator+= (const Key &k) { | |
| //current node has right child | | int_set.insert(k); | |
| if( ent->right != nil ) | | return *this; | |
| { | | } | |
| ent = ent->right; | | | |
| while( ent->left != nil ) | | /** union operation with this BPatch_Set | |
| ent = ent->left; | | * @param 1 BPatch_Set that will be used in union operation | |
| } | | */ | |
| else | | DO_INLINE_F BPatch_Set<Key,Compare>& operator|= (const BPatch_Set<Key,Co | |
| { | | mpare> &rhs) { | |
| while( ent->parent && ent->parent != nil && | | Compare comp; | |
| ent->parent->right != nil && ent == ent->parent->rig | | std::set<Key, Compare> tmp; | |
| ht ) | | std::set_union(begin(), end(), rhs.begin(), rhs.end(), | |
| { | | std::inserter(tmp, tmp.begin()), comp); | |
| ent = ent->parent; | | int_set.swap(tmp); | |
| } | | return *this; | |
| | | } | |
| if( ent->parent == nil || ent->parent == NULL ) | | | |
| { | | /** intersection operation with this BPatch_Set | |
| ent = nil; | | * @param 1 BPatch_Set that will be used in intersection operation | |
| } | | */ | |
| else | | DO_INLINE_F BPatch_Set<Key,Compare>& operator&= (const BPatch_Set<Key,Co | |
| { | | mpare>&rhs) { | |
| ent = ent->parent; | | Compare comp; | |
| } | | std::set<Key, Compare> tmp; | |
| } | | std::set_intersection(begin(), end(), rhs.begin(), rhs.end(), | |
| } | | std::inserter(tmp, tmp.begin()), comp); | |
| | | int_set.swap(tmp); | |
| void operator--( int ) | | return *this; | |
| { | | } | |
| if( ent == nil || ent == NULL ) | | | |
| return; | | /** difference operation with this BPatch_Set | |
| | | * @param 1 BPatch_Set that will be used in difference operation | |
| //current node has left child | | */ | |
| if( ent->left != nil ) | | DO_INLINE_F BPatch_Set<Key,Compare>& operator-= (const BPatch_Set<Key,Co | |
| { | | mpare>&rhs) { | |
| ent = ent->left; | | Compare comp; | |
| while( ent->right != nil ) | | std::set<Key, Compare> tmp; | |
| ent = ent->right; | | std::set_difference(begin(), end(), rhs.begin(), rhs.end(), | |
| } | | std::inserter(tmp, tmp.begin()), comp); | |
| else | | int_set.swap(tmp); | |
| { | | return *this; | |
| while( ent->parent && ent->parent != nil && | | } | |
| ent->parent->left != nil && ent == ent->parent->left | | | |
| ) | | /** union operation | |
| { | | * @param 1 BPatch_Set that will be used in union operation | |
| ent = ent->parent; | | */ | |
| } | | DO_INLINE_F BPatch_Set<Key,Compare> operator| (const BPatch_Set<Key,Comp | |
| | | are>&rhs) const { | |
| if( ent->parent == nil || ent->parent == NULL ) | | Compare comp; | |
| { | | BPatch_Set<Key, Compare> tmp; | |
| ent = nil; | | std::set_union(begin(), end(), rhs.begin(), rhs.end(), | |
| } | | std::inserter(tmp.int_set, tmp.int_set.begin()), comp) | |
| else | | ; | |
| { | | return tmp; | |
| ent = ent->parent; | | } | |
| } | | | |
| } | | /** intersection operation | |
| } | | * @param 1 BPatch_Set that will be used in intersection operation | |
| | | */ | |
| void operator=( const iterator& right ) | | DO_INLINE_F BPatch_Set<Key,Compare> operator& (const BPatch_Set<Key,Comp | |
| { | | are>&rhs) const { | |
| ent = right.ent; | | Compare comp; | |
| nil = right.nil; | | BPatch_Set<Key, Compare> tmp; | |
| } | | std::set_intersection(begin(), end(), rhs.begin(), rhs.end(), | |
| | | std::inserter(tmp.int_set, tmp.int_set.begin()) | |
| bool operator==( const iterator& right ) | | , comp); | |
| { | | return tmp; | |
| return ent == right.ent; | | } | |
| } | | | |
| bool operator!=( const iterator& right ) | | /** difference operation | |
| { | | * @param 1 BPatch_Set that will be used in difference operation | |
| return ent != right.ent; | | */ | |
| } | | DO_INLINE_F BPatch_Set<Key,Compare> operator- (const BPatch_Set<Key,Comp | |
| | | are>&rhs) const { | |
| T& operator*() | | Compare comp; | |
| { | | BPatch_Set<Key, Compare> tmp; | |
| return ent->data; | | std::set_difference(begin(), end(), rhs.begin(), rhs.end(), | |
| } | | std::inserter(tmp.int_set, tmp.int_set.begin()), | |
| }; | | comp); | |
| | | return tmp; | |
| friend class iterator; | | } | |
| | | | |
| iterator begin() | | /** removes the element in the root of the tree | |
| { | | * if the BPatch_Set is empty it return false | |
| entry* b = setData; | | * @param e refernce to the element that the value of removed | |
| /* Check for an empty set */ | | * element will be copied. | |
| if (b == nil) | | */ | |
| return iterator(nil, this); | | DO_INLINE_F bool extract(Key&k) { | |
| | | if (empty()) return false; | |
| while( b->left != nil ) | | iterator iter = begin(); | |
| b = b->left; | | std::advance(iter, size() / 2); | |
| | | k = *iter; | |
| return iterator( b, this ); | | int_set.erase(iter); | |
| } | | return true; | |
| | | } | |
| iterator end() | | | |
| { | | | |
| return iterator( nil, this ); | | | |
| } | | | |
| iterator rend() | | | |
| { | | | |
| return iterator( nil, this ); | | | |
| } | | | |
| | | | |
| iterator rbegin() | | | |
| { | | | |
| entry* b = setData; | | | |
| | | | |
| /* Check for an empty set */ | | | |
| if (b == nil) | | | |
| return iterator(nil, this); | | | |
| | | | |
| while( b->right != nil ) | | | |
| b = b->right; | | | |
| | | | |
| return iterator( b, this ); | | | |
| } | | | |
| | | | |
| /** constructor. The default comparison structure is used */ | | | |
| DO_INLINE_F BPatch_Set() : setSize(0), compareFunc() | | | |
| { | | | |
| nil = new entry; | | | |
| setData = nil; | | | |
| //compareFunc = Compare(); | | | |
| } | | | |
| | | | |
| /** copy constructor. | | | |
| * @param newBPatch_Set the BPatch_Set which will be copied | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set(const BPatch_Set<T,Compare>& newBPatch_Set){ | | | |
| nil = new entry; | | | |
| compareFunc = newBPatch_Set.compareFunc; | | | |
| setSize = newBPatch_Set.setSize; | | | |
| setData = replicateTree(newBPatch_Set.setData,NULL,newBPatch | | | |
| _Set.nil,nil); | | | |
| } | | | |
| | | | |
| /** destructor which deletes all tree structure and allocated entrie | | | |
| s */ | | | |
| DO_INLINE_F ~BPatch_Set() | | | |
| { | | | |
| destroy(setData); | | | |
| delete nil; | | | |
| } | | | |
| | | | |
| /** returns the cardinality of the tree , number of elements */ | | | |
| DO_INLINE_F unsigned int size() const { return setSize; } | | | |
| | | | |
| /** returns true if tree is empty */ | | | |
| DO_INLINE_F bool empty() const { return (setData == nil); } | | | |
| | | | |
| /** inserts the element in the tree | | | |
| * @param 1 element that will be inserted | | | |
| */ | | | |
| DO_INLINE_F void insert(const T&); | | | |
| | | | |
| /** removes the element in the tree | | | |
| * @param 1 element that will be removed | | | |
| */ | | | |
| DO_INLINE_F void remove(const T&); | | | |
| | | | |
| /** returns true if the argument is member of the BPatch_Set | | | |
| * @param e the element that will be searched for | | | |
| */ | | | |
| DO_INLINE_F bool contains(const T&) const; | | | |
| | | | |
| /** fill an buffer array with the sorted | | | |
| * elements of the BPatch_Set in ascending order according to compa | | | |
| rison function | | | |
| * if the BPatch_Set is empty it retuns NULL, other wise it returns | | | |
| * the input argument. | | | |
| */ | | | |
| DO_INLINE_F T* elements(T*) const; | | | |
| | | | |
| /** Like the above, but put things in a vector. | | | |
| */ | | | |
| DO_INLINE_F void elements(BPatch_Vector<T> &) const; | | | |
| | | | |
| /** returns the minimum valued member in the BPatch_Set according to | | | |
| the | | | |
| * comparison function supplied. If the BPatch_Set is empty it retu | | | |
| ns | | | |
| * any number. Not safe to use for empty sets | | | |
| */ | | | |
| DO_INLINE_F T minimum() const; | | | |
| | | | |
| /** returns the maximum valued member in the BPatch_Set according to | | | |
| the | | | |
| * comparison function supplied. If the BPatch_Set is empty it retu | | | |
| ns | | | |
| * any number. Not safe to use for empty sets | | | |
| */ | | | |
| DO_INLINE_F T maximum() const; | | | |
| | | | |
| /** assignment operator for BPatch_Set. It replicate sthe tree | | | |
| * structure into the new BPatch_Set. | | | |
| * @param 1 BPatch_Set that will be used in assignment | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& operator= (const BPatch_Set<T,Com | | | |
| pare>&); | | | |
| | | | |
| /** equality comparison for the BPatch_Set | | | |
| * @param 1 BPatch_Set that will be used equality check | | | |
| */ | | | |
| DO_INLINE_F bool operator== (const BPatch_Set<T,Compare>&) const; | | | |
| | | | |
| /** inequality comparison for the BPatch_Set | | | |
| * @param 1 BPatch_Set that will be used inequality check | | | |
| */ | | | |
| DO_INLINE_F bool operator!= (const BPatch_Set<T,Compare>&) const; | | | |
| | | | |
| /** insertion in to the BPatch_Set | | | |
| * @param 1 element that will be inserted | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& operator+= (const T&); | | | |
| | | | |
| /** union operation with this BPatch_Set | | | |
| * @param 1 BPatch_Set that will be used in union operation | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& operator|= (const BPatch_Set<T,Co | | | |
| mpare>&); | | | |
| | | | |
| /** intersection operation with this BPatch_Set | | | |
| * @param 1 BPatch_Set that will be used in intersection operation | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& operator&= (const BPatch_Set<T,Co | | | |
| mpare>&); | | | |
| | | | |
| /** difference operation with this BPatch_Set | | | |
| * @param 1 BPatch_Set that will be used in difference operation | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& operator-= (const BPatch_Set<T,Co | | | |
| mpare>&); | | | |
| | | | |
| /** union operation | | | |
| * @param 1 BPatch_Set that will be used in union operation | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare> operator| (const BPatch_Set<T,Comp | | | |
| are>&) const; | | | |
| | | | |
| /** intersection operation | | | |
| * @param 1 BPatch_Set that will be used in intersection operation | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare> operator& (const BPatch_Set<T,Comp | | | |
| are>&) const; | | | |
| | | | |
| /** difference operation | | | |
| * @param 1 BPatch_Set that will be used in difference operation | | | |
| */ | | | |
| DO_INLINE_F BPatch_Set<T,Compare> operator- (const BPatch_Set<T,Comp | | | |
| are>&) const; | | | |
| | | | |
| /** removes the element in the root of the tree | | | |
| * if the BPatch_Set is empty it return false | | | |
| * @param e refernce to the element that the value of removed | | | |
| * element will be copied. | | | |
| */ | | | |
| DO_INLINE_F bool extract(T&); | | | |
| | | | |
| }; | | }; | |
| | | | |
|
| // VG(06/15/02): VC.NET doesn't like definitions for dll imports | | | |
| #ifndef BPATCH_DLL_IMPORT | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| typename BPatch_Set<T,Compare>::entry* | | | |
| BPatch_Set<T,Compare>::replicateTree(entry* node,entry* parent, | | | |
| entry* oldNil,entry* newNil) | | | |
| { | | | |
| if(node == oldNil) | | | |
| return newNil; | | | |
| | | | |
| entry* newNode = new entry(*node); | | | |
| newNode->parent = parent; | | | |
| newNode->left = replicateTree(node->left,newNode,oldNil,newNil); | | | |
| newNode->right = replicateTree(node->right,newNode,oldNil,newNil); | | | |
| return newNode; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| void BPatch_Set<T,Compare>::destroy(entry* node){ | | | |
| if(!node || (node == nil)) | | | |
| return; | | | |
| if(node->left != nil) | | | |
| destroy(node->left); | | | |
| if(node->right != nil) | | | |
| destroy(node->right); | | | |
| delete node; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| void BPatch_Set<T,Compare>::traverse(T* all,entry* node,int& n) const{ | | | |
| if(node == nil) | | | |
| return; | | | |
| if(node->left != nil) | | | |
| traverse(all,node->left,n); | | | |
| if(all) | | | |
| all[n++] = node->data; | | | |
| if(node->right != nil) | | | |
| traverse(all,node->right,n); | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| void BPatch_Set<T,Compare>::traverse(BPatch_Vector<T> &all,entry* node) con | | | |
| st{ | | | |
| if(node == nil) | | | |
| return; | | | |
| if(node->left != nil) | | | |
| traverse(all,node->left); | | | |
| all.push_back(node->data); | | | |
| if(node->right != nil) | | | |
| traverse(all,node->right); | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F T BPatch_Set<T,Compare>::minimum() const{ | | | |
| if(setData == nil) | | | |
| return nil->data; | | | |
| entry* node = setData; | | | |
| while(node->left != nil) | | | |
| node = node->left; | | | |
| return node->data; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F T BPatch_Set<T,Compare>::maximum() const{ | | | |
| if(setData == nil) | | | |
| return nil->data; | | | |
| entry* node = setData; | | | |
| while(node->right != nil) | | | |
| node = node->right; | | | |
| return node->data; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F T* BPatch_Set<T,Compare>::elements(T* buffer) const{ | | | |
| if(setData == nil) return NULL; | | | |
| if(!buffer) return NULL; | | | |
| int tmp = 0; | | | |
| traverse(buffer,setData,tmp); | | | |
| return buffer; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F void BPatch_Set<T,Compare>::elements(BPatch_Vector<T> &buffer) | | | |
| const{ | | | |
| if(setData == nil) return; | | | |
| traverse(buffer,setData); | | | |
| return; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& BPatch_Set<T,Compare>::operator= (const | | | |
| BPatch_Set<T,Compare>& newBPatch_Set){ | | | |
| if(this == &newBPatch_Set) | | | |
| return *this; | | | |
| destroy(setData); | | | |
| compareFunc = newBPatch_Set.compareFunc; | | | |
| setSize = newBPatch_Set.setSize; | | | |
| nil->parent = NULL; | | | |
| setData = replicateTree(newBPatch_Set.setData,NULL,newBPatch_Set.nil | | | |
| ,nil); | | | |
| return *this; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F bool BPatch_Set<T,Compare>::operator== (const BPatch_Set<T,Comp | | | |
| are>& newBPatch_Set) const{ | | | |
| unsigned int i; | | | |
| if(this == &newBPatch_Set) | | | |
| return true; | | | |
| T* all = new T[newBPatch_Set.size()]; | | | |
| newBPatch_Set.elements(all); | | | |
| for(i=0;i<newBPatch_Set.size();i++) | | | |
| if(!contains(all[i])) | | | |
| return false; | | | |
| delete[] all; | | | |
| all = new T[setSize]; | | | |
| elements(all); | | | |
| for(i=0;i<setSize;i++) | | | |
| if(!newBPatch_Set.contains(all[i])) | | | |
| return false; | | | |
| delete[] all; | | | |
| return true; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F bool BPatch_Set<T,Compare>::operator!= (const BPatch_Set<T,Comp | | | |
| are>& newBPatch_Set) const{ | | | |
| return !(*this == newBPatch_Set); | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& BPatch_Set<T,Compare>::operator+= (const | | | |
| T& element){ | | | |
| insert(element); | | | |
| return *this; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& BPatch_Set<T,Compare>::operator|= (const | | | |
| BPatch_Set<T,Compare>& newBPatch_Set){ | | | |
| if(this == &newBPatch_Set) | | | |
| return *this; | | | |
| T* all = new T[newBPatch_Set.size()]; | | | |
| newBPatch_Set.elements(all); | | | |
| for(unsigned int i=0;i<newBPatch_Set.size();i++) | | | |
| insert(all[i]); | | | |
| delete[] all; | | | |
| return *this; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& BPatch_Set<T,Compare>::operator&= (const | | | |
| BPatch_Set<T,Compare>& newBPatch_Set){ | | | |
| if(this == &newBPatch_Set) | | | |
| return *this; | | | |
| T* all = new T[setSize]; | | | |
| elements(all); | | | |
| unsigned int s = setSize; | | | |
| for(unsigned int i=0;i<s;i++) | | | |
| if(!newBPatch_Set.contains(all[i])) | | | |
| remove(all[i]); | | | |
| delete[] all; | | | |
| return *this; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare>& BPatch_Set<T,Compare>::operator-= (const | | | |
| BPatch_Set<T,Compare>& newBPatch_Set){ | | | |
| T* all = new T[setSize]; | | | |
| elements(all); | | | |
| unsigned int s = setSize; | | | |
| for(unsigned int i=0;i<s;i++) | | | |
| if(newBPatch_Set.contains(all[i])) | | | |
| remove(all[i]); | | | |
| delete[] all; | | | |
| return *this; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare> BPatch_Set<T,Compare>::operator| (const B | | | |
| Patch_Set<T,Compare>& newBPatch_Set) const{ | | | |
| BPatch_Set<T,Compare> ret; | | | |
| ret |= newBPatch_Set; | | | |
| ret |= *this; | | | |
| return ret; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare> BPatch_Set<T,Compare>::operator& (const B | | | |
| Patch_Set<T,Compare>& newBPatch_Set) const{ | | | |
| BPatch_Set<T,Compare> ret; | | | |
| ret |= newBPatch_Set; | | | |
| ret &= *this; | | | |
| return ret; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F BPatch_Set<T,Compare> BPatch_Set<T,Compare>::operator- (const B | | | |
| Patch_Set<T,Compare>& newBPatch_Set) const{ | | | |
| BPatch_Set<T,Compare> ret; | | | |
| ret |= *this; | | | |
| ret -= newBPatch_Set; | | | |
| return ret; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| void BPatch_Set<T,Compare>::leftRotate(entry* pivot){ | | | |
| if(!pivot || (pivot == nil)) | | | |
| return; | | | |
| entry* y = pivot->right; | | | |
| if(y == nil) | | | |
| return; | | | |
| pivot->right = y->left; | | | |
| if(y->left != nil) | | | |
| y->left->parent = pivot; | | | |
| y->parent = pivot->parent; | | | |
| if(!pivot->parent) | | | |
| setData = y; | | | |
| else if(pivot == pivot->parent->left) | | | |
| pivot->parent->left = y; | | | |
| else | | | |
| pivot->parent->right = y; | | | |
| y->left = pivot; | | | |
| pivot->parent = y; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| void BPatch_Set<T,Compare>::rightRotate(entry* pivot){ | | | |
| if(!pivot || (pivot == nil)) | | | |
| return; | | | |
| entry* x = pivot->left; | | | |
| if(x == nil) | | | |
| return; | | | |
| pivot->left = x->right; | | | |
| if(x->right != nil) | | | |
| x->right->parent = pivot; | | | |
| x->parent = pivot->parent; | | | |
| if(!pivot->parent) | | | |
| setData = x; | | | |
| else if(pivot == pivot->parent->left) | | | |
| pivot->parent->left = x; | | | |
| else | | | |
| pivot->parent->right = x; | | | |
| x->right = pivot; | | | |
| pivot->parent = x; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| typename BPatch_Set<T,Compare>::entry* | | | |
| BPatch_Set<T,Compare>::find(const T& element) const{ | | | |
| entry* x = setData; | | | |
| while(x != nil){ | | | |
| int check = compareFunc(element,x->data); | | | |
| if(check < 0) | | | |
| x = x->left; | | | |
| else if(check > 0) | | | |
| x = x->right; | | | |
| else | | | |
| return x; | | | |
| } | | | |
| return NULL; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F bool BPatch_Set<T,Compare>::contains(const T& element) const{ | | | |
| entry* x = setData; | | | |
| while(x != nil){ | | | |
| int check = compareFunc(element,x->data); | | | |
| if(check < 0) | | | |
| x = x->left; | | | |
| else if(check > 0) | | | |
| x = x->right; | | | |
| else | | | |
| return true; | | | |
| } | | | |
| return false; | | | |
| } | | | |
| /** return pointer if the node is inserted, returns NULL if thevalue is | | | |
| * already there | | | |
| */ | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| typename BPatch_Set<T,Compare>::entry* | | | |
| BPatch_Set<T,Compare>::treeInsert(const T& element){ | | | |
| entry* y = NULL; | | | |
| entry* x = setData; | | | |
| while(x != nil){ | | | |
| y = x; | | | |
| int check = compareFunc(element,x->data); | | | |
| if(check < 0) | | | |
| x = x->left; | | | |
| else if(check > 0) | | | |
| x = x->right; | | | |
| else | | | |
| return NULL; | | | |
| } | | | |
| entry* z = new entry(element,nil); | | | |
| z->parent = y; | | | |
| if(!y) | | | |
| setData = z; | | | |
| else { | | | |
| int check = compareFunc(element,y->data); | | | |
| if(check < 0) | | | |
| y->left = z; | | | |
| else if(check > 0) | | | |
| y->right = z; | | | |
| } | | | |
| setSize++; | | | |
| return z; | | | |
| } | | | |
| /** finds the minimum value node when x is being deleted */ | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| typename BPatch_Set<T,Compare>::entry* | | | |
| BPatch_Set<T,Compare>::treeSuccessor(entry* x) const{ | | | |
| if(!x || (x == nil)) | | | |
| return NULL; | | | |
| if(x->right != nil){ | | | |
| entry* z = x->right; | | | |
| while(z->left != nil) z = z->left; | | | |
| return z; | | | |
| } | | | |
| entry* y = x->parent; | | | |
| while(y && (x == y->right)){ | | | |
| x = y; | | | |
| y = y->parent; | | | |
| } | | | |
| return y; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_P | | | |
| void BPatch_Set<T,Compare>::deleteFixup(entry* x){ | | | |
| while((x != setData) && | | | |
| (x->color == BLACK)) | | | |
| { | | | |
| if(x == x->parent->left){ | | | |
| entry* w = x->parent->right; | | | |
| if(w->color == RED){ | | | |
| w->color = BLACK; | | | |
| x->parent->color = RED; | | | |
| leftRotate(x->parent); | | | |
| w = x->parent->right; | | | |
| } | | | |
| if((w->left->color == BLACK) && | | | |
| (w->right->color == BLACK)){ | | | |
| w->color = RED; | | | |
| x = x->parent; | | | |
| } | | | |
| else{ | | | |
| if(w->right->color == BLACK){ | | | |
| w->left->color = BLACK; | | | |
| w->color = RED; | | | |
| rightRotate(w); | | | |
| w = x->parent->right; | | | |
| } | | | |
| w->color = x->parent->color; | | | |
| x->parent->color = BLACK; | | | |
| w->right->color = BLACK; | | | |
| leftRotate(x->parent); | | | |
| x = setData; | | | |
| } | | | |
| } | | | |
| else{ | | | |
| entry* w = x->parent->left; | | | |
| if(w->color == RED){ | | | |
| w->color = BLACK; | | | |
| x->parent->color = RED; | | | |
| rightRotate(x->parent); | | | |
| w = x->parent->left; | | | |
| } | | | |
| if((w->right->color == BLACK) && | | | |
| (w->left->color == BLACK)){ | | | |
| w->color = RED; | | | |
| x = x->parent; | | | |
| } | | | |
| else{ | | | |
| if(w->left->color == BLACK){ | | | |
| w->right->color = BLACK; | | | |
| w->color = RED; | | | |
| leftRotate(w); | | | |
| w = x->parent->left; | | | |
| } | | | |
| w->color = x->parent->color; | | | |
| x->parent->color = BLACK; | | | |
| w->left->color = BLACK; | | | |
| rightRotate(x->parent); | | | |
| x = setData; | | | |
| } | | | |
| } | | | |
| } | | | |
| x->color = BLACK; | | | |
| } | | | |
| | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F void BPatch_Set<T,Compare>::insert(const T& element){ | | | |
| entry* x = treeInsert(element); | | | |
| if(!x) return; | | | |
| x->color = RED; | | | |
| while((x != setData) && (x->parent->color == RED)){ | | | |
| if(x->parent == x->parent->parent->left){ | | | |
| entry* y = x->parent->parent->right; | | | |
| if(y->color == RED){ | | | |
| x->parent->color = BLACK; | | | |
| y->color = BLACK; | | | |
| x->parent->parent->color = RED; | | | |
| x = x->parent->parent; | | | |
| } | | | |
| else{ | | | |
| if(x == x->parent->right){ | | | |
| x = x->parent; | | | |
| leftRotate(x); | | | |
| } | | | |
| x->parent->color = BLACK; | | | |
| x->parent->parent->color = RED; | | | |
| rightRotate(x->parent->parent); | | | |
| } | | | |
| } | | | |
| else{ | | | |
| entry* y = x->parent->parent->left; | | | |
| if(y->color == RED){ | | | |
| x->parent->color = BLACK; | | | |
| y->color = BLACK; | | | |
| x->parent->parent->color = RED; | | | |
| x = x->parent->parent; | | | |
| } | | | |
| else{ | | | |
| if(x == x->parent->left){ | | | |
| x = x->parent; | | | |
| rightRotate(x); | | | |
| } | | | |
| x->parent->color = BLACK; | | | |
| x->parent->parent->color = RED; | | | |
| leftRotate(x->parent->parent); | | | |
| } | | | |
| } | | | |
| } | | | |
| setData->color = BLACK; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F void BPatch_Set<T,Compare>::remove(const T& element){ | | | |
| entry* z = find(element); | | | |
| if(!z) | | | |
| return; | | | |
| entry* y=((z->left == nil)||(z->right == nil)) ? z : treeSuccessor(z | | | |
| ); | | | |
| entry* x=(y->left != nil) ? y->left : y->right; | | | |
| x->parent = y->parent; | | | |
| if(!y->parent) | | | |
| setData = x; | | | |
| else if(y == y->parent->left) | | | |
| y->parent->left = x; | | | |
| else | | | |
| y->parent->right = x; | | | |
| if(y != z) | | | |
| z->data = y->data; | | | |
| if(y->color == BLACK) | | | |
| deleteFixup(x); | | | |
| setSize--; | | | |
| delete y; | | | |
| } | | | |
| template <class T,class Compare> | | | |
| DO_INLINE_F | | | |
| bool BPatch_Set<T,Compare>::extract(T& element){ | | | |
| element = setData->data; | | | |
| if(setData == nil) | | | |
| return false; | | | |
| remove(element); | | | |
| return true; | | | |
| } | | | |
| | | | |
| #endif /* BPATCH_DLL_IMPORT */ | | | |
| #endif /* _BPatch_Set_h_ */ | | #endif /* _BPatch_Set_h_ */ | |
| | | | |
End of changes. 7 change blocks. |
| 858 lines changed or deleted | | 219 lines changed or added | |
|