File: split_bitstring.h

package info (click to toggle)
libcds 2.3.3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,632 kB
  • sloc: cpp: 135,002; ansic: 7,234; perl: 243; sh: 237; makefile: 6
file content (445 lines) | stat: -rw-r--r-- 14,526 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSLIB_ALGO_SPLIT_BITSTRING_H
#define CDSLIB_ALGO_SPLIT_BITSTRING_H

#include <cds/algo/base.h>

namespace cds { namespace algo {

    /// Cuts a bit sequence from fixed-size bit-string
    /**
        The splitter can be used as an iterator over bit-string.
        Each call of \p cut() or \p safe_cut() cuts the bit count specified
        and keeps the position inside bit-string for the next call.

        The splitter stores a const reference to bit-string, not a copy.
        The maximum count of bits that can be cut in a single call is <tt> sizeof(UInt) * 8 </tt>

        The splitter keeps byte order.

        Template parameters:
        - \p BitString - a fixed-sized type that interprets as bit string
        - \p BitStringSize - the size of \p BitString in bytes, default is <tt>sizeof( BitString )</tt>.
             You can specify 0 for default.
        - \p UInt - an unsigned integer, return type for \p cut(), default is \p unsigned

        There are specialized splitters:
        - a simplified \p byte_splitter algorithm that is suitable when count is multiple of 8.
        - \p number_splitter algorithm is suitable for a number
    */
    template <typename BitString, size_t BitStringSize = sizeof( BitString ), typename UInt = unsigned >
    class split_bitstring
    {
    public:
        typedef BitString bitstring;    ///< Bit-string type
        typedef UInt      uint_type;    ///< Result type of \p cut() function
        static constexpr size_t const c_bitstring_size = BitStringSize ? BitStringSize : sizeof( BitString ); ///< size of \p BitString in bytes

        //@cond
        static constexpr unsigned const c_nBitPerByte = 8;
        //@endcond

    public:
        /// Initializises the splitter with reference to \p h and zero start bit offset
        explicit split_bitstring( bitstring const& h )
            : cur_( reinterpret_cast<uint8_t const*>( &h ))
            , offset_( 0 )
            , first_( cur_ )
            , last_( cur_ + c_bitstring_size )
        {}

        /// Initializises the splitter with reference to \p h and start bit offset \p nBitOffset
        split_bitstring( bitstring const& h, size_t nBitOffset )
            : cur_( reinterpret_cast<uint8_t const*>( &h ) + nBitOffset / c_nBitPerByte )
            , offset_( nBitOffset % c_nBitPerByte  )
            , first_( reinterpret_cast<uint8_t const*>( &h ))
            , last_( first_ + c_bitstring_size )
        {}

        /// Returns \p true if end-of-string is not reached yet
        explicit operator bool() const
        {
            return !eos();
        }

        /// Returns \p true if end-of-stream encountered
        bool eos() const
        {
            return cur_ >= last_;
        }

        /// Cuts next \p count bits from bit-string
        /**
            For performance reason, the function does not manage out-of-bound condition.
            To control that use \p safe_cut().
        */
        uint_type cut( unsigned count )
        {
            assert( !eos());

            uint_type result = 0;
#       if defined( CDS_ARCH_LITTLE_ENDIAN )
            for ( unsigned done = 0; done < count; ) {
                assert( cur_ < last_ );
                unsigned bits = count - done;
                if ( bits > c_nBitPerByte - offset_ )
                    bits = c_nBitPerByte - offset_;

                result |= static_cast<uint_type>(( *cur_ >> offset_ ) & (( 1 << bits ) - 1 )) << done;

                offset_ += bits;
                assert( offset_ <= c_nBitPerByte );
                if ( offset_ == c_nBitPerByte ) {
                    offset_ = 0;
                    ++cur_;
                }
                done += bits;
            }
#       else
            while ( count ) {
                assert( cur_ < last_ );

                unsigned bits = count <= ( c_nBitPerByte - offset_ ) ? count : c_nBitPerByte - offset_;

                result = ( result << bits ) | (( *cur_ >> offset_ ) & ( ( 1 << bits ) - 1 ));

                offset_ += bits;
                assert( offset_ <= c_nBitPerByte );
                if ( offset_ == c_nBitPerByte ) {
                    offset_ = 0;
                    ++cur_;
                }
                count -= bits;
            }
#       endif

            return result;
        }

