1 #ifndef BMSERIAL__H__INCLUDED__
2 #define BMSERIAL__H__INCLUDED__
33 #ifndef BM__H__INCLUDED__
36 # error missing include (bm.h or bm64.h)
41 #pragma warning( push )
42 #pragma warning( disable : 4311 4312 4127)
87 typedef byte_buffer<allocator_type>
buffer;
146 unsigned char* buf,
size_t buf_size);
216 bool inverted =
false);
258 unsigned size_control);
289 void add_model(
unsigned char mod,
unsigned score);
298 typedef bm::heap_vector<bm::gap_word_t, allocator_type> block_arridx_type;
299 typedef typename allocator_type::allocator_pool_type allocator_pool_type;
303 unsigned bit_model_d0_size_;
304 unsigned bit_model_0run_size_;
305 block_arridx_type bit_idx_arr_;
306 unsigned scores_[64];
307 unsigned char models_[64];
313 bool byte_order_serial_;
315 unsigned compression_level_;
316 bool own_temp_block_;
320 allocator_pool_type pool_;
367 const char*
err_msg() {
return "BM::Invalid serialization format"; }
378 template<
class BV,
class DEC>
394 const unsigned char* buf,
425 template<
class BV,
class SerialIterator>
438 bool exit_on_one =
false);
441 typedef typename BV::blocks_manager_type blocks_manager_type;
453 size_type finalize_target_vector(blocks_manager_type& bman,
463 const char* err_msg() {
return "BM::de-serialization format error"; }
585 bool clear_target=
true);
641 const unsigned char* buf,
644 bool exit_on_one =
false
648 typename BV::blocks_manager_type blocks_manager_type;
728 compression_stat_(0),
730 byte_order_serial_(true),
733 bit_idx_arr_.resize(65536);
736 temp_block_ = alloc_.alloc_bit_block();
737 own_temp_block_ =
true;
741 temp_block_ = temp_block;
742 own_temp_block_ =
false;
744 compression_stat_ = (size_type*) alloc_.alloc_bit_block();
745 optimize_ = free_ =
false;
751 compression_stat_(0),
753 byte_order_serial_(true),
759 temp_block_ = alloc_.alloc_bit_block();
760 own_temp_block_ =
true;
764 temp_block_ = temp_block;
765 own_temp_block_ =
false;
767 compression_stat_ = (size_type*) alloc_.alloc_bit_block();
768 optimize_ = free_ =
false;
775 alloc_.free_bit_block(temp_block_);
776 if (compression_stat_)
777 alloc_.free_bit_block((
bm::word_t*)compression_stat_);
784 for (
unsigned i = 0; i < 256; ++i)
785 compression_stat_[i] = 0;
793 compression_level_ = clevel;
805 byte_order_serial_ = value;
813 unsigned char header_flag = 0;
819 if (!byte_order_serial_)
829 enc.
put_8(header_flag);
831 if (byte_order_serial_)
834 enc.
put_8((
unsigned char)bo);
877 unsigned gamma_size = (unsigned)(enc_pos1 - enc_pos0);
890 enc.
put_16(gap_block, len-1);
900 if (len > 3 && (compression_level_ > 3))
915 unsigned gamma_size = (unsigned)(enc_pos1 - enc_pos0);
929 enc.
put_16(gap_block, len-1);
942 if (compression_level_ > 3 && arr_len > 1)
950 bout.
gamma(prev + 1);
952 for (
unsigned i = 1; i < arr_len; ++i)
955 bout.
gamma(curr - prev);
960 unsigned gamma_size = (unsigned)(enc_pos1 - enc_pos0);
962 if (gamma_size >= plain_size)
968 compression_stat_[scode]++;
975 compression_stat_[scode]++;
1001 bout.
gamma(arr_len-4);
1006 unsigned enc_size = (unsigned)(enc_pos1 - enc_pos0);
1008 if (enc_size >= raw_size)
1014 compression_stat_[scode]++;
1021 compression_stat_[scode]++;
1028 scores_[mod_size_] = score; models_[mod_size_] = mod;
1042 if (compression_level_ <= 1)
1047 if (compression_level_ <= 5)
1050 if (compression_level_ >= 2)
1059 bit_model_d0_size_ = unsigned(8 + (32 * d0_bc *
sizeof(
bm::word_t)));
1078 if (compression_level_ >= 3)
1082 unsigned arr_size_inv =
1088 if (compression_level_ >= 4)
1090 const unsigned gamma_bits_per_int = 6;
1093 if (compression_level_ == 4)
1097 16 + (bit_gaps-1) * gamma_bits_per_int);
1100 16 + bc * gamma_bits_per_int);
1101 if (inverted_bc > 3 && inverted_bc < bit_gaps && inverted_bc <
bm::gap_equiv_len)
1103 16 + inverted_bc * gamma_bits_per_int);
1106 if (compression_level_ >= 5)
1108 const unsigned bie_bits_per_int = 4;
1112 32 + (bit_gaps-1) * bie_bits_per_int);
1139 for (
unsigned i = 0; i < mod_size_; ++i)
1141 if (scores_[i] < min_score)
1143 min_score = scores_[i];
1157 if (compression_level_ <= 2)
1165 if (compression_level_ < 4)
1167 if (compression_level_ == 4)
1172 if (inverted_bc < len)
1174 if (compression_level_ < 4)
1176 if (compression_level_ == 4)
1185 if (compression_level_ == 4)
1199 bool invert =
false;
1201 unsigned char enc_choice = find_gap_best_encoding(gap_block);
1205 gamma_gap_block(gap_block, enc);
1214 enc.
put_16(gap_temp_block[0]);
1228 gamma_gap_array(gap_temp_block, arr_len, enc, invert);
1231 interpolated_encode_gap_block(gap_block, enc);
1242 interpolated_gap_array(gap_temp_block, arr_len, enc, invert);
1245 gamma_gap_block(gap_block, enc);
1256 enc.
put_8((blk[0]==0) ? 0 : 1);
1292 enc.
put_32(blk + i, j - i);
1310 if (bit_model_0run_size_ < bit_model_d0_size_)
1312 encode_bit_interval(block, enc, 0);
1331 enc.
put_32(block[off+j+0]);
1332 enc.
put_32(block[off+j+1]);
1333 enc.
put_32(block[off+j+2]);
1334 enc.
put_32(block[off+j+3]);
1347 encode_bit_interval(block, enc, 0);
1366 bv.calc_stat(&stat);
1371 optimize_ = free_ =
false;
1373 size_type slen = this->
serialize(bv, buf.data(), buf.size());
1385 optimize_ = free_ =
true;
1390 bv.optimize(temp_block_, BV::opt_compress, &st);
1393 optimize_ = free_ =
false;
1402 unsigned mask = inverted ? ~0u : 0u;
1411 unsigned char scode =
1414 compression_stat_[scode]++;
1417 encode_bit_digest(block, enc, digest0_);
1426 gamma_gap_block(bit_idx_arr_.data(), enc);
1433 unsigned mask = inverted ? ~0u : 0u;
1441 gamma_gap_array(bit_idx_arr_.data(), arr_len, enc, inverted);
1452 unsigned mask = inverted ? ~0u : 0u;
1460 interpolated_gap_array(bit_idx_arr_.data(), arr_len, enc, inverted);
1463 encode_bit_digest(block, enc, digest0_);
1472 interpolated_encode_gap_block(bit_idx_arr_.data(), enc);
1497 enc.
put_8((
unsigned char)head);
1504 unsigned enc_size = (unsigned)(enc_pos1 - enc_pos0);
1506 if (enc_size >= raw_size)
1512 compression_stat_[scode]++;
1515 encode_bit_digest(block, enc, digest0_);
1522 unsigned mask = inverted ? ~0u : 0u;
1530 unsigned char scode =
1549 unsigned enc_size = (unsigned)(enc_pos1 - enc_pos0);
1551 if (enc_size >= raw_size)
1557 if (digest0_ != ~0ull && enc_size > bit_model_d0_size_)
1563 compression_stat_[scode]++;
1568 encode_bit_digest(block, enc, digest0_);
1573 #define BM_SER_NEXT_GRP(enc, nb, B_1ZERO, B_8ZERO, B_16ZERO, B_32ZERO, B_64ZERO) \
1575 enc.put_8(B_1ZERO); \
1576 else if (nb < 256u) \
1578 enc.put_8(B_8ZERO); \
1579 enc.put_8((unsigned char)nb); \
1581 else if (nb < 65536u) \
1583 enc.put_8(B_16ZERO); \
1584 enc.put_16((unsigned short)nb); \
1586 else if (nb < bm::id_max32) \
1588 enc.put_8(B_32ZERO); \
1589 enc.put_32(unsigned(nb)); \
1593 enc.put_8(B_64ZERO); \
1597 #define BM_SET_ONE_BLOCKS(x) \
1599 block_idx_type end_block = i + x; \
1600 for (;i < end_block; ++i) \
1601 bman.set_block_all_set(i); \
1607 typename serializer<BV>::size_type
1609 unsigned char* buf,
size_t buf_size)
1613 reset_compression_stats();
1617 encode_header(bv, enc);
1623 bman.get_block_coord(i, i0, j0);
1625 const bm::word_t* blk = bman.get_block(i0, j0);
1642 return (size_type)enc.
size();
1646 if (nb > 1 && nb < 128)
1649 unsigned char c = (
unsigned char)((1u << 7) | nb);
1674 bman.get_block_coord(j, i0, j0);
1675 const bm::word_t* blk_next = bman.get_block(i0, j0);
1710 unsigned char model = find_bit_best_encoding(blk);
1718 unsigned bit_idx = 0;
1731 encode_bit_array(blk, enc,
false);
1734 encode_bit_array(blk, enc,
true);
1737 gamma_gap_bit_block(blk, enc);
1740 encode_bit_interval(blk, enc, 0);
1743 gamma_arr_bit_block(blk, enc,
false);
1746 gamma_arr_bit_block(blk, enc,
true);
1749 bienc_arr_bit_block(blk, enc,
false);
1752 bienc_arr_bit_block(blk, enc,
true);
1755 interpolated_arr_bit_block(blk, enc,
false);
1758 interpolated_arr_bit_block(blk, enc,
true);
1761 interpolated_gap_bit_block(blk, enc);
1764 bienc_gap_bit_block(blk, enc);
1767 encode_bit_digest(blk, enc, digest0_);
1785 return (size_type)enc.
size();
1901 const unsigned char* buf,
1907 unsigned char header_flag = dec.
get_8();
1914 if (bo_current == bo)
1939 unsigned block_type,
1966 if (k == 0) --bit_idx;
1969 dst_arr[k] = bit_idx;
1982 dst_arr[len-1] = max_v;
1983 bin.bic_decode_u16(&dst_arr[1], len-2, min_v, max_v);
1989 throw std::logic_error(err_msg());
1991 BM_THROW(BM_ERR_SERIALFORMAT);
2005 unsigned arr_len = dec.
get_16();
2007 bit_in_type bin(dec);
2011 bin.bic_decode_u16_dry(arr_len-2, min_v, max_v);
2016 bin.bic_decode_u16_bitset(blk, arr_len-2, min_v, max_v);
2024 this->read_bic_arr(
decoder, blk);
2036 unsigned arr_len = dec.
get_16();
2042 id_array_[0] = head;
2043 id_array_[1] = min_v;
2044 id_array_[arr_len] = 65535;
2046 bit_in_type bin(dec);
2047 bin.bic_decode_u16(&id_array_[2], arr_len-2, min_v, 65535);
2083 block[off+j+0] |= dec.
get_32();
2084 block[off+j+1] |= dec.
get_32();
2085 block[off+j+2] |= dec.
get_32();
2086 block[off+j+3] |= dec.
get_32();
2101 unsigned char run_type = dec.
get_8();
2104 unsigned run_length = dec.
get_16();
2107 unsigned run_end = j + run_length;
2109 for (;j < run_end; ++j)
2111 unsigned w = dec.
get_32();
2125 unsigned block_type,
2137 *dst_block = gap_head;
2166 unsigned arr_len = read_id_list(
decoder, block_type, id_array_);
2176 unsigned len = (gap_head >> 3);
2180 *dst_block = gap_head;
2187 for (
unsigned i = 1; i < len; ++i)
2191 *(++gap_data_ptr) = gap_sum;
2201 unsigned len = (gap_head >> 3);
2202 *dst_block = gap_head;
2204 dst_block[1] = min_v;
2206 bin.bic_decode_u16(&dst_block[2], len-2, min_v, 65535);
2213 throw std::logic_error(err_msg());
2215 BM_THROW(BM_ERR_SERIALFORMAT);
2229 template<
class BV,
class DEC>
2232 temp_block_ = alloc_.alloc_bit_block();
2234 this->id_array_ = bit_idx_arr_.data();
2238 template<
class BV,
class DEC>
2241 alloc_.free_bit_block(temp_block_);
2245 template<
class BV,
class DEC>
2268 *gap_temp_block = gap_head;
2269 dec.
get_16(gap_temp_block+1, len - 1);
2274 blk = bman.get_allocator().alloc_bit_block();
2275 bman.set_block(nb, blk);
2282 bv.combine_operation_with_block(nb,
2296 bman.get_allocator().alloc_gap_block(
unsigned(level), bman.glen());
2298 *gap_blk_ptr = gap_head;
2303 dec.
get_16(gap_blk + 1, len - 1);
2309 *gap_temp_block = gap_head;
2310 dec.
get_16(gap_temp_block + 1, len - 1);
2320 unsigned arr_len = this->read_id_list(dec, btype, this->id_array_);
2321 gap_temp_block[0] = 0;
2330 bv.combine_operation_with_block(nb,
2344 this->read_gap_block(dec, btype, gap_temp_block, gap_head);
2348 this->read_gap_block(dec, btype, gap_temp_block, gap_head);
2353 throw std::logic_error(this->err_msg());
2355 BM_THROW(BM_ERR_SERIALFORMAT);
2359 bv.combine_operation_with_block(nb,
2365 template<
class BV,
class DEC>
2374 blk = bman.get_allocator().alloc_bit_block();
2375 bman.set_block(nb, blk);
2380 blk = bman.deoptimize_block(nb);
2388 blk = bman.deoptimize_block(nb);
2392 for (
unsigned k = 0; k < len; ++k)
2401 this->read_bic_arr(dec, blk);
2406 blk = bman.deoptimize_block(nb);
2409 this->read_bic_arr(dec, temp_block_);
2414 this->read_bic_gap(dec, blk);
2417 this->read_digest0_block(dec, blk);
2422 throw std::logic_error(this->err_msg());
2424 BM_THROW(BM_ERR_SERIALFORMAT);
2432 template<
class BV,
class DEC>
2434 const unsigned char* buf,
2438 if (!bman.is_init())
2444 bv.set_new_blocks_strat(
BM_GAP);
2450 unsigned char header_flag = dec.
get_8();
2460 throw std::logic_error(this->err_msg());
2462 BM_THROW(BM_ERR_SERIALFORMAT);
2480 if (bv_size > bv.size())
2483 for (
unsigned cnt = dec.
get_32(); cnt; --cnt)
2489 return dec.
size()-1;
2512 throw std::logic_error(this->err_msg());
2514 BM_THROW(BM_ERR_SERIALFORMAT);
2521 if (bv_size > bv.size())
2525 unsigned char btype;
2531 btype = dec.
get_8();
2533 bman.get_block_coord(i, i0, j0);
2534 bm::word_t* blk = bman.get_block_ptr(i0, j0);
2537 if (btype & (1 << 7))
2539 nb = btype & ~(1 << 7);
2571 throw std::logic_error(this->err_msg());
2573 BM_THROW(BM_ERR_SERIALFORMAT);
2583 bman.set_block_all_set(i);
2600 throw std::logic_error(this->err_msg());
2602 BM_THROW(BM_ERR_SERIALFORMAT);
2611 blk = bman.get_allocator().alloc_bit_block();
2612 bman.set_block(i, blk);
2618 bv.combine_operation_with_block(i,
2628 bv.set_bit_no_check(bit_idx);
2634 this->read_0runs_block(dec, temp_block);
2635 bv.combine_operation_with_block(i,
2642 unsigned head_idx, tail_idx;
2648 blk = bman.get_allocator().alloc_bit_block();
2649 bman.set_block(i, blk);
2650 for (
unsigned k = 0; k < head_idx; ++k)
2654 dec.
get_32(blk + head_idx, tail_idx - head_idx + 1);
2662 dec.
get_32(temp_block + head_idx, tail_idx - head_idx + 1);
2664 bv.combine_operation_with_block(i,
2679 deserialize_gap(btype, dec, bv, bman, i, blk);
2687 blk = bman.deoptimize_block(i);
2693 blk = bman.get_allocator().alloc_bit_block();
2694 bman.set_block(i, blk);
2700 for (
unsigned k = 0; k < len; ++k)
2709 for (
unsigned k = 0; k < len; ++k)
2720 decode_bit_block(btype, dec, bman, i, blk);
2723 decode_bit_block(btype, dec, bman, i, blk);
2728 throw std::logic_error(this->err_msg());
2730 BM_THROW(BM_ERR_SERIALFORMAT);
2735 bv.set_new_blocks_strat(strat);
2745 end_of_stream_(false),
2785 unsigned char header_flag =
decoder_.get_8();
2838 ::free(block_idx_arr_);
2857 end_of_stream_ =
true;
2861 last_id_ = decoder_.get_32();
2868 end_of_stream_ =
true;
2873 block_type_ = decoder_.get_8();
2877 if (block_type_ & (1u << 7u))
2879 mono_block_cnt_ = (block_type_ & ~(1u << 7u)) - 1;
2880 state_ = e_zero_blocks;
2884 switch (block_type_)
2888 end_of_stream_ =
true; state_ = e_unknown;
2891 state_ = e_zero_blocks;
2892 mono_block_cnt_ = 0;
2895 state_ = e_zero_blocks;
2896 mono_block_cnt_ = decoder_.get_8()-1;
2899 state_ = e_zero_blocks;
2900 mono_block_cnt_ = decoder_.get_16()-1;
2903 state_ = e_zero_blocks;
2904 mono_block_cnt_ = decoder_.get_32()-1;
2907 state_ = e_one_blocks;
2911 state_ = e_one_blocks;
2912 mono_block_cnt_ = 0;
2915 state_ = e_one_blocks;
2916 mono_block_cnt_ = decoder_.get_8()-1;
2919 state_ = e_one_blocks;
2920 mono_block_cnt_ = decoder_.get_16()-1;
2923 state_ = e_one_blocks;
2924 mono_block_cnt_ = decoder_.get_32()-1;
2936 state_ = e_bit_block;
2942 gap_head_ = decoder_.get_16();
2950 state_ = e_gap_block;
2953 state_ = e_gap_block;
2958 throw std::logic_error(this->err_msg());
2960 BM_THROW(BM_ERR_SERIALFORMAT);
2969 if (!mono_block_cnt_)
2981 throw std::logic_error(this->err_msg());
2983 BM_THROW(BM_ERR_SERIALFORMAT);
2992 BM_ASSERT(state_ == e_zero_blocks || state_ == e_one_blocks);
2993 if (!mono_block_cnt_)
2997 block_idx_ += mono_block_cnt_+1;
2998 mono_block_cnt_ = 0;
3011 for (
unsigned k = 0; k < len; ++k)
3019 for (
unsigned k = 0; k < len; ++k)
3036 switch (this->block_type_)
3045 unsigned char run_type = decoder_.get_8();
3048 unsigned run_length = decoder_.get_16();
3051 decoder_.get_32(dst_block ? dst_block + j : dst_block, run_length);
3059 unsigned head_idx = decoder_.get_16();
3060 unsigned tail_idx = decoder_.get_16();
3063 for (
unsigned i = 0; i < head_idx; ++i)
3065 decoder_.get_32(dst_block + head_idx,
3066 tail_idx - head_idx + 1);
3072 int pos = int(tail_idx - head_idx) + 1;
3080 get_arr_bit(dst_block,
true );
3085 throw std::logic_error(this->err_msg());
3087 BM_THROW(BM_ERR_SERIALFORMAT);
3091 get_inv_arr(dst_block);
3096 this->read_bic_arr(decoder_, dst_block);
3099 this->read_bic_arr_inv(decoder_, tmp_block);
3106 this->read_bic_gap(decoder_, dst_block);
3111 this->read_digest0_block(decoder_, dst_block);
3116 throw std::logic_error(this->err_msg());
3118 BM_THROW(BM_ERR_SERIALFORMAT);
3131 switch (block_type_)
3138 unsigned head_idx = decoder_.get_16();
3139 unsigned tail_idx = decoder_.get_16();
3140 for (
unsigned i = head_idx; i <= tail_idx; ++i)
3141 dst_block[i] |= decoder_.get_32();
3146 unsigned char run_type = decoder_.get_8();
3149 unsigned run_length = decoder_.get_16();
3152 unsigned run_end = j + run_length;
3153 for (;j < run_end; ++j)
3156 dst_block[j] |= decoder_.get_32();
3168 get_arr_bit(dst_block,
false );
3171 get_inv_arr(tmp_block);
3175 this->read_bic_arr(decoder_, dst_block);
3178 this->read_bic_arr_inv(decoder_, tmp_block);
3182 this->read_bic_gap(decoder_, dst_block);
3185 this->read_digest0_block(decoder_, dst_block);
3190 throw std::logic_error(this->err_msg());
3192 BM_THROW(BM_ERR_SERIALFORMAT);
3206 switch (block_type_)
3213 unsigned char run_type = decoder_.get_8();
3216 unsigned run_length = decoder_.get_16();
3218 unsigned run_end = j + run_length;
3221 for (;j < run_end; ++j)
3224 dst_block[j] &= decoder_.get_32();
3229 for (;j < run_end; ++j)
3240 unsigned head_idx = decoder_.get_16();
3241 unsigned tail_idx = decoder_.get_16();
3243 for ( i = 0; i < head_idx; ++i)
3245 for ( i = head_idx; i <= tail_idx; ++i)
3246 dst_block[i] &= decoder_.get_32();
3253 get_arr_bit(tmp_block,
true );
3258 get_inv_arr(tmp_block);
3266 this->read_bic_arr(decoder_, tmp_block);
3270 this->read_bic_arr(decoder_, 0);
3273 this->read_bic_arr_inv(decoder_, tmp_block);
3282 this->read_bic_gap(decoder_, tmp_block);
3286 this->read_bic_gap(decoder_, 0);
3293 this->read_digest0_block(decoder_, tmp_block);
3297 this->read_digest0_block(decoder_, 0);
3302 throw std::logic_error(this->err_msg());
3304 BM_THROW(BM_ERR_SERIALFORMAT);
3319 switch (block_type_)
3323 dst_block[i] ^= decoder_.get_32();
3327 unsigned char run_type = decoder_.get_8();
3330 unsigned run_length = decoder_.get_16();
3333 unsigned run_end = j + run_length;
3334 for (;j < run_end; ++j)
3337 dst_block[j] ^= decoder_.get_32();
3349 unsigned head_idx = decoder_.get_16();
3350 unsigned tail_idx = decoder_.get_16();
3351 for (
unsigned i = head_idx; i <= tail_idx; ++i)
3352 dst_block[i] ^= decoder_.get_32();
3358 get_arr_bit(tmp_block,
true );
3363 get_inv_arr(tmp_block);
3369 this->read_bic_arr(decoder_, tmp_block);
3374 this->read_bic_arr_inv(decoder_, tmp_block);
3386 this->read_bic_gap(decoder_, tmp_block);
3390 this->read_bic_gap(decoder_, 0);
3397 this->read_digest0_block(decoder_, tmp_block);
3401 this->read_digest0_block(decoder_, 0);
3406 throw std::logic_error(this->err_msg());
3408 BM_THROW(BM_ERR_SERIALFORMAT);
3423 switch (block_type_)
3427 dst_block[i] &= ~decoder_.get_32();
3431 unsigned char run_type = decoder_.get_8();
3434 unsigned run_length = decoder_.get_16();
3437 unsigned run_end = j + run_length;
3438 for (;j < run_end; ++j)
3441 dst_block[j] &= ~decoder_.get_32();
3453 unsigned head_idx = decoder_.get_16();
3454 unsigned tail_idx = decoder_.get_16();
3455 for (
unsigned i = head_idx; i <= tail_idx; ++i)
3456 dst_block[i] &= ~decoder_.get_32();
3462 get_arr_bit(tmp_block,
true );
3467 get_inv_arr(tmp_block);
3473 this->read_bic_arr(decoder_, tmp_block);
3478 this->read_bic_arr_inv(decoder_, tmp_block);
3487 this->read_bic_gap(decoder_, tmp_block);
3491 this->read_bic_gap(decoder_, 0);
3498 this->read_digest0_block(decoder_, tmp_block);
3502 this->read_digest0_block(decoder_, 0);
3507 throw std::logic_error(this->err_msg());
3509 BM_THROW(BM_ERR_SERIALFORMAT);
3524 switch (block_type_)
3533 unsigned char run_type = decoder_.get_8();
3536 unsigned run_length = decoder_.get_16();
3539 unsigned run_end = j + run_length;
3540 for (;j < run_end; ++j)
3554 unsigned head_idx = decoder_.get_16();
3555 unsigned tail_idx = decoder_.get_16();
3556 for (
unsigned i = head_idx; i <= tail_idx; ++i)
3561 count += get_arr_bit(0);
3568 get_inv_arr(tmp_block);
3572 this->read_bic_arr(decoder_, tmp_block);
3575 this->read_bic_arr_inv(decoder_, tmp_block);
3579 this->read_digest0_block(decoder_, tmp_block);
3583 this->read_bic_gap(decoder_, tmp_block);
3590 throw std::logic_error(this->err_msg());
3592 BM_THROW(BM_ERR_SERIALFORMAT);
3612 switch (block_type_)
3619 unsigned char run_type = decoder_.get_8();
3622 unsigned run_length = decoder_.get_16();
3625 unsigned run_end = j + run_length;
3626 for (;j < run_end; ++j)
3641 unsigned head_idx = decoder_.get_16();
3642 unsigned tail_idx = decoder_.get_16();
3643 for (
unsigned i = head_idx; i <= tail_idx; ++i)
3654 get_inv_arr(tmp_block);
3657 this->read_bic_arr(decoder_, tmp_block);
3660 this->read_bic_arr_inv(decoder_, tmp_block);
3663 this->read_bic_gap(decoder_, tmp_block);
3666 this->read_digest0_block(decoder_, 0);
3671 throw std::logic_error(this->err_msg());
3673 BM_THROW(BM_ERR_SERIALFORMAT);
3690 switch (block_type_)
3699 unsigned char run_type = decoder_.get_8();
3702 unsigned run_length = decoder_.get_16();
3705 unsigned run_end = j + run_length;
3706 for (;j < run_end; ++j)
3720 unsigned head_idx = decoder_.get_16();
3721 unsigned tail_idx = decoder_.get_16();
3722 for (
unsigned i = head_idx; i <= tail_idx; ++i)
3729 get_arr_bit(tmp_block,
true );
3733 get_inv_arr(tmp_block);
3738 this->read_bic_arr(decoder_, tmp_block);
3741 this->read_bic_arr_inv(decoder_, tmp_block);
3745 this->read_digest0_block(decoder_, tmp_block);
3749 this->read_bic_gap(decoder_, tmp_block);
3756 throw std::logic_error(this->err_msg());
3758 BM_THROW(BM_ERR_SERIALFORMAT);
3774 switch (block_type_)
3791 unsigned char run_type = decoder_.get_8();
3794 unsigned run_length = decoder_.get_16();
3795 unsigned run_end = j + run_length;
3798 for (;j < run_end; ++j)
3806 for (;j < run_end; ++j)
3817 unsigned head_idx = decoder_.get_16();
3818 unsigned tail_idx = decoder_.get_16();
3821 for (i = 0; i < head_idx; ++i)
3823 for (i = head_idx; i <= tail_idx; ++i)
3832 get_arr_bit(tmp_block,
true );
3835 get_inv_arr(tmp_block);
3839 this->read_bic_arr(decoder_, tmp_block);
3842 this->read_bic_arr_inv(decoder_, tmp_block);
3846 this->read_digest0_block(decoder_, tmp_block);
3850 this->read_bic_gap(decoder_, tmp_block);
3856 throw std::logic_error(this->err_msg());
3858 BM_THROW(BM_ERR_SERIALFORMAT);
3862 return count_adapter.
sum();
3874 switch (block_type_)
3891 unsigned char run_type = decoder_.get_8();
3894 unsigned run_length = decoder_.get_16();
3895 unsigned run_end = j + run_length;
3898 for (;j < run_end; ++j)
3906 for (;j < run_end; ++j)
3917 unsigned head_idx = decoder_.get_16();
3918 unsigned tail_idx = decoder_.get_16();
3921 for (i = 0; i < head_idx; ++i)
3923 for (i = head_idx; i <= tail_idx; ++i)
3932 get_arr_bit(tmp_block,
true );
3935 get_inv_arr(tmp_block);
3939 this->read_bic_arr(decoder_, tmp_block);
3942 this->read_bic_arr_inv(decoder_, tmp_block);
3947 this->read_digest0_block(decoder_, tmp_block);
3951 this->read_bic_gap(decoder_, tmp_block);
3957 throw std::logic_error(this->err_msg());
3959 BM_THROW(BM_ERR_SERIALFORMAT);
3963 return count_adapter.
sum();
3975 switch (block_type_)
3992 unsigned char run_type = decoder_.get_8();
3995 unsigned run_length = decoder_.get_16();
3996 unsigned run_end = j + run_length;
3999 for (;j < run_end; ++j)
4007 for (;j < run_end; ++j)
4018 unsigned head_idx = decoder_.get_16();
4019 unsigned tail_idx = decoder_.get_16();
4022 for (i = 0; i < head_idx; ++i)
4024 for (i = head_idx; i <= tail_idx; ++i)
4034 get_arr_bit(tmp_block,
true );
4037 get_inv_arr(tmp_block);
4041 this->read_bic_arr(decoder_, tmp_block);
4044 this->read_bic_arr_inv(decoder_, tmp_block);
4048 this->read_digest0_block(decoder_, tmp_block);
4052 this->read_bic_gap(decoder_, tmp_block);
4058 throw std::logic_error(this->err_msg());
4060 BM_THROW(BM_ERR_SERIALFORMAT);
4064 return count_adapter.
sum();
4076 switch (block_type_)
4093 unsigned char run_type = decoder_.get_8();
4096 unsigned run_length = decoder_.get_16();
4097 unsigned run_end = j + run_length;
4100 for (;j < run_end; ++j)
4103 count +=
word_bitcount(decoder_.get_32() & (~dst_block[j]));
4116 unsigned head_idx = decoder_.get_16();
4117 unsigned tail_idx = decoder_.get_16();
4120 for (i = head_idx; i <= tail_idx; ++i)
4128 get_arr_bit(tmp_block,
true );
4131 get_inv_arr(tmp_block);
4135 this->read_bic_arr(decoder_, tmp_block);
4138 this->read_bic_arr_inv(decoder_, tmp_block);
4142 this->read_digest0_block(decoder_, tmp_block);
4146 this->read_bic_gap(decoder_, tmp_block);
4152 throw std::logic_error(this->err_msg());
4154 BM_THROW(BM_ERR_SERIALFORMAT);
4157 return count_adapter.
sum();
4182 for (
unsigned k = 0; k < len; ++k)
4195 decoder_.seek(len * 2);
4204 ++(this->block_idx_);
4205 this->state_ = e_blocks;
4207 return decoder_.get_16();
4214 BM_ASSERT(this->state_ == e_gap_block ||
4218 this->read_gap_block(this->decoder_,
4223 ++(this->block_idx_);
4224 this->state_ = e_blocks;
4236 get_bit_func_type bit_func = bit_func_table_[op];
4238 unsigned cnt = ((*this).*(bit_func))(dst_block, tmp_block);
4239 this->state_ = e_blocks;
4240 ++(this->block_idx_);
4249 const unsigned char* buf,
4256 unsigned char header_flag = dec.
get_8();
4262 blocks_manager_type& bman = bv.get_blocks_manager();
4263 bit_block_guard<blocks_manager_type> bg(bman);
4264 if (temp_block == 0)
4266 temp_block = bg.allocate();
4275 if (bo_current == bo)
4301 throw std::logic_error(
"BM::Platform error unknown endian");
4303 BM_THROW(BM_ERR_SERIALFORMAT);
4309 template<
class BV,
class SerialIterator>
4312 serial_iterator_type& sit,
4316 const unsigned win_size = 64;
4321 for (
unsigned i = 0; i <= id_count;)
4324 for (j = 0; j < win_size && i <= id_count; ++j, ++i)
4326 id_buffer[j] = sit.get_id();
4334 for (
unsigned i = 0; i <= id_count;)
4337 for (j = 0; j < win_size && i <= id_count; ++j, ++i)
4339 id_buffer[j] = sit.get_id();
4347 template<
class BV,
class SerialIterator>
4349 iterator_deserializer<BV, SerialIterator>::finalize_target_vector(
4350 blocks_manager_type& bman,
4352 size_type bv_block_idx)
4354 size_type count = 0;
4365 if (bv_block_idx <= nblock_last)
4366 bman.set_all_zero(bv_block_idx, nblock_last);
4374 bman.get_block_coord(bv_block_idx, i, j);
4375 bm::word_t*** blk_root = bman.top_blocks_root();
4376 unsigned top_size = bman.top_block_size();
4377 for (;i < top_size; ++i)
4390 count += bman.block_bitcount(blk_blk[j]);
4400 throw std::logic_error(err_msg());
4402 BM_THROW(BM_ERR_SERIALFORMAT);
4408 template<
class BV,
class SerialIterator>
4410 iterator_deserializer<BV, SerialIterator>::process_id_list(
4412 serial_iterator_type& sit,
4415 size_type count = 0;
4416 unsigned id_count = sit.get_id_count();
4417 bool set_clear =
true;
4424 load_id_list(bv_tmp, sit, id_count,
true);
4436 load_id_list(bv, sit, id_count, set_clear);
4439 for (
unsigned i = 0; i < id_count; ++i)
4447 for (
unsigned i = 0; i < id_count; ++i)
4457 for (size_type i = 0; i < id_count; ++i)
4468 load_id_list(bv_tmp, sit, id_count,
true);
4476 load_id_list(bv_tmp, sit, id_count,
true);
4484 load_id_list(bv_tmp, sit, id_count,
false);
4485 count += bv_tmp.count();
4491 load_id_list(bv_tmp, sit, id_count,
true);
4499 throw std::logic_error(err_msg());
4501 BM_THROW(BM_ERR_SERIALFORMAT);
4509 template<
class BV,
class SerialIterator>
4522 gap_temp_block[0] = 0;
4524 blocks_manager_type& bman = bv.get_blocks_manager();
4525 if (!bman.is_init())
4528 if (sit.bv_size() && (sit.bv_size() > bv.size()))
4529 bv.resize(sit.bv_size());
4531 typename serial_iterator_type::iterator_state state;
4532 state = sit.get_state();
4533 if (state == serial_iterator_type::e_list_ids)
4535 count = process_id_list(bv, sit, op);
4546 count += finalize_target_vector(bman, op, bv_block_idx);
4550 state = sit.state();
4553 case serial_iterator_type::e_blocks:
4556 case serial_iterator_type::e_bit_block:
4558 BM_ASSERT(sit.block_idx() == bv_block_idx);
4560 bman.get_block_coord(bv_block_idx, i0, j0);
4561 bm::word_t* blk = bman.get_block_ptr(i0, j0);
4574 blk = bman.make_bit_block(bv_block_idx);
4587 throw std::logic_error(err_msg());
4589 BM_THROW(BM_ERR_SERIALFORMAT);
4606 blk = bman.deoptimize_block(bv_block_idx);
4612 unsigned c = sit.get_bit_block(blk, temp_block, sop);
4614 if (exit_on_one && count)
4620 case serial_iterator_type::e_zero_blocks:
4622 BM_ASSERT(bv_block_idx == sit.block_idx());
4629 bv_block_idx = sit.skip_mono_blocks();
4635 bv_block_idx = sit.skip_mono_blocks();
4636 bman.set_all_zero(nb_start, bv_block_idx-1);
4646 bman.get_block_coord(bv_block_idx, i0, j0);
4647 bm::word_t* blk = bman.get_block_ptr(i0, j0);
4658 bman.zero_block(bv_block_idx);
4672 count += blk ? bman.block_bitcount(blk) : 0;
4673 if (exit_on_one && count)
4685 case serial_iterator_type::e_one_blocks:
4687 BM_ASSERT(bv_block_idx == sit.block_idx());
4689 bman.get_block_coord(bv_block_idx, i0, j0);
4690 bm::word_t* blk = bman.get_block_ptr(i0, j0);
4697 bman.set_block_all_set(bv_block_idx);
4705 bman.zero_block(bv_block_idx);
4711 count += blk ? bman.block_bitcount(blk) : 0;
4719 blk = bman.deoptimize_block(bv_block_idx);
4750 bman.set_block_all_set(bv_block_idx);
4764 if (exit_on_one && count)
4769 case serial_iterator_type::e_gap_block:
4771 BM_ASSERT(bv_block_idx == sit.block_idx());
4773 bman.get_block_coord(bv_block_idx, i0, j0);
4774 const bm::word_t* blk = bman.get_block(i0, j0);
4776 sit.get_gap_block(gap_temp_block);
4794 if (exit_on_one && count)
4802 bman.zero_block(bv_block_idx);
4813 bv_block_idx, gap_temp_block, level);
4826 bv.combine_operation_with_block(bv_block_idx,
4834 bv.combine_operation_with_block(
4843 bman.get_block_coord(bv_block_idx, i0, j0);
4844 blk = bman.get_block_ptr(i0, j0);
4860 throw std::logic_error(err_msg());
4862 BM_THROW(BM_ERR_SERIALFORMAT);
4882 #pragma warning( pop )