LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Bitset.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
33 #ifndef LSST_AP_BITSET_H
34 #define LSST_AP_BITSET_H
35 
36 #include <cassert>
37 #include <cstring>
38 
39 #include "boost/static_assert.hpp"
40 
41 #include "Common.h"
42 
43 
44 namespace lsst { namespace ap { namespace detail {
45 
46 template <typename WordT> struct BitTraits {
47  static bool const IS_SPECIALIZED = false;
48 };
49 
50 template <> struct BitTraits<uint8_t> {
51  static bool const IS_SPECIALIZED = true;
52  static int const BITS_PER_WORD_LOG2 = 3;
53  static int const BITS_PER_WORD = 8;
54  static boost::uint8_t const WORD_MASK = 0xff;
55 };
56 
57 template <> struct BitTraits<uint16_t> {
58  static bool const IS_SPECIALIZED = true;
59  static int const BITS_PER_WORD_LOG2 = 4;
60  static int const BITS_PER_WORD = 16;
61  static boost::uint16_t const WORD_MASK = 0xffff;
62 };
63 
64 template <> struct BitTraits<uint32_t> {
65  static bool const IS_SPECIALIZED = true;
66  static int const BITS_PER_WORD_LOG2 = 5;
67  static int const BITS_PER_WORD = 32;
68  static boost::uint32_t const WORD_MASK = 0xffffffff;
69 };
70 
71 template <> struct BitTraits<uint64_t> {
72  static bool const IS_SPECIALIZED = true;
73  static int const BITS_PER_WORD_LOG2 = 6;
74  static int const BITS_PER_WORD = 64;
75  static boost::uint64_t const WORD_MASK = UINT64_C(0xffffffffffffffff);
76 };
77 
78 template <typename WordT>
79 inline int wordForBit(int const i) {
81 }
82 
83 template <typename WordT>
84 inline WordT maskForBit(int const i) {
85  return static_cast<WordT>(1) << (i & (BitTraits<WordT>::BITS_PER_WORD - 1));
86 }
87 
88 template <typename WordT>
89 bool setBits(
90  int * const indexes,
91  WordT * const words,
92  int const numBitsToSet,
93  int const numBits
94 );
95 
96 template <typename WordT>
97 void resetBits(
98  WordT * const words,
99  int const * const indexes,
100  int const numBitsToReset,
101  int const numBits
102 );
103 
104 } // end of namespace detail
105 
106 
108 template <typename WordT, int NumBits>
109 class Bitset {
110 
111 private :
112 
113  BOOST_STATIC_ASSERT(NumBits > 0);
115 
116 public :
117 
118  static int const NUM_BITS = NumBits;
119  static int const NUM_WORDS = (NumBits + (detail::BitTraits<WordT>::BITS_PER_WORD - 1)) >>
121 
123  void reset() {
124  std::memset(_bits, 0, sizeof(_bits));
125  }
126 
128  void set() {
129  std::memset(_bits, -1, sizeof(_bits));
130  }
131 
133  void reset(int const i) {
134  assert(i >= 0 && i < NumBits);
135  _bits[detail::wordForBit<WordT>(i)] &= ~ detail::maskForBit<WordT>(i);
136  }
137 
139  void set(int const i) {
140  assert(i >= 0 && i < NumBits);
141  _bits[detail::wordForBit<WordT>(i)] |= detail::maskForBit<WordT>(i);
142  }
143 
145  void set(int const i, bool const on) {
146  if (on) {
147  set(i);
148  } else {
149  reset(i);
150  }
151  }
152 
154  bool test(int const i) const {
155  assert(i >= 0 && i < NumBits);
156  return _bits[detail::wordForBit<WordT>(i)] & detail::maskForBit<WordT>(i);
157  }
158 
163  bool set(int * const indexes, int const numBits) {
164  return detail::setBits<WordT>(indexes, _bits, numBits, NumBits);
165  }
166 
168  void reset(int const * const indexes, int const numBits) {
169  detail::resetBits<WordT>(_bits, indexes, numBits, NumBits);
170  }
171 
172 private :
173 
174  WordT _bits[NUM_WORDS];
175 };
176 
177 
178 }} // end of namespace lsst::ap
179 
180 #endif // LSST_AP_BITSET_H
BOOST_STATIC_ASSERT(NumBits > 0)
int wordForBit(int const i)
Definition: Bitset.h:79
WordT maskForBit(int const i)
Definition: Bitset.h:84
void set(int const i, bool const on)
Definition: Bitset.h:145
static int const NUM_BITS
Definition: Bitset.h:118
A fixed size set of bits.
Definition: Bitset.h:109
void reset()
Definition: Bitset.h:123
void set(int const i)
Definition: Bitset.h:139
WordT _bits[NUM_WORDS]
Definition: Bitset.h:174
void set()
Definition: Bitset.h:128
Master header file for the association pipeline.
void resetBits(WordT *const words, int const *const indexes, int const numBitsToReset, int const numBits)
Definition: Bitset.cc:212
bool setBits(int *const indexes, WordT *const words, int const numBitsToSet, int const numBits)
Definition: Bitset.cc:149
static bool const IS_SPECIALIZED
Definition: Bitset.h:47
void reset(int const *const indexes, int const numBits)
Definition: Bitset.h:168
bool test(int const i) const
Definition: Bitset.h:154
void reset(int const i)
Definition: Bitset.h:133
bool set(int *const indexes, int const numBits)
Definition: Bitset.h:163
static int const NUM_WORDS
Definition: Bitset.h:119