Set.H

00001 
00002 /*
00003   This file is part of Aleph system
00004 
00005   Copyright (c) 2002, 2003, 2004, 2005, 2006
00006   UNIVERSITY LOS ANDES (ULA) Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA  
00007 
00008   - Center of Studies in Microelectronics & Distributed Systems (CEMISID) 
00009   - ULA Computer Science Department
00010   - FUNDACITE Mérida - Ministerio de Ciencia y Tecnología
00011 
00012   PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS
00013   DOCUMENTATION IS HEREBY GRANTED, PROVIDED THAT BOTH THE COPYRIGHT
00014   NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES OF THE
00015   SOFTWARE, DERIVATIVE WORKS OR MODIFIED VERSIONS, AND ANY PORTIONS
00016   THEREOF, AND THAT BOTH NOTICES APPEAR IN SUPPORTING DOCUMENTATION.
00017 
00018   Aleph is distributed in the hope that it will be useful, but WITHOUT
00019   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020   or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022   UNIVERSIDAD DE LOS ANDES requests users of this software to return to 
00023 
00024   Leandro Leon
00025   CEMISID 
00026   Ed La Hechicera 
00027   3er piso, ala sur
00028   Facultad de Ingenieria 
00029   Universidad de Los Andes 
00030   Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA    or
00031 
00032   lrleon@ula.ve
00033 
00034   any improvements or extensions that they make and grant Universidad 
00035   de Los Andes (ULA) the rights to redistribute these changes. 
00036 
00037   Aleph is (or was) granted by: 
00038   - Consejo de Desarrollo Cientifico, Humanistico, Tecnico de la ULA 
00039     (CDCHT)
00040   - Fundacite Mérida
00041 */
00042 
00043 /*
00044      Aleph implementation of set C++ standard container
00045 */
00046 
00047 # ifndef AH_SET_H
00048 # define AH_SET_H
00049 
00050 # include <ahPair.H>
00051 # include <ah_stdc++_utils.H>
00052 # include <tpl_treapRk.H>
00053 
00054 namespace Aleph
00055 {
00056 
00078     template <class T, 
00079            class Compare = Aleph::less<T>,
00080            template <class, class> class Tree = Treap_Rk
00081            >
00082 class set
00083 {
00084 private:
00085 
00086   Tree<T, Compare> tree;
00087 
00088   typedef typename Tree<T, Compare>::Node Node;
00089 
00090 public:
00091 
00093   typedef T value_type;
00094 
00096   typedef typename set::value_type * pointer;
00097 
00099   typedef typename set::value_type & reference;
00100 
00102   typedef const typename set::value_type & const_reference;
00103 
00105   typedef size_t size_type;
00106 
00108   typedef T key_type;
00109 
00114   class iterator
00115   {
00116   private:
00117 
00118     friend class set<T, Compare, Tree>;
00119     
00120     typedef typename Tree<T, Compare>::Iterator Iterator;
00121     
00122     Tree<T, Compare> * tree;
00123     Iterator  itor;
00124     bool underflow;
00125     bool overflow;
00126 
00127     iterator (Tree<T, Compare> & _tree, Node * node) 
00128       : tree (&_tree), itor (_tree, node), underflow (false), overflow (false)
00129     {
00130       /* empty */
00131     }
00132 
00133     void init_flags ()
00134     {
00135       if (tree->size () == 0)
00136      underflow = overflow = true;
00137       else
00138      underflow = overflow = false;
00139     }
00140 
00141     iterator (Tree<T, Compare> &_tree) : tree (&_tree), itor (_tree)
00142     {
00143       init_flags ();
00144     }
00145 
00146     void goto_begin ()
00147     {
00148       itor.reset_first ();
00149       init_flags ();
00150     }
00151 
00152     void goto_last ()
00153     {
00154       itor.reset_last ();
00155       init_flags ();
00156     }
00157 
00158     void goto_end ()
00159     {
00160       itor.reset_last ();
00161       init_flags ();
00162       itor.next ();
00163       overflow = true;
00164     }
00165 
00166     void forward ()
00167     {
00168       if (underflow)
00169      {
00170        goto_begin ();
00171        return;
00172      }
00173       
00174       itor.next ();
00175 
00176       if (not itor.has_current () )
00177      overflow = true;
00178     }
00179 
00180     void backward ()
00181     {
00182       if (overflow)
00183      {
00184        goto_last ();
00185        return;
00186      }
00187       
00188       itor.prev ();
00189 
00190       if (not itor.has_current () )
00191      underflow = true;
00192     }
00193 
00194   public:
00195 
00197     typedef typename set<T>::value_type   value_type;
00198 
00201     typedef typename set<T>::size_type       difference_type;
00202 
00204     typedef typename set<T>::value_type *    pointer;
00205 
00207     typedef typename set<T>::reference       reference;
00208 
00210     typedef const typename set<T>::reference const_reference;
00211 
00213     iterator () : tree (NULL), underflow (true), overflow (true) 
00214     {
00215       /* empty */ 
00216     }
00217     
00219     T & operator * ()
00220     {
00221       return KEY (itor.get_current () );
00222     }
00223 
00225     T * operator -> ()
00226     {
00227       return & KEY (itor.get_current () );
00228     }
00229 
00232     T & operator ++ ()
00233     {
00234       forward ();
00235 
00236       return KEY (itor.get_current() );
00237     }
00238 
00240     T & operator ++ (int)
00241     {
00242       T & return_value = KEY (itor.get_current () );
00243 
00244       forward ();
00245 
00246       return return_value;
00247     }
00248 
00251     T & operator -- ()
00252     {
00253       backward ();
00254 
00255       return KEY (itor.get_current () );
00256     }
00257 
00259     T & operator -- (int)
00260     {
00261       T& return_value = KEY (itor.get_current () );
00262       backward ();
00263 
00264       return return_value;
00265     }
00266     
00269     T & operator += (const size_type & n)
00270     {
00271       itor.reset_to_pos (itor.get_current_position () + n);
00272 
00273       return itor.get_current ()->get_key();
00274     } 
00275 
00278     T & operator -= (const size_type & n)
00279     {
00280       itor.reset_to_pos (itor.get_current_position () - n);
00281       
00282       return itor.get_current ()->get_key ();
00283     } 
00284 
00286     bool operator == (const iterator & _itor) const
00287     {
00288       return itor == _itor.itor;
00289     }
00290 
00292     bool operator != (const iterator & _itor) const
00293     {
00294       return not (itor == _itor.itor);
00295     }
00296 
00297     bool verify (const set & _set) const
00298     {
00299       return itor.verify ( (Tree<T, Compare>*) &_set.tree);
00300     }
00301 
00302     bool verify (const iterator & it) const
00303     {
00304       return itor.verify (it.itor);
00305     }
00306   };
00307 
00309   set () { /* empty */  }
00310 
00312   set (const set & c) 
00313   {
00314     tree.getRoot () = copyRec (c.tree.getRoot () );
00315   }
00316 
00332       template <typename Itor> 
00333   set (Itor beg, const Itor & end)
00334   {
00335     Aleph::verify_iterators (beg, end);
00336 
00337     while (beg != end)
00338       tree.insert (new Node (beg++) ) ;
00339   }
00340 
00343   ~set () 
00344   { 
00345     destroyRec (tree.getRoot () );
00346   }  
00347 
00349   size_type size () { return COUNT (tree.getRoot () ); }
00350 
00352   bool empty () const
00353   {
00354     return COUNT (tree.getRoot () ) == 0;
00355   }
00356 
00359   bool operator == (const set & c) const
00360   {
00361     if (this == &c)
00362       return true;
00363 
00364     if (size () != c.size () )
00365       return false;
00366 
00367     typename Tree<T, Compare>::Iterator itor1 (tree), itor2 (c.tree);
00368 
00369     while (itor1.has_current () and itor2.has_current () )
00370       {
00371      if (KEY (itor1.get_current () ) != KEY (itor2.get_current () ))
00372        return false;
00373 
00374      itor1.next ();
00375      itor2.next ();
00376       }
00377     
00378     return true;
00379   }
00380 
00383   bool operator != (const set & c) const
00384   {
00385     return not (*this == c);
00386   }
00387 
00390   bool operator < (const set & c) const
00391   {
00392     if (this == &c)
00393       return false;
00394 
00395     typename Tree<T, Compare>::Iterator itor1 (tree), itor2 (c.tree);
00396 
00397     while (itor1.has_current () and itor2.has_current () )
00398       {
00399      if (KEY (itor1.get_current ()) < KEY (itor2.get_current ()))
00400        return true;
00401      else if (KEY (itor2.get_current ()) < KEY (itor1.get_current () ) )
00402        return false; // no son iguales
00403 
00404      itor1.next ();
00405      itor2.next ();
00406       }
00407     
00408     if (itor1.has_current () ) /* |*this| >= |c| */
00409       return false;
00410 
00411     return itor2.has_current ();
00412   }
00413 
00416   bool operator > (const set & c) const
00417   {
00418     return not (*this == c or *this < c);
00419   }
00420 
00423   bool operator <= (const set & c) const
00424   {
00425     return not (*this > c );
00426   }
00427 
00430   bool operator >= (const set & c) const
00431   {
00432     return not (*this < c);
00433   }
00434   
00446   size_type count (const T & value) 
00447   {
00448     if (tree.search (value) == NULL)
00449       return 0;
00450 
00451     return 1;
00452   } 
00453 
00467   iterator find (const T & value) 
00468   { 
00469     Node * node = tree.search (value);
00470 
00471     if (node == NULL)
00472       return end();
00473 
00474     iterator itor (tree);
00475 
00476     itor.itor.reset_to_node (node);
00477 
00478     return itor;
00479   }
00480 
00484   iterator lower_bound (const T & value)
00485   {
00486     if (size () == 0)
00487       return end ();
00488 
00489     Node * p = search_rank_parent (tree.getRoot (), value);
00490 
00491     return iterator (tree, p);
00492   }
00493 
00497   iterator upper_bound (const T & value)
00498   {
00499     if (size () == 0)
00500       return end ();
00501 
00502     Node * p = search_rank_parent (tree.getRoot (), value);
00503 
00504     iterator upper (tree, p);
00505     
00506     if (KEY (p) == value)
00507       upper.itor.next ();
00508 
00509     return upper;
00510   }
00511 
00513   set & operator = (const set & c)
00514   {
00515     if (*this == &c)
00516       return *this;
00517 
00518     destroyRec (tree.getRoot () );
00519 
00520     tree.getRoot () = copyRec (c.tree.getRoot () ); 
00521 
00522     return *this;
00523   }
00524 
00527   void swap (set & c)
00528   {
00529     Aleph::swap (tree.getRoot (), c.tree.getRoot () );
00530   }
00531 
00533   iterator begin () 
00534   {
00535     return iterator (tree);
00536   }
00537 
00539   iterator end ()
00540   {
00541     iterator last (tree);
00542     last.goto_end ();
00543 
00544     return last;
00545   }
00546 
00562   Aleph::pair<iterator, bool> insert (const T & value)
00563   {
00564     Node * node = new Node (value);
00565 
00566     iterator itor (tree);
00567 
00568     if (tree.insert (node) == NULL)
00569       {
00570      delete node;
00571      itor.itor.reset_to_key (value);
00572      
00573      return Aleph::pair<iterator, bool> (itor, false);
00574       }
00575 
00576     itor.itor.reset_to_node (node);
00577 
00578     return Aleph::pair<iterator, bool> (itor, true);
00579   }
00580     
00602   iterator insert (const iterator & /* pos */, const T & value)
00603   {
00604     Node * node = new Node (value);
00605 
00606     iterator _itor (tree);
00607 
00608     if (tree.insert (node) == NULL)
00609       { // clave duplicada
00610      delete node;
00611      _itor.itor.reset_to_key (value);
00612       }
00613     else
00614       _itor.itor.reset_to_node (node);   
00615   
00616     return _itor;    
00617   }
00618 
00636       template <typename Itor>
00637   void insert (Itor beg, const Itor & end)
00638   {
00639     Aleph::verify_iterators (beg, end);
00640 
00641     while (beg != end)
00642     {
00643       insert (*beg);
00644       beg++;
00645     }
00646   }
00647 
00659   size_type erase (const T & value)
00660   {
00661     Node * p = tree.remove (value);
00662 
00663     if (p == NULL)
00664       return 0;
00665      
00666     delete p;
00667 
00668     return 1;
00669   }
00670 
00684   void erase (iterator pos)
00685   {
00686     Aleph::verify_container_and_iterator (*this, pos);
00687 
00688     delete pos.itor.del ();
00689   }
00690   
00704   iterator erase (const iterator & beg, const iterator & end)
00705   {
00706     Aleph::verify_container_and_iterator (*this, beg);
00707     Aleph::verify_iterators (beg, end);
00708 
00709     iterator ret_val = end;
00710 
00711     const size_t pos_beg = beg.itor.get_current_position ();
00712     const size_t pos_end = end.itor.get_current_position ();
00713 
00714     Node * removed_tree = tree.remove(pos_beg, pos_end - 1);
00715 
00716     destroyRec(removed_tree);
00717 
00718     return ret_val;
00719   }
00720 
00722   void clear ()
00723   {
00724     destroyRec(tree.getRoot());
00725   }
00726 };
00727 
00728 
00741     template <typename T>
00742 typename iterator_traits<typename set<T>::iterator>::difference_type
00743 distance(typename set<T>::iterator it1, typename set<T>::iterator it2)
00744 {
00745   Aleph::verify_iterators(it1, it2);
00746 
00747   return it2.itor.get_current_position() - it1.itor.get_current_position();
00748 }
00749 
00750 }
00751 
00752 # endif // AH_SET_H

Leandro R. León