File indexing completed on 2025-01-18 09:55:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef DDSEGMENTATION_BITFIELDCODER_H
0013 #define DDSEGMENTATION_BITFIELDCODER_H 1
0014
0015 #include <map>
0016 #include <string>
0017 #include <vector>
0018 #include <sstream>
0019 #include <cstdint>
0020
0021 namespace dd4hep {
0022
0023 namespace DDSegmentation {
0024
0025 typedef int64_t FieldID;
0026 typedef uint64_t CellID;
0027 typedef uint64_t VolumeID;
0028
0029 class StringTokenizer ;
0030
0031
0032 class BitFieldElement {
0033
0034 public :
0035
0036 BitFieldElement() = default ;
0037
0038 BitFieldElement(const BitFieldElement&) = default ;
0039
0040 BitFieldElement(BitFieldElement&&) = default ;
0041
0042
0043
0044
0045
0046
0047 BitFieldElement( const std::string& name, unsigned offset, int signedWidth ) ;
0048
0049 ~BitFieldElement() = default ;
0050
0051
0052 BitFieldElement& operator=(const BitFieldElement&) = default ;
0053
0054
0055 FieldID value(CellID bitfield) const;
0056
0057
0058 void set(CellID& bitfield, FieldID value) const ;
0059
0060
0061 const std::string& name() const { return _name ; }
0062
0063
0064 unsigned offset() const { return _offset ; }
0065
0066
0067 unsigned width() const { return _width ; }
0068
0069
0070 bool isSigned() const { return _isSigned ; }
0071
0072
0073 CellID mask() const { return _mask ; }
0074
0075
0076 int minValue() const { return _minVal; }
0077
0078
0079 int maxValue() const { return _maxVal; }
0080
0081 protected:
0082
0083 CellID _mask {};
0084 unsigned _offset {};
0085 unsigned _width {};
0086 int _minVal {};
0087 int _maxVal {};
0088 bool _isSigned {};
0089 std::string _name;
0090 };
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 class BitFieldCoder{
0115 public :
0116 typedef std::map<std::string, unsigned int> IndexMap ;
0117
0118 public :
0119
0120 BitFieldCoder() = default ;
0121
0122 BitFieldCoder(const BitFieldCoder&) = default ;
0123
0124 BitFieldCoder(BitFieldCoder&&) = default ;
0125
0126 ~BitFieldCoder() = default ;
0127
0128
0129 BitFieldCoder& operator=(const BitFieldCoder&) = default ;
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 BitFieldCoder( const std::string& initString ) : _joined(0){
0146 init( initString ) ;
0147 }
0148
0149
0150
0151 static CellID toLong(unsigned low_Word, unsigned high_Word ) {
0152 return ( ( low_Word & 0xffffffffULL ) | ( ( high_Word & 0xffffffffULL ) << 32 ) ) ;
0153 }
0154
0155
0156
0157 static unsigned lowWord(CellID bitfield) { return unsigned( bitfield & 0xffffFFFFUL ); }
0158
0159
0160
0161 static unsigned highWord(CellID bitfield) { return unsigned( bitfield >> 32); }
0162
0163
0164
0165 FieldID get(CellID bitfield, size_t idx) const {
0166 return _fields.at(idx).value( bitfield ) ;
0167 }
0168
0169
0170
0171 FieldID get(CellID bitfield, const std::string& name) const {
0172 return _fields.at( index( name ) ).value( bitfield ) ;
0173 }
0174
0175
0176
0177 void set(CellID& bitfield, size_t idx, FieldID value) const {
0178 _fields.at(idx).set( bitfield , value ) ;
0179 }
0180
0181
0182
0183 void set(CellID& bitfield, const std::string& name, FieldID value) const {
0184 _fields.at( index( name ) ).set( bitfield, value ) ;
0185 }
0186
0187
0188
0189 unsigned highestBit() const ;
0190
0191
0192 size_t size() const { return _fields.size() ; }
0193
0194
0195
0196 size_t index( const std::string& name) const ;
0197
0198
0199
0200 const BitFieldElement& operator[](const std::string& name) const {
0201 return _fields[ index( name ) ] ;
0202 }
0203
0204
0205
0206 const BitFieldElement& operator[](unsigned idx) const {
0207 return _fields[ idx ] ;
0208 }
0209
0210
0211
0212 std::string fieldDescription() const ;
0213
0214
0215
0216 std::string valueString(CellID bitfield) const ;
0217
0218 const std::vector<BitFieldElement>& fields() const {
0219 return _fields;
0220 }
0221
0222
0223 CellID mask() const { return _joined ; }
0224
0225 protected:
0226
0227
0228
0229 void addField( const std::string& name, unsigned offset, int width );
0230
0231
0232
0233
0234 void init( const std::string& initString) ;
0235
0236 protected:
0237
0238 std::vector<BitFieldElement> _fields{} ;
0239 IndexMap _map{} ;
0240 CellID _joined{} ;
0241 };
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253 class StringTokenizer{
0254 std::vector< std::string >& _tokens ;
0255 char _del ;
0256 char _last ;
0257
0258 public:
0259
0260
0261 StringTokenizer( std::vector< std::string >& tokens, char del )
0262 : _tokens(tokens)
0263 , _del(del),
0264 _last(del) {
0265 }
0266
0267
0268 void operator()(const char& c) {
0269 if( c != _del ) {
0270 if( _last == _del ) {
0271 _tokens.emplace_back("") ;
0272 }
0273 _tokens.back() += c ;
0274 }
0275 _last = c ;
0276 }
0277 };
0278
0279 }
0280 }
0281 #endif
0282
0283
0284
0285