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 the union of two value iterators 38 * 39 * \ingroup FuncIterValues 40 */ 41 42 template<class I, class J> 43 class Union { 44 protected: 45 /// First iterator 46 I i; 47 /// Second iterator 48 J j; 49 /// Current value 50 int v; 51 /// Whether iterator is done 52 bool done; 53 public: 54 /// \name Constructors and initialization 55 //@{ 56 /// Default constructor 57 Union(void); 58 /// Initialize with values from \a i and \a j 59 Union(I& i, J& j); 60 /// Initialize with values from \a i and \a j 61 void init(I& i, J& j); 62 //@} 63 64 /// \name Iteration control 65 //@{ 66 /// Test whether iterator is still at a value or done 67 bool operator ()(void) const; 68 /// Move iterator to next value (if possible) 69 void operator ++(void); 70 //@} 71 72 /// \name Value access 73 //@{ 74 /// Return current value 75 int val(void) const; 76 //@} 77 }; 78 79 80 template<class I, class J> 81 forceinline 82 Union<I,J>::Union(void) : v(0) {} 83 84 template<class I, class J> 85 forceinline void 86 Union<I,J>::operator ++(void) { 87 if (i()) { 88 if (j()) { 89 if (i.val() == j.val()) { 90 v=i.val(); ++i; ++j; 91 } else if (i.val() < j.val()) { 92 v=i.val(); ++i; 93 } else { 94 v=j.val(); ++j; 95 } 96 } else { 97 v=i.val(); ++i; 98 } 99 } else if (j()) { 100 v=j.val(); ++j; 101 } else { 102 done=true; 103 } 104 } 105 106 template<class I, class J> 107 inline void 108 Union<I,J>::init(I& i0, J& j0) { 109 i=i0; j=j0; v=0; done=false; 110 operator ++(); 111 } 112 113 template<class I, class J> 114 forceinline 115 Union<I,J>::Union(I& i0, J& j0) : i(i0), j(j0), v(0), done(false) { 116 operator ++(); 117 } 118 119 template<class I, class J> 120 forceinline bool 121 Union<I,J>::operator ()(void) const { 122 return !done; 123 } 124 125 template<class I, class J> 126 forceinline int 127 Union<I,J>::val(void) const { 128 return v; 129 } 130 131}}} 132 133// STATISTICS: iter-any