        /// Cuts up to \p count from the bit-string
        /**
            Safe analog of \p cut() but if \p count is more than the rest of bit-string,
            only the rest is returned.
            When \p eos() condition is met the function returns 0.
        */
        uint_type safe_cut( unsigned count )
        {
            if ( eos())
                return 0;

            unsigned const rest = static_cast<unsigned>( last_ - cur_ - 1 ) * c_nBitPerByte + ( c_nBitPerByte - offset_ );
            if ( rest < count )
                count = rest;
            return count ? cut( count ) : 0;
        }

        /// Resets the splitter
        void reset() noexcept
        {
            cur_ = first_;
            offset_ = 0;
        }

        /// Returns pointer to source bitstring
        bitstring const * source() const
        {
            return reinterpret_cast<bitstring const *>( first_ );
        }

        /// Returns current bit offset from beginning of bit-string
        size_t bit_offset() const
        {
            return offset_ + (cur_ - first_) * c_nBitPerByte;
        }

        /// Returns how many bits remain
        size_t rest_count() const
        {
            return c_bitstring_size * c_nBitPerByte - bit_offset();
        }

        /// Returns \p true for any argument
        static constexpr bool is_correct( unsigned /*count*/ )
        {
            return true;
        }

    private:
        //@cond
        uint8_t const*  cur_;
        unsigned        offset_;
        uint8_t const* const    first_;
        uint8_t const* const    last_;
        //@endcond
    };

    /// Simplified \p split_bitstring algorithm when \p count is multiple of 8
    template <typename BitString, size_t BitStringSize = sizeof( BitString ), typename UInt = unsigned >
    class byte_splitter
    {
    public:
        typedef BitString bitstring;    ///< Bit-string type
        typedef UInt      uint_type;    ///< Result type of \p cut() function
        static constexpr size_t const c_bitstring_size = BitStringSize ? BitStringSize : sizeof( BitString ); ///< size of \p BitString in bytes

        //@cond
        static constexpr unsigned const c_nBitPerByte = 8;
        //@endcond

    public:
        /// Initializises the splitter with reference to \p h and zero start bit offset
        explicit byte_splitter( bitstring const& h )
            : cur_( reinterpret_cast<uint8_t const*>( &h ))
            , first_( cur_ )
            , last_( cur_ + c_bitstring_size )
        {}

        /// Initializises the splitter with reference to \p h and start bit offset \p nBitOffset
        byte_splitter( bitstring const& h, size_t nBitOffset )
            : cur_( reinterpret_cast<uint8_t const*>( &h ) + nBitOffset / c_nBitPerByte )
            , first_( reinterpret_cast<uint8_t const*>( &h ))
            , last_( first_ + c_bitstring_size )
        {
            assert( is_correct( static_cast<unsigned>( nBitOffset )));
            assert( !eos());
        }

        /// Returns \p true if end-of-string is not reached yet
        explicit operator bool() const
        {
            return !eos();
        }

        /// Returns \p true if end-of-stream encountered
        bool eos() const
        {
            return cur_ >= last_;
        }

        /// Cuts next \p count bits (must be multiplier of 8) from bit-string
        /**
            For performance reason, the function does not manage out-of-bound condition.
            To control that use \p safe_cut().
        */
        uint_type cut( unsigned count )
        {
            assert( !eos());
            assert( is_correct( count ));

            uint_type result = 0;

#       if defined( CDS_ARCH_LITTLE_ENDIAN )
            for ( unsigned i = 0; i < count; i += c_nBitPerByte ) {
                result |= static_cast<uint_type>( *cur_ ) << i;
                ++cur_;
            }
#       else
            for ( ; count; count -= c_nBitPerByte ) {
                result = ( result << c_nBitPerByte ) | *cur_;
                ++cur_;
            }
#       endif

            return result;
        }

        /// Cuts up to \p count from the bit-string
        /**
            Safe analog of \p cut(): if \p count is more than the rest of bit-string,
            only the rest is returned.
            When \p eos() condition is met the function returns 0.
        */
        uint_type safe_cut( unsigned count )
        {
            if ( eos())
                return 0;

            unsigned const rest = static_cast<unsigned>( last_ - cur_ - 1 ) * c_nBitPerByte;
            if ( rest < count )
                count = rest;
            return count ? cut( count ) : 0;
        }

        /// Resets the splitter
        void reset() noexcept
        {
            cur_ = first_;
        }

        /// Returns pointer to source bitstring
        bitstring const* source() const
        {
            return reinterpret_cast<bitstring const *>( first_ );
        }

        /// Returns current bit offset from beginning of bit-string
        size_t bit_offset() const
        {
            return (cur_ - first_) * c_nBitPerByte;
        }

