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