Vaucanson  1.4.1
listg_sparse_interval.hxx
1 // listg_sparse_interval.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_AUTOMATA_IMPLEMENTATION_LISTG_LISTG_SPARSE_INTERVAL_HXX
18 # define VCSN_AUTOMATA_IMPLEMENTATION_LISTG_LISTG_SPARSE_INTERVAL_HXX
19 
20 #include <vaucanson/misc/limits.hh>
21 
22 namespace vcsn
23 {
24  namespace misc
25  {
26  /*---------------.
27  | SparseIterator |
28  `---------------*/
29 
30  template <class T, class ExcludedContainer>
31  SparseIterator<handler<T, unsigned>, ExcludedContainer>::
32  SparseIterator (integer_t from,
33  const excluded_container_t& c)
34  : excluded_ (&c),
35  integer_ (from)
36  {}
37 
38  template <class T, class ExcludedContainer>
39  SparseIterator<handler<T, unsigned>, ExcludedContainer>&
40  SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator++ ()
41  {
42  if (excluded_->size () == 0)
43  ++integer_;
44  else
45  do
46  ++integer_;
47  while (excluded_->find (handler_t(integer_)) != excluded_->end ());
48  return *this;
49  }
50 
51  template <class T, class ExcludedContainer>
52  SparseIterator<handler<T, unsigned>, ExcludedContainer>&
53  SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator-- ()
54  {
55  if (excluded_->size () == 0)
56  --integer_;
57  else
58  do
59  --integer_;
60  while (excluded_->find (handler_t(integer_)) != excluded_->end ());
61  return *this;
62  }
63 
64  template <class T, class ExcludedContainer>
65  SparseIterator<handler<T, unsigned>, ExcludedContainer>
66  SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator++ (int)
67  {
68  SparseIterator tmp = *this;
69  ++*this;
70  return tmp;
71  }
72 
73  template <class T, class ExcludedContainer>
74  SparseIterator<handler<T, unsigned>, ExcludedContainer>
75  SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator-- (int)
76  {
77  SparseIterator tmp = *this;
78  --*this;
79  return tmp;
80  }
81 
82  template <class T, class ExcludedContainer>
83  typename SparseIterator<handler<T, unsigned>, ExcludedContainer>::
84  integer_t
85  SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator* ()
86  {
87  return handler_t(integer_);
88  }
89 
90  template <class T, class ExcludedContainer>
91  bool
92  SparseIterator<handler<T, unsigned>, ExcludedContainer>
93  ::operator!= (const SparseIterator& i) const
94  {
95  return i.integer_ != integer_;
96  }
97 
98  template <class T, class ExcludedContainer>
99  bool
100  SparseIterator<handler<T, unsigned>, ExcludedContainer>
101  ::operator== (const SparseIterator& i) const
102  {
103  return i.integer_ == integer_;
104  }
105 
106  template <class T, class ExcludedContainer>
107  SparseIterator<handler<T, unsigned>, ExcludedContainer>&
108  SparseIterator<handler<T, unsigned>, ExcludedContainer>
109  ::operator= (const SparseIterator& i)
110  {
111  integer_ = i.integer_;
112  excluded_ = i.excluded_;
113  return *this;
114  }
115 
116 
117 
118 
119  /*-----------------.
120  | SparseInterval. |
121  `-----------------*/
122 
123 
127  template <class T, class ExcludedContainer>
128  SparseInterval<handler<T, unsigned>, ExcludedContainer>
129  ::SparseInterval (integer_t f, integer_t t, const excluded_container_t& c)
130  : excluded_ (c),
131  from_ (f),
132  to_ (t)
133  {
134  precondition (from_ <= to_ + 1);
135  precondition (excluded_.find (handler_t(to_ + 1)) == excluded_.end ());
136  precondition ((to_ == limits<unsigned int>::max())
137  || (to_ + 1 - from_ >= excluded_.size()));
138  }
139 
140  template <class T, class ExcludedContainer>
141  SparseInterval<handler<T, unsigned>, ExcludedContainer>
143  : excluded_ (a.excluded_),
144  from_ (a.from_),
145  to_ (a.to_)
146  {
147  }
148 
149  template <class T, class ExcludedContainer>
150  unsigned
151  SparseInterval<handler<T, unsigned>, ExcludedContainer>::size () const
152  {
153  // to_ is equal to limits<unsigned int>::max() if the container
154  // is empty. See for instance GClass::states() or GClass::edges().
155  return to_ == limits<unsigned int>::max() ? 0
156  : to_ - from_ + 1 - excluded_.size ();
157  }
158 
159  template <class T, class ExcludedContainer>
160  std::string
161  SparseInterval<handler<T, unsigned>, ExcludedContainer>::to_string () const
162  {
163  std::stringstream s;
164  s << "from :" << from_ << " to : " << to_ << " ex:";
165  for (typename ExcludedContainer::iterator i = excluded_.begin ();
166  i != excluded_.end ();
167  ++i)
168  s << *i << " ";
169  return s.str ();
170  }
171 
172  template <class T, class ExcludedContainer>
173  typename SparseInterval<handler<T, unsigned>, ExcludedContainer>::iterator
174  SparseInterval<handler<T, unsigned>, ExcludedContainer>::begin () const
175  {
176  unsigned from = from_;
177 
178  if (excluded_.size () != 0)
179  while (excluded_.find (handler_t(from)) != excluded_.end ())
180  ++from;
181  return iterator (handler_t(from), excluded_);
182  }
183 
184  template <class T, class ExcludedContainer>
185  typename SparseInterval<handler<T, unsigned>, ExcludedContainer>::iterator
186  SparseInterval<handler<T, unsigned>, ExcludedContainer>::end () const
187  {
188  return iterator (handler_t(to_ + 1), excluded_);
189  }
190 
191  template <class T, class ExcludedContainer>
192  typename SparseInterval<handler<T, unsigned>, ExcludedContainer>::handler_t
193  SparseInterval<handler<T, unsigned>, ExcludedContainer>::back () const
194  {
195  unsigned to = to_;
196 
197  if (excluded_.size () != 0)
198  while (excluded_.find (handler_t(to)) != excluded_.end ())
199  --to;
200  return *(iterator (handler_t(to), excluded_));
201  }
202 
203 
204  } // misc
205 } // vcsn
206 
207 #endif // ! VCSN_MISC_SPARSE_INTERVAL_HXX