Back to index

texmacs  1.0.7.15
iterator.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : iterator.cpp
00004 * DESCRIPTION: dynamic iterators
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef ITERATOR_CC
00013 #define ITERATOR_CC
00014 #include "hashmap.hpp"
00015 #include "hashset.hpp"
00016 #include "iterator.hpp"
00017 
00018 template<class T> int
00019 iterator_rep<T>::remains () {
00020   if (busy ()) return -1;
00021   else return 0;
00022 }
00023 
00024 template<class T>
00025 iterator<T>::operator tree () {
00026   tree t (TUPLE);
00027   while (this->rep->busy ()) {
00028     t << as_tree (this->rep->next());
00029   }
00030   return t;
00031 }
00032 
00033 template<class T> tm_ostream&
00034 operator << (tm_ostream& out, iterator<T> it) {
00035   bool flag=false;
00036   out << "[ ";
00037   while (it->busy ()) {
00038     if (flag) out << ", ";
00039     else flag= true;
00040     out << it->next ();
00041   }
00042   return out << " ]";
00043 }
00044 
00045 //hashset_iterator
00046 template<class T>
00047 class hashset_iterator_rep: public iterator_rep<T> {
00048   hashset<T> h;
00049   int i;
00050   list<T> l;
00051   void spool ();
00052 
00053 public:
00054   hashset_iterator_rep<T> (hashset<T> h);
00055   bool busy ();
00056   T next ();
00057 };
00058 
00059 template<class T>
00060 hashset_iterator_rep<T>::hashset_iterator_rep (hashset<T> h2):
00061   h (h2), i (0), l (h2->a[0]) {}
00062 
00063 template<class T> void
00064 hashset_iterator_rep<T>::spool () {
00065   if (i >= h->n) return;
00066   while (is_nil (l)) {
00067     if ((++i) >= h->n) return;
00068     l= h->a[i];
00069   }
00070 }
00071 
00072 template<class T> bool
00073 hashset_iterator_rep<T>::busy () {
00074   spool ();
00075   return i < h->n;
00076 }
00077 
00078 template<class T> T
00079 hashset_iterator_rep<T>::next () {
00080   ASSERT (busy (), "end of iterator");
00081   T x (l->item);
00082   l= l->next;
00083   return x;
00084 }
00085 
00086 template<class T> iterator<T>
00087 iterate (hashset<T> h) {
00088   return tm_new<hashset_iterator_rep<T> > (h);
00089 }
00090 // end hashset_iterator
00091 
00092 // hashmap_iterator
00093 template<class T, class U>
00094 class hashmap_iterator_rep: public iterator_rep<T> {
00095   hashmap<T,U> h;
00096   int i;
00097   list<hashentry<T,U> > l;
00098   void spool ();
00099 
00100 public:
00101   hashmap_iterator_rep (hashmap<T,U> h);
00102   bool busy ();
00103   T next ();
00104 };
00105 
00106 template<class T, class U>
00107 hashmap_iterator_rep<T,U>::hashmap_iterator_rep (hashmap<T,U> h2):
00108   h (h2), i (0), l (h2->a[0]) {}
00109 
00110 template<class T, class U> void
00111 hashmap_iterator_rep<T,U>::spool () {
00112   if (i >= h->n) return;
00113   while (is_nil (l)) {
00114     if ((++i) >= h->n) return;
00115     l= h->a[i];
00116   }
00117 }
00118 
00119 template<class T, class U> bool
00120 hashmap_iterator_rep<T,U>::busy () {
00121   spool ();
00122   return i < h->n;
00123 }
00124 
00125 template<class T, class U> T
00126 hashmap_iterator_rep<T,U>::next () {
00127   ASSERT (busy (), "end of iterator");
00128   T x (l->item.key);
00129   l= l->next;
00130   return x;
00131 }
00132 
00133 template<class T, class U> iterator<T>
00134 iterate (hashmap<T,U> h) {
00135   return tm_new<hashmap_iterator_rep<T,U> > (h);
00136 }
00137 // hashmap_iterator
00138 
00139 #endif // defined ITERATOR_CC