values-list.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2010 00008 * 00009 * Last modified: 00010 * $Date: 2010-07-28 17:58:23 +0200 (Wed, 28 Jul 2010) $ by $Author: tack $ 00011 * $Revision: 11296 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { namespace Iter { namespace Values { 00039 00045 class ValueListIter { 00046 protected: 00048 class ValueList : public Support::BlockClient<ValueList,Region> { 00049 public: 00051 int val; 00053 ValueList* next; 00054 }; 00056 class VLIO : public Support::BlockAllocator<ValueList,Region> { 00057 public: 00059 unsigned int use_cnt; 00061 VLIO(Region& r); 00062 }; 00064 VLIO* vlio; 00066 ValueList* h; 00068 ValueList* c; 00070 void set(ValueList* l); 00071 public: 00073 00074 00075 ValueListIter(void); 00077 ValueListIter(const ValueListIter& i); 00079 ValueListIter(Region& r); 00081 void init(Region& r); 00083 ValueListIter& operator =(const ValueListIter& i); 00085 00087 00088 00089 bool operator ()(void) const; 00091 void operator ++(void); 00093 void reset(void); 00095 00097 00098 00099 int val(void) const; 00101 00103 ~ValueListIter(void); 00104 }; 00105 00106 00107 forceinline 00108 ValueListIter::VLIO::VLIO(Region& r) 00109 : Support::BlockAllocator<ValueList,Region>(r), use_cnt(1) {} 00110 00111 00112 forceinline 00113 ValueListIter::ValueListIter(void) 00114 : vlio(NULL) {} 00115 00116 forceinline 00117 ValueListIter::ValueListIter(Region& r) 00118 : vlio(new (r.ralloc(sizeof(VLIO))) VLIO(r)), 00119 h(NULL), c(NULL) {} 00120 00121 forceinline void 00122 ValueListIter::init(Region& r) { 00123 vlio = new (r.ralloc(sizeof(VLIO))) VLIO(r); 00124 h = c = NULL; 00125 } 00126 00127 forceinline 00128 ValueListIter::ValueListIter(const ValueListIter& i) 00129 : vlio(i.vlio), h(i.h), c(i.c) { 00130 vlio->use_cnt++; 00131 } 00132 00133 forceinline ValueListIter& 00134 ValueListIter::operator =(const ValueListIter& i) { 00135 if (&i != this) { 00136 if (--vlio->use_cnt == 0) { 00137 Region& r = vlio->allocator(); 00138 vlio->~VLIO(); 00139 r.rfree(vlio,sizeof(VLIO)); 00140 } 00141 vlio = i.vlio; 00142 vlio->use_cnt++; 00143 c=i.c; h=i.h; 00144 } 00145 return *this; 00146 } 00147 00148 forceinline 00149 ValueListIter::~ValueListIter(void) { 00150 if (--vlio->use_cnt == 0) { 00151 Region& r = vlio->allocator(); 00152 vlio->~VLIO(); 00153 r.rfree(vlio,sizeof(VLIO)); 00154 } 00155 } 00156 00157 00158 forceinline void 00159 ValueListIter::set(ValueList* l) { 00160 h = c = l; 00161 } 00162 00163 forceinline bool 00164 ValueListIter::operator ()(void) const { 00165 return c != NULL; 00166 } 00167 00168 forceinline void 00169 ValueListIter::operator ++(void) { 00170 c = c->next; 00171 } 00172 00173 forceinline void 00174 ValueListIter::reset(void) { 00175 c = h; 00176 } 00177 00178 forceinline int 00179 ValueListIter::val(void) const { 00180 return c->val; 00181 } 00182 00183 }}} 00184 00185 // STATISTICS: iter-any 00186