this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2008
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
34#include <algorithm>
35
36namespace Gecode { namespace Iter { namespace Ranges {
37
38 /**
39 * \brief Range iterator for negative part of a range iterator
40 *
41 * If \a strict is true, zero is excluded.
42 *
43 * \ingroup FuncIterRanges
44 */
45
46 template<class I, bool strict=false>
47 class Negative {
48 protected:
49 /// Input iterator
50 I i;
51 public:
52 /// \name Constructors and initialization
53 //@{
54 /// Default constructor
55 Negative(void);
56 /// Initialize with ranges from \a i
57 Negative(I& i);
58 /// Initialize with ranges from \a i
59 void init(I& i);
60 //@}
61
62 /// \name Iteration control
63 //@{
64 /// Test whether iterator is still at a range or done
65 bool operator ()(void) const;
66 /// Move iterator to next range (if possible)
67 void operator ++(void);
68 //@}
69
70 /// \name Range access
71 //@{
72 /// Return smallest value of range
73 int min(void) const;
74 /// Return largest value of range
75 int max(void) const;
76 /// Return width of range (distance between minimum and maximum)
77 unsigned int width(void) const;
78 //@}
79 };
80
81
82 template<class I, bool strict>
83 forceinline
84 Negative<I,strict>::Negative(void) {}
85
86 template<class I, bool strict>
87 forceinline void
88 Negative<I,strict>::init(I& i0) {
89 i=i0;
90 }
91
92 template<class I, bool strict>
93 forceinline
94 Negative<I,strict>::Negative(I& i0) : i(i0) {}
95
96 template<class I, bool strict>
97 forceinline void
98 Negative<I,strict>::operator ++(void) {
99 ++i;
100 }
101 template<class I, bool strict>
102 forceinline bool
103 Negative<I,strict>::operator ()(void) const {
104 if (strict) {
105 return i() && (i.min() < 0);
106 } else {
107 return i() && (i.min() <= 0);
108 }
109 }
110
111 template<class I, bool strict>
112 forceinline int
113 Negative<I,strict>::min(void) const {
114 return i.min();
115 }
116 template<class I, bool strict>
117 forceinline int
118 Negative<I,strict>::max(void) const {
119 if (strict) {
120 return std::min(i.max(),-1);
121 } else {
122 return std::min(i.max(),0);
123 }
124 }
125 template<class I, bool strict>
126 forceinline unsigned int
127 Negative<I,strict>::width(void) const {
128 return static_cast<unsigned int>(max()-min()+1);
129 }
130
131}}}
132
133// STATISTICS: iter-any