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 34namespace Gecode { namespace Iter { namespace Values { 35 36 /** 37 * \brief Value iterator for mapping values of a value iterator 38 * 39 * If \a strict is true, the values obtained by mapping must be 40 * strictly increasing (that is, no duplicates). 41 * 42 * \ingroup FuncIterValues 43 */ 44 template<class I, class M, bool strict=false> 45 class Map { 46 protected: 47 /// Input iterator 48 I i; 49 /// Mapping object 50 M m; 51 public: 52 /// \name Constructors and initialization 53 //@{ 54 /// Default constructor 55 Map(void); 56 /// Initialize with values from \a i 57 Map(I& i); 58 /// Initialize with values from \a i and map \a m 59 Map(I& i, const M& m); 60 /// Initialize with values from \a i 61 void init(I& i); 62 /// Initialize with values from \a i and map \a m 63 void init(I& i, const M& m); 64 //@} 65 66 /// \name Iteration control 67 //@{ 68 /// Test whether iterator is still at a value or done 69 bool operator ()(void) const; 70 /// Move iterator to next value (if possible) 71 void operator ++(void); 72 //@} 73 74 /// \name Value access 75 //@{ 76 /// Return current value 77 int val(void) const; 78 //@} 79 }; 80 81 82 template<class I, class M, bool strict> 83 forceinline 84 Map<I,M,strict>::Map(void) {} 85 86 template<class I, class M, bool strict> 87 forceinline 88 Map<I,M,strict>::Map(I& i0) : i(i0) {} 89 90 template<class I, class M, bool strict> 91 forceinline 92 Map<I,M,strict>::Map(I& i0, const M& m0) : i(i0), m(m0) {} 93 94 template<class I, class M, bool strict> 95 forceinline void 96 Map<I,M,strict>::init(I& i0) { 97 i=i0; 98 } 99 100 template<class I, class M, bool strict> 101 forceinline void 102 Map<I,M,strict>::init(I& i0, const M& m0) { 103 i=i0; m=m0; 104 } 105 106 template<class I, class M, bool strict> 107 forceinline void 108 Map<I,M,strict>::operator ++(void) { 109 if (strict) { 110 ++i; 111 } else { 112 int n=m.val(i.val()); 113 do { 114 ++i; 115 } while (i() && (n == m.val(i.val()))); 116 } 117 } 118 template<class I, class M, bool strict> 119 forceinline bool 120 Map<I,M,strict>::operator ()(void) const { 121 return i(); 122 } 123 124 template<class I, class M, bool strict> 125 forceinline int 126 Map<I,M,strict>::val(void) const { 127 return m.val(i.val()); 128 } 129 130}}} 131 132// STATISTICS: iter-any