Generated on Thu Apr 11 13:59:20 2019 for Gecode by doxygen 1.6.3

bitset-offset.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christopher Mears <chris.mears@monash.edu>
00005  *
00006  *  Contributing authors:
00007  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00008  *     Christian Schulte <schulte@gecode.org>
00009  *
00010  *  Copyright:
00011  *     Mikael Lagerkvist, 2007
00012  *     Christopher Mears, 2012
00013  *     Christian Schulte, 2007
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <climits>
00041 #include <cmath>
00042 #include <iostream>
00043 
00044 namespace Gecode { namespace Support {
00045 
00050   template<class A>
00051   class BitSetOffset : public BitSetBase {
00052   protected:
00054     A& a;
00056     int _offset;
00057   public:
00059     BitSetOffset(A& a, unsigned int s, int o);
00061     BitSetOffset(A& a, const BitSetOffset& bs);
00063     ~BitSetOffset(void);
00064 
00065     // As for the ordinary bitset, most operations can be inherited
00066     // directly from BitSetBase.  We only modify the operations that
00067     // involve indices or the offset itself.
00068 
00070     bool get(int i) const;
00072     void set(int i);
00074     void clear(int i);
00076     int next(int i) const;
00078     void resize(A& a, unsigned int n, int offset, bool set=false);
00079 
00081     int offset(void) const;
00083     int max_bit(void) const;
00085     bool valid(int i) const;
00086   };
00087 
00088   template<class A>
00089   forceinline
00090   BitSetOffset<A>::BitSetOffset(A& a0, unsigned int s, int o)
00091     : BitSetBase(a0,s), a(a0), _offset(o) {}
00092 
00093   template<class A>
00094   forceinline
00095   BitSetOffset<A>::BitSetOffset(A& a0, const BitSetOffset<A>& bs)
00096     : BitSetBase(a0,bs), a(a0), _offset(bs._offset) {}
00097 
00098   template<class A>
00099   forceinline
00100   BitSetOffset<A>::~BitSetOffset(void) {
00101     dispose(a);
00102   }
00103 
00104   template<class A>
00105   forceinline bool
00106   BitSetOffset<A>::get(int i) const { return BitSetBase::get(i-_offset); }
00107 
00108   template<class A>
00109   forceinline void
00110   BitSetOffset<A>::set(int i) { BitSetBase::set(i-_offset); }
00111 
00112   template<class A>
00113   forceinline void
00114   BitSetOffset<A>::clear(int i) { BitSetBase::clear(i-_offset); }
00115 
00116   template<class A>
00117   forceinline int
00118   BitSetOffset<A>::next(int i) const { return _offset + BitSetBase::next(i-_offset); }
00119 
00120   template<class A>
00121   void
00122   BitSetOffset<A>::resize(A& a, unsigned int n, int offset, bool set) {
00123     BitSetBase::resize(a, n, set);
00124     _offset = offset;
00125   }
00126 
00127   template<class A>
00128   forceinline int
00129   BitSetOffset<A>::offset(void) const { return _offset; }
00130 
00131   template<class A>
00132   forceinline int
00133   BitSetOffset<A>::max_bit(void) const { return _offset + size() - 1; }
00134 
00135   template<class A>
00136   forceinline bool
00137   BitSetOffset<A>::valid(int i) const { return _offset <= i && i <= _offset + (int)size() - 1; }
00138 
00139   template <class A, class Char, class Traits>
00140   std::basic_ostream<Char,Traits>&
00141   operator <<(std::basic_ostream<Char,Traits>& os, const BitSetOffset<A>& bs) {
00142     for (int i = bs.offset() ; i < bs.offset()+static_cast<int>(bs.size()) ; i++)
00143       if (bs.get(i))
00144         os << i << " ";
00145     return os;
00146   }
00147 
00148 }}
00149 
00150 // STATISTICS: support-any
00151