        /// Returns how many bits remain
        size_t rest_count() const
        {
            return c_bitstring_size * c_nBitPerByte - bit_offset();
        }

        /// Checks if \p count is multiple of 8
        static constexpr bool is_correct( unsigned count )
        {
            return count % 8 == 0;
        }

    private:
        //@cond
        uint8_t const*  cur_;
        uint8_t const* const    first_;
        uint8_t const* const    last_;
        //@endcond
    };


    /// Cuts a bit sequence from a number
    /**
        The splitter can be used as an iterator over bit representation of the number of type \p Int.
        Each call of \p cut() or \p safe_cut() cuts the bit count specified
        and keeps the position inside the number for the next call.
    */
    template <typename Int>
    class number_splitter
    {
    public:
        typedef Int       int_type;     ///< Number type
        typedef Int       uint_type;    ///< Result type of \p cut() function

        //@cond
        static constexpr unsigned const c_nBitPerByte = 8;
        //@endcond

    public:
        /// Initalizes the splitter with nymber \p n and initial bit offset 0
        explicit number_splitter( int_type n )
            : number_( n )
            , shift_( 0 )
        {}

        /// Initalizes the splitter with nymber \p n and initial bit offset \p initial_offset
        number_splitter( int_type n, size_t initial_offset )
            : number_( n )
            , shift_( static_cast<unsigned>( initial_offset ))
        {
            assert( initial_offset < sizeof( int_type ) * c_nBitPerByte );
        }

        /// Returns \p true if end-of-string is not reached yet
        explicit operator bool() const
        {
            return !eos();
        }

        /// Returns \p true if end-of-stream encountered
        bool eos() const
        {
            return shift_ >= sizeof( int_type ) * c_nBitPerByte;
        }

        /// Cuts next \p count bits (must be multiplier of 8) from the number
        /**
            For performance reason, the function does not manage out-of-bound condition.
            To control that use \p safe_cut().
        */
        int_type cut( unsigned count )
        {
            assert( !eos());
            assert( is_correct( count ));

            int_type result = ( number_ >> shift_ ) & (( 1 << count ) - 1 );
            shift_ += count;

            return result;
        }

        /// Cuts up to \p count from the bit-string
        /**
            Safe analog of \p cut(): if \p count is more than the rest of \p int_type,
            only the rest is returned.
            When \p eos() condition is met the function returns 0.
        */
        int_type safe_cut( unsigned count )
        {
            if ( eos())
                return 0;

            unsigned rest = static_cast<unsigned>( rest_count());
            if ( rest < count )
                count = rest;
            return count ? cut( count ) : 0;
        }

        /// Resets the splitter
        void reset() noexcept
        {
            shift_ = 0;
        }

        /// Returns initial number
        int_type source() const
        {
            return number_;
        }

        /// Returns current bit offset from beginning of the number
        size_t bit_offset() const
        {
            return shift_;
        }

        /// Returns how many bits remain
        size_t rest_count() const
        {
            return sizeof( int_type ) * c_nBitPerByte - shift_;
        }

        /// Checks if \p count is multiple of 8
        static constexpr bool is_correct( unsigned count )
        {
            return count < sizeof( int_type ) * c_nBitPerByte;
        }

    private:
        //@cond
        int_type const  number_;
        unsigned        shift_;
        //@endcond
    };

    /// Metafunctin to select a most suitable splitter for type \p BitString of size \p BitStringSize
    template <typename BitString, size_t BitStringSize >
    struct select_splitter
    {
        typedef split_bitstring< BitString, BitStringSize > type; ///< metafunction result
    };

    //@cond
#   define CDS_SELECT_NUMBER_SPLITTER( num_type ) \
        template <> struct select_splitter<num_type, sizeof(num_type)> { typedef number_splitter<num_type> type; }

    CDS_SELECT_NUMBER_SPLITTER( int );
    CDS_SELECT_NUMBER_SPLITTER( unsigned );
    CDS_SELECT_NUMBER_SPLITTER( short );
    CDS_SELECT_NUMBER_SPLITTER( unsigned short );
    CDS_SELECT_NUMBER_SPLITTER( long );
    CDS_SELECT_NUMBER_SPLITTER( unsigned long );
    CDS_SELECT_NUMBER_SPLITTER( long long );
    CDS_SELECT_NUMBER_SPLITTER( unsigned long long );

#   undef CDS_SELECT_NUMBER_SPLITTER
    //@endcond

}} // namespace cds::algo

#endif // #ifndef CDSLIB_ALGO_SPLIT_BITSTRING_H