this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 *
6 * Copyright:
7 * Guido Tack, 2006
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Iter { namespace Ranges {
35
36 /**
37 * \brief %Range iterator with size counting
38 *
39 * Allows to iterate the ranges as defined by the input iterator
40 * and access the size of the set already iterated. After complete
41 * iteration, the reported size is thus the size of the set.
42 *
43 * \ingroup FuncIterRanges
44 */
45 template<class I>
46 class Size {
47 protected:
48 /// Iterator to compute size of
49 I i;
50 /// Accumulated size
51 unsigned int _size;
52 public:
53 /// \name Constructors and initialization
54 //@{
55 /// Default constructor
56 Size(void);
57 /// Initialize with ranges from \a i
58 Size(I& i);
59 /// Initialize with ranges from \a i
60 void init(I& i);
61 //@}
62
63 /// \name Iteration control
64 //@{
65 /// Test whether iterator is still at a range or done
66 bool operator ()(void);
67 /// Move iterator to next range (if possible)
68 void operator ++(void);
69 //@}
70
71 /// \name %Range access
72 //@{
73 /// Return smallest value of range
74 int min(void) const;
75 /// Return largest value of range
76 int max(void) const;
77 /// Return width of range (distance between minimum and maximum)
78 unsigned int width(void) const;
79 //@}
80
81 /// \name %Size access
82 //@{
83 /// Return accumulated size
84 unsigned int size(void) const;
85 //@}
86 };
87
88
89 template<class I>
90 forceinline
91 Size<I>::Size(void)
92 : _size(0) {}
93
94 template<class I>
95 inline void
96 Size<I>::init(I& i0) {
97 i.init(i0);
98 _size = 0;
99 }
100
101 template<class I>
102 inline
103 Size<I>::Size(I& i0) : i(i0), _size(0) {}
104
105 template<class I>
106 forceinline void
107 Size<I>::operator ++(void) {
108 _size += i.width();
109 ++i;
110 }
111 template<class I>
112 forceinline bool
113 Size<I>::operator ()(void) {
114 return i();
115 }
116
117 template<class I>
118 forceinline int
119 Size<I>::min(void) const {
120 return i.min();
121 }
122 template<class I>
123 forceinline int
124 Size<I>::max(void) const {
125 return i.max();
126 }
127 template<class I>
128 forceinline unsigned int
129 Size<I>::width(void) const {
130 return i.width();
131 }
132
133 template<class I>
134 forceinline unsigned int
135 Size<I>::size(void) const {
136 return _size;
137 }
138
139}}}
140
141// STATISTICS: iter-any
142