Vaucanson  1.4.1
sparse_interval.hxx
1 // 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_MISC_SPARSE_INTERVAL_HXX
18 # define VCSN_MISC_SPARSE_INTERVAL_HXX
19 
22 # include <sstream>
23 
24 namespace vcsn
25 {
26  namespace misc
27  {
28  /*---------------.
29  | SparseIterator |
30  `---------------*/
31 
32  template <class Integer, class ExcludedContainer>
33  SparseIterator<Integer, ExcludedContainer>::
34  SparseIterator (integer_t from,
35  const excluded_container_t& c)
36  : excluded_ (&c),
37  integer_ (from)
38  {}
39 
40  template <class Integer, class ExcludedContainer>
41  SparseIterator<Integer, ExcludedContainer>&
42  SparseIterator<Integer, ExcludedContainer>::operator++ ()
43  {
44  if (excluded_->size () == 0)
45  integer_ = integer_ + 1;
46  else
47  do
48  integer_ = integer_ + 1;
49  while (excluded_->find (integer_) != excluded_->end ());
50  return *this;
51  }
52 
53  template <class Integer, class ExcludedContainer>
54  SparseIterator<Integer, ExcludedContainer>&
55  SparseIterator<Integer, ExcludedContainer>::operator-- ()
56  {
57  if (excluded_->size () == 0)
58  integer_ = integer_ - 1;
59  else
60  do
61  integer_ = integer_ - 1;
62  while (excluded_->find (integer_) != excluded_->end ());
63  return *this;
64  }
65 
66  template <class Integer, class ExcludedContainer>
67  SparseIterator<Integer, ExcludedContainer>
68  SparseIterator<Integer, ExcludedContainer>::operator++ (int)
69  {
70  SparseIterator tmp = *this;
71  ++*this;
72  return tmp;
73  }
74 
75  template <class Integer, class ExcludedContainer>
76  SparseIterator<Integer, ExcludedContainer>
77  SparseIterator<Integer, ExcludedContainer>::operator-- (int)
78  {
79  SparseIterator tmp = *this;
80  --*this;
81  return tmp;
82  }
83 
84  template <class Integer, class ExcludedContainer>
85  typename SparseIterator<Integer, ExcludedContainer>::
86  integer_t
88  {
89  return integer_;
90  }
91 
92  template <class Integer, class ExcludedContainer>
93  bool
95  ::operator!= (const SparseIterator& i) const
96  {
97  return i.integer_ != integer_;
98  }
99 
100  template <class Integer, class ExcludedContainer>
101  bool
102  SparseIterator<Integer, ExcludedContainer>
103  ::operator== (const SparseIterator& i) const
104  {
105  return i.integer_ == integer_;
106  }
107 
108  template <class Integer, class ExcludedContainer>
109  SparseIterator<Integer, ExcludedContainer>&
110  SparseIterator<Integer, ExcludedContainer>
111  ::operator= (const SparseIterator& i)
112  {
113  integer_ = i.integer_;
114  excluded_ = i.excluded_;
115  return *this;
116  }
117 
118 
119 
120 
121  /*-----------------.
122  | SparseInterval. |
123  `-----------------*/
124 
125 
129  template <class Integer, class ExcludedContainer>
131  ::SparseInterval (integer_t f, integer_t t, const excluded_container_t& c)
132  : excluded_ (c),
133  from_ (f),
134  to_ (t)
135  {
136  precondition (from_ <= to_ + 1);
137  precondition (excluded_.find (to_ + 1) == excluded_.end ());
138  }
139 
140  template <class Integer, class ExcludedContainer>
142  ::SparseInterval (const SparseInterval& a)
143  : excluded_ (a.excluded_),
144  from_ (a.from_),
145  to_ (a.to_)
146  {
147  }
148 
149  template <class Integer, class ExcludedContainer>
150  unsigned
151  SparseInterval<Integer, ExcludedContainer>::size () const
152  {
153  return to_ < from_ ? 0 : to_ - from_ + 1 - excluded_.size ();
154  }
155 
156  template <class Integer, class ExcludedContainer>
157  typename SparseInterval<Integer, ExcludedContainer>::integer_t
158  SparseInterval<Integer, ExcludedContainer>::max () const
159  {
160  unsigned r = to_;
161 
162  while (excluded_.find (r) != excluded_.end ())
163  --r;
164  return r;
165  }
166 
167  template <class Integer, class ExcludedContainer>
168  std::string
169  SparseInterval<Integer, ExcludedContainer>::to_string () const
170  {
171  std::stringstream s;
172  s << "from :" << from_ << " to : " << to_ << " ex:";
173  for (typename ExcludedContainer::iterator i = excluded_.begin ();
174  i != excluded_.end ();
175  ++i)
176  s << *i << " ";
177  return s.str ();
178  }
179 
180  template <class Integer, class ExcludedContainer>
181  typename SparseInterval<Integer, ExcludedContainer>::iterator
182  SparseInterval<Integer, ExcludedContainer>::begin () const
183  {
184  int from = from_;
185 
186  if (excluded_.size () != 0)
187  while (excluded_.find (from) != excluded_.end ())
188  from = from + 1;
189  return iterator (from, excluded_);
190  }
191 
192  template <class Integer, class ExcludedContainer>
193  typename SparseInterval<Integer, ExcludedContainer>::iterator
194  SparseInterval<Integer, ExcludedContainer>::end () const
195  {
196  return iterator (to_ + 1, excluded_);
197  }
198 
199  } // misc
200 } // vcsn
201 
202 #endif // ! VCSN_MISC_SPARSE_INTERVAL_HXX