Generated on Mon Aug 25 11:35:37 2008 for Gecode by doxygen 1.5.6

bitset.icc

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: 2007-12-10 08:54:45 +0100 (Mon, 10 Dec 2007) $ by $Author: schulte $
00015  *     $Revision: 5650 $
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 = static_cast<Base*>(home->alloc(size*sizeof(Base)));
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(static_cast<Base*>(home->alloc(bs.size*sizeof(Base)))), 
00089       size(bs.size) {
00090     for (int i = size; i--; ) data[i] = bs.data[i];
00091   }
00092   forceinline bool
00093   BitSet::get(unsigned int i) {
00094     unsigned int pos = i / (sizeof(Base)*CHAR_BIT);
00095     unsigned int bit = i % (sizeof(Base)*CHAR_BIT);
00096     assert(pos < size);
00097     return data[pos] & ((Base)1 << bit);
00098   }
00099   forceinline void
00100   BitSet::set(unsigned int i, bool value) {
00101     unsigned int pos = i / (sizeof(Base)*CHAR_BIT);
00102     unsigned int bit = i % (sizeof(Base)*CHAR_BIT);
00103     assert(pos < size);
00104     if (value)
00105       data[pos] |= 1 << bit;
00106     else
00107       data[pos] &= ~(1 << bit);
00108   }
00109 
00110 }}}
00111 
00112 // STATISTICS: int-other
00113