Generated on Mon Jul 6 18:09:09 2009 for Gecode by doxygen 1.5.9

bitset.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *
00009  *  Copyright:
00010  *     Mikael Lagerkvist, 2007
00011  *     Christian Schulte, 2007
00012  *
00013  *  Last modified:
00014  *     $Date: 2009-01-20 23:44:27 +0100 (Tue, 20 Jan 2009) $ by $Author: schulte $
00015  *     $Revision: 8082 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 #include <climits>
00043 
00044 namespace Gecode { namespace Int { namespace Extensional {
00045 
00047   class BitSet {
00049     typedef unsigned int Base;
00051     Base* data;
00053     unsigned int size;
00054   public:
00056     BitSet(void);
00058     BitSet(Space& home, int s, bool value = false);
00060     BitSet(Space& home, const BitSet& bs);
00062     void init(Space& home, int s, bool value=false);
00064     bool get(unsigned int i);
00066     void set(unsigned int i, bool value=true);
00067   };
00068 
00069   forceinline void
00070   BitSet::init(Space& home, int s, bool value) {
00071     size = static_cast<int>(std::ceil(static_cast<double>(s)
00072                                       /(CHAR_BIT*sizeof(Base))));
00073     data = home.alloc<Base>(size);
00074     Base ival = value ? ~0 : 0;
00075     for (int i = size; i--; ) data[i] = ival;
00076   }
00077 
00078   forceinline
00079   BitSet::BitSet(void) : data(NULL), size(0) {}
00080 
00081   forceinline
00082   BitSet::BitSet(Space& home, int s, bool value)
00083     : data(NULL), size(0) {
00084     init(home, s, value);
00085   }
00086   forceinline
00087   BitSet::BitSet(Space& home, const BitSet& bs)
00088     : data(home.alloc<Base>(bs.size)), size(bs.size) {
00089     for (int i = size; i--; ) data[i] = bs.data[i];
00090   }
00091   forceinline bool
00092   BitSet::get(unsigned int i) {
00093     unsigned int pos = i / (sizeof(Base)*CHAR_BIT);
00094     unsigned int bit = i % (sizeof(Base)*CHAR_BIT);
00095     assert(pos < size);
00096     return data[pos] & ((Base)1 << bit);
00097   }
00098   forceinline void
00099   BitSet::set(unsigned int i, bool value) {
00100     unsigned int pos = i / (sizeof(Base)*CHAR_BIT);
00101     unsigned int bit = i % (sizeof(Base)*CHAR_BIT);
00102     assert(pos < size);
00103     if (value)
00104       data[pos] |= 1 << bit;
00105     else
00106       data[pos] &= ~(1 << bit);
00107   }
00108 
00109 }}}
00110 
00111 // STATISTICS: int-other
00112