Project
Loading...
Searching...
No Matches
BitSet.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_MCH_RAW_BITSET_H
13#define O2_MCH_RAW_BITSET_H
14
15#include <cstdlib>
16#include <cstdint>
17#include <gsl/span>
18#include <iostream>
19#include <string>
20#include <string_view>
21#include <vector>
22
23namespace o2
24{
25namespace mch
26{
27namespace raw
28{
29
30class BitSet
31{
32
33 public:
34 BitSet();
35
36 // construct a BitSet using a string composed of '0' (meaning
37 // bit unset) and '1' (meaning bit set) characters
38 // the length of the resulting bitset is that of the string.
39 explicit BitSet(std::string_view s);
40
42 // construct a bitset initialized with the x-bits value v
43 explicit BitSet(uint8_t v, int n = -1);
44 explicit BitSet(uint16_t v, int n = -1);
45 explicit BitSet(uint32_t v, int n = -1);
46 explicit BitSet(uint64_t v, int n = -1);
48
49 public:
50 // check equality
51 bool operator==(const BitSet& rhs) const;
52 bool operator!=(const BitSet& rhs) const;
53
54 // any returns true if any of the bits is set
55 bool any() const;
56
57 // appends a bit at the current position (i.e. len-1)
58 int append(bool val);
59
61 // appends the n first bits from a x-bits word.
62 // if n is < 0 it is computed for val (using log2(val)+1)
63 // otherwise it should be >= log2(val)+1 and <=x
64 // and the exact number of specified bits will be set
65 // (to 0 or 1).
66 // returns the number of bits actually added
67 int append(uint8_t val, int n = -1);
68 int append(uint16_t val, int n = -1);
69 int append(uint32_t val, int n = -1);
70 int append(uint64_t val, int n = -1);
72
73 // count returns the number of bits set at 1
74 int count() const;
75
76 // sets all the bits to false (i.e. resets)
77 void clear();
78
79 // returns true if we hold not bit at all
80 bool isEmpty() const { return len() == 0; }
81
82 // sets the value of the bit at given pos
83 void set(int pos, bool val);
84
85 // gets the value of the bit at given pos
86 bool get(int pos) const;
87
88 // grows the BitSet so it can accomodate at least n bits. Returns true if size changed.
89 bool grow(int n);
90
91 // last returns a bitset containing the last n bits of the bitset
92 // if there's not enough bits, throw an exception
93 BitSet last(int n) const;
94
95 // return the max number of bits this object can currently hold
96 // it is a multiple of 8.
97 int size() const { return mSize; }
98
99 // return the max number of bits any bitset can hold
100 static int maxSize() { return 2 * 8192; }
101
102 // return the number of bits we are current holding
103 int len() const { return mLen; }
104
105 // return the max number of bits we've ever held
106 int maxlen() const { return mMaxLen; }
107
108 // pruneFirst removes the first n bits from the bitset
109 void pruneFirst(int n);
110
111 void setFast(int pos, bool val);
112
113 void setFromBytes(gsl::span<uint8_t> bytes);
114
115 // setRangeFromString populates the bits at indice [a,b] (inclusive range)
116 // from the characters in the string: 0 to unset the bit (=false)
117 // or 1 to set the bit (=true).
118 // A string containing anything else than '0' or '1' is invalid and
119 // triggers an exception
120 void setRangeFromString(int a, int b, std::string_view s);
121
123 // setRangeFromUint(a,b,uintX_t) populates the bits at indices [a,b] (inclusive range)
124 // with the bits of value v. b-a must be <=X otherwise throws an exception
125 void setRangeFromUint(int a, int b, uint8_t v);
126 void setRangeFromUint(int a, int b, uint16_t v);
127 void setRangeFromUint(int a, int b, uint32_t v);
128 void setRangeFromUint(int a, int b, uint64_t v);
130
131 // returns a textual representation of the BitSet
132 // where the LSB is on the left
133 std::string stringLSBLeft() const;
134
135 // returns a textual representation of the BitSet
136 // where the LSB is on the right
137 std::string stringLSBRight() const;
138
139 // subset returns a subset of the bitset.
140 // subset is not a slice (i.e. not a reference, but a copy of the internals)
141 // [a,b] inclusive
142 BitSet subset(int a, int b) const;
143
144 // uint8 converts the bit set into a 8-bits value, if possible.
145 // if b is negative, it is set to the bitset length
146 uint8_t uint8(int a, int b) const;
147
148 // uint16 converts the bit set into a 16-bits value, if possible.
149 // if b is negative, it is set to the bitset length
150 uint16_t uint16(int a, int b) const;
151
152 // uint32 converts the bit set into a 32-bits value, if possible.
153 // if b is negative, it is set to the bitset length
154 uint32_t uint32(int a, int b) const;
155
156 // uint64 converts the bit set into a 64-bits value, if possible.
157 // if b is negative, it is set to the bitset length
158 uint64_t uint64(int a, int b) const;
159
160 private:
161 int mSize; // max number of bits we can hold
162 int mLen; // actual number of bits we are holding
163 int mMaxLen; // the max number of bits we've ever held
164 std::vector<uint8_t> mBytes;
165 static int nofInstances;
166};
167
168int circularAppend(BitSet& bs, const BitSet& ringBuffer, int startBit, int n);
169
170std::ostream& operator<<(std::ostream& os, const BitSet& bs);
171
172std::string compactString(const BitSet& bs);
173} // namespace raw
174} // namespace mch
175} // namespace o2
176
177#endif
uint16_t pos
Definition RawData.h:3
uint8_t uint8(int a, int b) const
Definition BitSet.cxx:397
uint16_t uint16(int a, int b) const
Definition BitSet.cxx:402
uint64_t uint64(int a, int b) const
Definition BitSet.cxx:412
bool grow(int n)
Definition BitSet.cxx:234
void pruneFirst(int n)
Definition BitSet.cxx:266
void setFromBytes(gsl::span< uint8_t > bytes)
Definition BitSet.cxx:307
static int maxSize()
Definition BitSet.h:100
BitSet last(int n) const
Definition BitSet.cxx:254
bool operator!=(const BitSet &rhs) const
Definition BitSet.cxx:167
std::string stringLSBLeft() const
Definition BitSet.cxx:357
void setRangeFromString(int a, int b, std::string_view s)
Definition BitSet.cxx:320
void setFast(int pos, bool val)
Definition BitSet.cxx:288
void setRangeFromUint(int a, int b, uint8_t v)
Definition BitSet.cxx:337
int maxlen() const
Definition BitSet.h:106
int append(bool val)
Definition BitSet.cxx:172
bool operator==(const BitSet &rhs) const
Definition BitSet.cxx:154
bool any() const
Definition BitSet.cxx:198
int len() const
Definition BitSet.h:103
bool get(int pos) const
Definition BitSet.cxx:225
uint32_t uint32(int a, int b) const
Definition BitSet.cxx:407
BitSet subset(int a, int b) const
Definition BitSet.cxx:383
bool isEmpty() const
Definition BitSet.h:80
int count() const
Definition BitSet.cxx:214
void set(int pos, bool val)
Definition BitSet.cxx:277
int size() const
Definition BitSet.h:97
std::string stringLSBRight() const
Definition BitSet.cxx:370
GLdouble n
Definition glcorearb.h:1982
const GLdouble * v
Definition glcorearb.h:832
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLuint GLfloat * val
Definition glcorearb.h:1582
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
int circularAppend(BitSet &bs, const BitSet &ringBuffer, int startBit, int n)
Definition BitSet.cxx:421
std::string compactString(const BitSet &bs)
Definition BitSet.cxx:440
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::ostream & operator<<(std::ostream &stream, o2::InteractionRecord const &ir)