igs 0.1.4

The library for for solving impartial games.
Documentation
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::io;
use std::collections::HashMap;
use std::fmt;
#[cfg(feature = "BP128")] use super::bp128delta::ClusterBP128;
use binout::{AsIs, Serializer};
pub use csf::{fp, ls};
use ph::fmph::{SeedSize, TwoToPowerBitsStatic, GroupSize};

use std::hash::BuildHasher;
use crate::enddb::SortedPositionNimberMap;
#[cfg(feature = "CMPH")] use crate::enddb::cmph::ClusterCMPH;
use crate::dbs::NimbersProvider;
use csf::coding::{BuildCoding, BuildMinimumRedundancy, Coding, SerializableCoding};
use std::borrow::Borrow;
use ph::{BuildDefaultSeededHasher, BuildSeededHasher, GetSize};

/// Slice of the end database, usually compressed/succinct.
pub trait CompressedSlice/*<InSliceGamePosition>: NimbersProvider<InSliceGamePosition>*/ {
    /// The size of self in bytes (including dynamically allocated memory).
    fn size_bytes(&self) -> usize;

    /// Writes given slice to the output.
    fn write(&self, output: &mut dyn io::Write) -> io::Result<()>;
}

/// Constructs slices and reads/writes them from/to files.
pub trait CompressedSliceBuilder</*InSliceGamePosition,*/ UncompressedSlice>: fmt::Display {

    /// The type of slice supported by self.
    type CompressedSlice: CompressedSlice/*<InSliceGamePosition>*/;

    /// Construct slice with positions and nimbers included in src.
    fn construct(&self, src: UncompressedSlice) -> Self::CompressedSlice;

    /// Returns end database slice read from the input.
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized;
}

// Generate value which is (we hope) unique for hash_builder.
/*fn hasher_id<S: BuildHasher>(hash_builder: &S) -> u64 {
    let mut h = hash_builder.build_hasher();
    h.write_u64(0xf178015a3109cc1d);
    h.finish()
}*/

/// Generate value which is (we hope) unique for hash_builder.
fn hasher_id<S: BuildSeededHasher>(seeded_hash_builder: &S) -> u64 {
    seeded_hash_builder.hash_one(0xf178015a3109cc1du64, 1234)
}


// ------------------ FPCMap -----------------

impl<InSliceGamePosition, C, S> NimbersProvider<InSliceGamePosition> for fp::CMap::<C, S>
where InSliceGamePosition: std::hash::Hash, C: Coding<Value=u8>, S: BuildSeededHasher {
    #[inline(always)]
    fn get_nimber(&self, position: &InSliceGamePosition) -> Option<u8> {
        self.get(position).map(|v| *v.borrow())
    }
}

impl<C, S> CompressedSlice for fp::CMap::<C, S>
where C: SerializableCoding<Value=u8> + GetSize, S: BuildSeededHasher {

    #[inline(always)]
    fn size_bytes(&self) -> usize {
        GetSize::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, output: &mut dyn io::Write) -> io::Result<()> {
        fp::CMap::<C, S>::write(&self, output, |o, v| AsIs::write(o, *v))
    }
}

//#[derive(Default)]
pub struct FPCMapBuilder<BC = BuildMinimumRedundancy, LSC = fp::OptimalLevelSize, CSB = fp::LoMemAcceptEquals, S = BuildDefaultSeededHasher>
where LSC: fp::LevelSizeChooser, CSB: fp::CollisionSolverBuilder, S: BuildSeededHasher {
    pub conf: fp::CMapConf<BC, LSC, CSB, S>
}

impl<BC, LSC, CSB, S> From<fp::CMapConf<BC, LSC, CSB, S>> for FPCMapBuilder<BC, LSC, CSB, S>
where LSC: fp::LevelSizeChooser, CSB: fp::CollisionSolverBuilder, S: BuildSeededHasher {
    fn from(conf: fp::CMapConf<BC, LSC, CSB, S>) -> Self {
        Self { conf }
    }
}

impl<BC, LSC, CSB, S> fmt::Display for FPCMapBuilder<BC, LSC, CSB, S>
where BC: BuildCoding<u8>, LSC: fmt::Display + fp::LevelSizeChooser, CSB: fp::CollisionSolverBuilder, S: BuildSeededHasher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FPCMap__{}__{}_levels__h{:X}", self.conf.coding.name(), self.conf.level_size_chooser, hasher_id(&self.conf.hash))
    }
}

impl<BC, LSC, InSliceGamePosition, CSB, S, HMS, Coding> CompressedSliceBuilder<HashMap<InSliceGamePosition, u8, HMS>> for FPCMapBuilder<BC, LSC, CSB, S>
where BC: BuildCoding<u8, Coding=Coding> + Clone,
      Coding: csf::coding::Coding<Value=u8> + csf::coding::SerializableCoding + GetSize,
        LSC: fp::LevelSizeChooser + fmt::Display + Clone,
      InSliceGamePosition: std::hash::Hash + Clone,
      CSB: fp::CollisionSolverBuilder + fp::IsLossless + Clone,
      S: BuildSeededHasher + Clone
{

    type CompressedSlice = fp::CMap::<Coding, S>;

    #[inline(always)]
    fn construct(&self, src: HashMap<InSliceGamePosition, u8, HMS>) -> Self::CompressedSlice {
        Self::CompressedSlice::from_map_with_conf(&src, self.conf.clone(), &mut ())
        //Self::CompressedSlice::with_map_bpf_hash_lsc(&src, self.bits_per_fragment, BuildHasherDefault::<XxHash64>::default(), &self.level_size_chooser, &mut ())
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, |i| AsIs::read(i), self.conf.hash.clone())
    }
}

impl<BC, LSC, InSliceGamePosition, CSB, S, Coding> CompressedSliceBuilder<SortedPositionNimberMap<InSliceGamePosition>>
for FPCMapBuilder<BC, LSC, CSB, S>
    where BC: BuildCoding<u8, Coding=Coding> + Clone,
      Coding: csf::coding::Coding<Value=u8> + csf::coding::SerializableCoding + GetSize,
      LSC: fp::LevelSizeChooser + fmt::Display + Clone,
      InSliceGamePosition: std::hash::Hash + Clone,
      CSB: fp::CollisionSolverBuilder + fp::IsLossless + Clone,
      S: BuildSeededHasher + Clone
{
    type CompressedSlice = fp::CMap::<Coding, S>;

    #[inline(always)]
    fn construct(&self, mut src: SortedPositionNimberMap<InSliceGamePosition>) -> Self::CompressedSlice {
        Self::CompressedSlice::from_slices_with_conf(&mut src.positions, &src.nimbers, self.conf.clone(), &mut ())
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, |i| AsIs::read(i), self.conf.hash.clone())
    }
}

// --------------- FPMap ----------------

impl<InSliceGamePosition: std::hash::Hash, S: BuildSeededHasher> NimbersProvider<InSliceGamePosition> for fp::Map<S> {
    #[inline(always)]
    fn get_nimber(&self, position: &InSliceGamePosition) -> Option<u8> {
        fp::Map::<S>::get(self, position).map(|nimber_u64| nimber_u64 as u8)
    }
}

impl<S: BuildSeededHasher> CompressedSlice for fp::Map<S> {

    #[inline(always)]
    fn size_bytes(&self) -> usize {
        GetSize::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, output: &mut dyn io::Write) -> io::Result<()> {
        fp::Map::<S>::write(&self, output)
    }
}

//#[derive(Default)]
pub struct FPMapBuilder<LSC = fp::OptimalLevelSize, CSB = fp::LoMemAcceptEquals, S = BuildDefaultSeededHasher>
    where LSC: fp::SimpleLevelSizeChooser, CSB: fp::CollisionSolverBuilder, S: BuildSeededHasher
{
    pub conf: fp::MapConf<LSC, CSB, S>
}

impl<LSC, CSB, S> From<fp::MapConf<LSC, CSB, S>> for FPMapBuilder<LSC, CSB, S>
    where LSC: fp::SimpleLevelSizeChooser, CSB: fp::CollisionSolverBuilder, S: BuildSeededHasher {
    fn from(conf: fp::MapConf<LSC, CSB, S>) -> Self {
        Self { conf }
    }
}

impl<LSC: fmt::Display + fp::SimpleLevelSizeChooser, CSB: fp::CollisionSolverBuilder, S: BuildSeededHasher> fmt::Display for FPMapBuilder<LSC, CSB, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let hid = hasher_id(&self.conf.hash);
        if self.conf.bits_per_value == 0 {
            write!(f, "FPMap__{}_levels__h{:X}", self.conf.level_size_chooser, hid)
        } else {
            write!(f, "FPMap{}__{}_levels__h{:X}", self.conf.bits_per_value, self.conf.level_size_chooser, hid)
        }
    }
}

impl<LSC, InSliceGamePosition, CSB, S, HMS> CompressedSliceBuilder<HashMap<InSliceGamePosition, u8, HMS>>
for FPMapBuilder<LSC, CSB, S>
    where LSC: fp::SimpleLevelSizeChooser + fmt::Display + Clone,
          InSliceGamePosition: std::hash::Hash + Clone,
          CSB: fp::CollisionSolverBuilder + Clone,
          S: BuildSeededHasher + Clone
{

    type CompressedSlice = fp::Map::<S>;

    #[inline(always)]
    fn construct(&self, src: HashMap<InSliceGamePosition, u8, HMS>) -> Self::CompressedSlice {
        Self::CompressedSlice::with_map_conf(&src, self.conf.clone(), &mut ())
        //Self::CompressedSlice::with_map_bpf_hash_lsc(&src, self.bits_per_fragment, BuildHasherDefault::<XxHash64>::default(), &self.level_size_chooser, &mut ())
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, self.conf.hash.clone())
    }
}

impl<LSC, InSliceGamePosition, CSB, S> CompressedSliceBuilder<SortedPositionNimberMap<InSliceGamePosition>>
for FPMapBuilder<LSC, CSB, S>
    where LSC: fp::SimpleLevelSizeChooser + fmt::Display + Clone,
          InSliceGamePosition: std::hash::Hash + Clone,
          CSB: fp::CollisionSolverBuilder + Clone,
          S: BuildSeededHasher + Clone
{
    type CompressedSlice = fp::Map::<S>;

    #[inline(always)]
    fn construct(&self, mut src: SortedPositionNimberMap<InSliceGamePosition>) -> Self::CompressedSlice {
        Self::CompressedSlice::with_slices_conf(&mut src.positions, &mut src.nimbers, self.conf.clone())
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, self.conf.hash.clone())
    }
}


// ------------------ FPCMap2 -----------------

impl<InSliceGamePosition, GS, SS, C, S> NimbersProvider<InSliceGamePosition> for fp::GOCMap::<C, GS, SS, S>
where InSliceGamePosition: std::hash::Hash, C: Coding<Value=u8>, GS:GroupSize, SS: SeedSize, S: BuildSeededHasher {
    #[inline(always)]
    fn get_nimber(&self, position: &InSliceGamePosition) -> Option<u8> {
        self.get(position).map(|v| *v.borrow())
    }
}

impl<C, GS, SS, S> CompressedSlice for fp::GOCMap::<C, GS, SS, S>
where C: SerializableCoding<Value=u8> + GetSize, GS: GroupSize, SS: SeedSize, S: BuildSeededHasher {

    #[inline(always)]
    fn size_bytes(&self) -> usize {
        GetSize::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, output: &mut dyn io::Write) -> io::Result<()> {
        fp::GOCMap::<C, GS, SS, S>::write(&self, output, |o, v| AsIs::write(o, *v))
    }
}

//#[derive(Default)]
pub struct FPCMap2Builder<GS = TwoToPowerBitsStatic::<4>, SS = TwoToPowerBitsStatic<2>, BC = BuildMinimumRedundancy, LSC = fp::OptimalLevelSize, S = BuildDefaultSeededHasher>
    where GS: GroupSize, SS: SeedSize, LSC: fp::LevelSizeChooser, S: BuildSeededHasher {
    pub conf: fp::GOCMapConf<BC, LSC, GS, SS, S>
}

impl<GS: GroupSize, SS: SeedSize, BC, LSC, S> From<fp::GOCMapConf<BC, LSC, GS, SS, S>> for FPCMap2Builder<GS, SS, BC, LSC, S>
where LSC: fp::LevelSizeChooser, S: BuildSeededHasher {
    fn from(conf: fp::GOCMapConf<BC, LSC, GS, SS, S>) -> Self {
        Self { conf }
    }
}

impl<GS: GroupSize, SS: SeedSize, BC, LSC, S> fmt::Display for FPCMap2Builder<GS, SS, BC, LSC, S>
where BC: BuildCoding<u8>, LSC: fmt::Display + fp::LevelSizeChooser, S: BuildSeededHasher {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "FPGOMap__{}__{}_levels__gr_seed_{}b_size_{}b__h{:X}",
                self.conf.coding.name(),
                self.conf.level_size_chooser,
                Into::<u8>::into(self.conf.goconf.bits_per_seed), 
                Into::<u8>::into(self.conf.goconf.bits_per_group),
               hasher_id(&self.conf.goconf.hash_builder))
    }
}

impl<BC, LSC, InSliceGamePosition, S, HMS, Coding> CompressedSliceBuilder<HashMap<InSliceGamePosition, u8, HMS>>
for FPCMap2Builder<TwoToPowerBitsStatic::<4>, TwoToPowerBitsStatic<2>, BC, LSC, S>
    where BC: BuildCoding<u8, Coding=Coding> + Clone,
          Coding: csf::coding::Coding<Value=u8> + csf::coding::SerializableCoding + GetSize,
        LSC: fp::LevelSizeChooser + fmt::Display + Clone,
          InSliceGamePosition: std::hash::Hash + Clone,
          S: BuildSeededHasher + Clone
{

    type CompressedSlice = fp::GOCMap::<Coding, TwoToPowerBitsStatic::<4>, TwoToPowerBitsStatic<2>, S>;

    #[inline(always)]
    fn construct(&self, src: HashMap<InSliceGamePosition, u8, HMS>) -> Self::CompressedSlice {
        Self::CompressedSlice::from_map_with_conf(&src, self.conf.clone(), &mut ())
        //Self::CompressedSlice::with_map_bpf_hash_lsc(&src, self.bits_per_fragment, BuildHasherDefault::<XxHash64>::default(), &self.level_size_chooser, &mut ())
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, |i| AsIs::read(i), self.conf.goconf.hash_builder.clone())
    }
}

impl<BC, LSC, InSliceGamePosition, S, Coding> CompressedSliceBuilder<SortedPositionNimberMap<InSliceGamePosition>>
for FPCMap2Builder<TwoToPowerBitsStatic::<4>, TwoToPowerBitsStatic<2>, BC, LSC, S>
    where BC: BuildCoding<u8, Coding=Coding> + Clone,
          Coding: csf::coding::Coding<Value=u8> + csf::coding::SerializableCoding + GetSize,
          LSC: fp::LevelSizeChooser + fmt::Display + Clone,
          InSliceGamePosition: std::hash::Hash + Clone,
          S: BuildSeededHasher + Clone
{
    type CompressedSlice = fp::GOCMap::<Coding, TwoToPowerBitsStatic::<4>, TwoToPowerBitsStatic<2>, S>;

    #[inline(always)]
    fn construct(&self, mut src: SortedPositionNimberMap<InSliceGamePosition>) -> Self::CompressedSlice {
        Self::CompressedSlice::from_slices_with_conf(&mut src.positions, &src.nimbers, self.conf.clone(), &mut ())
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, |i| AsIs::read(i), self.conf.goconf.hash_builder.clone())
    }
}




// --------------- LSMap ----------------

impl<InSliceGamePosition: std::hash::Hash, S: BuildSeededHasher> NimbersProvider<InSliceGamePosition> for ls::Map<S> {
    #[inline(always)]
    fn get_nimber(&self, position: &InSliceGamePosition) -> Option<u8> {
        Some(ls::Map::<S>::get(self, position) as u8)
    }
}

impl<S: BuildSeededHasher> CompressedSlice for ls::Map<S> {

    #[inline(always)]
    fn size_bytes(&self) -> usize {
        GetSize::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, output: &mut dyn io::Write) -> io::Result<()> {
        ls::Map::<S>::write(&self, output)
    }
}

#[derive(Default, Copy, Clone)]
pub struct LSMapBuilder<BH: BuildSeededHasher = BuildDefaultSeededHasher> {
    pub hash: BH
}

impl<BH: BuildSeededHasher> fmt::Display for LSMapBuilder<BH> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "LSMap_h{:X}", hasher_id(&self.hash))
    }
}

impl<InSliceGamePosition, BH, HMS> CompressedSliceBuilder<HashMap<InSliceGamePosition, u8, HMS>> for LSMapBuilder<BH>
    where InSliceGamePosition: std::hash::Hash, BH: BuildSeededHasher + Clone, HMS: BuildHasher
{
    type CompressedSlice = ls::Map<BH>;

    #[inline(always)]
    fn construct(&self, src: HashMap<InSliceGamePosition, u8, HMS>) -> Self::CompressedSlice {
        Self::CompressedSlice::try_from_hashmap(src, ls::MapConf::hash(self.hash.clone())).unwrap()
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, self.hash.clone())
    }
}

impl<InSliceGamePosition, BH> CompressedSliceBuilder<SortedPositionNimberMap<InSliceGamePosition>> for LSMapBuilder<BH>
    where InSliceGamePosition: std::hash::Hash, BH: BuildSeededHasher + Clone
{
    type CompressedSlice = ls::Map<BH>;

    #[inline(always)]
    fn construct(&self, src: SortedPositionNimberMap<InSliceGamePosition>) -> Self::CompressedSlice {
        Self::CompressedSlice::try_with_conf_kv(&src.positions, &src.nimbers, ls::MapConf::hash(self.hash.clone())).unwrap()
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, self.hash.clone())
    }
}


// --------------- LSCMap ----------------

impl<InSliceGamePosition, C, S> NimbersProvider<InSliceGamePosition> for ls::CMap<C, S>
where InSliceGamePosition: std::hash::Hash,
      C: Coding<Value=u8>,
      S: BuildSeededHasher
{
    #[inline(always)]
    fn get_nimber(&self, position: &InSliceGamePosition) -> Option<u8>
    {
        self.get(position).map(|v| *v.borrow())
    }
}

impl<C, S> CompressedSlice for ls::CMap<C, S>
where //InSliceGamePosition: std::hash::Hash,
    C: SerializableCoding<Value=u8> + GetSize,
      S: BuildSeededHasher
{
    #[inline(always)]
    fn size_bytes(&self) -> usize {
        <ls::CMap::<C, S> as GetSize>::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, output: &mut dyn io::Write) -> io::Result<()> {
        ls::CMap::<C, S>::write(&self, output, |o, v| AsIs::write(o, *v))
    }
}

#[derive(Copy, Clone)]
pub struct LSCMapBuilder<BC, BH: BuildSeededHasher = BuildDefaultSeededHasher> {
    pub hash: BH,
    pub coding: BC
}

impl<BC: BuildCoding<u8>, BH: BuildSeededHasher> fmt::Display for LSCMapBuilder<BC, BH> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "LSCMap_{}_h{:X}", self.coding.name(), hasher_id(&self.hash))
    }
}

impl<InSliceGamePosition, BC, BH, HMS, Coding> CompressedSliceBuilder<HashMap<InSliceGamePosition, u8, HMS>> for LSCMapBuilder<BC, BH>
    where InSliceGamePosition: std::hash::Hash,
        BC: BuildCoding<u8, Coding=Coding>,
    Coding: csf::coding::Coding<Value=u8> + csf::coding::SerializableCoding + GetSize,
        BH: BuildSeededHasher + Clone
{
    type CompressedSlice = ls::CMap<Coding, BH>;

    #[inline(always)]
    fn construct(&self, src: HashMap<InSliceGamePosition, u8, HMS>) -> Self::CompressedSlice {
        Self::CompressedSlice::try_from_map_with_builder_conf(&src, &self.coding, ls::MapConf::hash(self.hash.clone()), 0).unwrap()
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, |i| AsIs::read(i), self.hash.clone())
    }
}

impl<InSliceGamePosition, BC, BH, Coding> CompressedSliceBuilder<SortedPositionNimberMap<InSliceGamePosition>> for LSCMapBuilder<BC, BH>
    where InSliceGamePosition: std::hash::Hash,
          BC: BuildCoding<u8, Coding=Coding>,
          BH: BuildSeededHasher + Clone,
        Coding: csf::coding::SerializableCoding<Value=u8> + GetSize
{
    type CompressedSlice = ls::CMap<Coding, BH>;

    #[inline(always)]
    fn construct(&self, src: SortedPositionNimberMap<InSliceGamePosition>) -> Self::CompressedSlice {
        Self::CompressedSlice::try_from_kv_with_builder_conf(&src.positions, &src.nimbers,
                                                         &self.coding,
                                                         ls::MapConf::hash(self.hash.clone()),
                                                     0).unwrap()
    }

    #[inline(always)]
    fn read(&self, input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        Self::CompressedSlice::read_with_hasher(input, |i| AsIs::read(i), self.hash.clone())
    }
}


// --------------- BP128 ----------------

#[cfg(feature = "BP128")]
impl NimbersProvider<u32> for ClusterBP128 {
    #[inline(always)]
    fn get_nimber(&self, position: &u32) -> Option<u8> {
        Some(ClusterBP128::get(self, *position))
    }
}

#[cfg(feature = "BP128")]
impl CompressedSlice<u32> for ClusterBP128 {

    #[inline(always)]
    fn size_bytes(&self) -> usize {
        ClusterBP128::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, _output: &mut dyn io::Write) -> io::Result<()> {
        unimplemented!()
    }
}

#[cfg(feature = "BP128")]
#[derive(Default, Copy, Clone)]
pub struct BP128Buider {}

#[cfg(feature = "BP128")]
impl fmt::Display for BP128Buider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "BP128delta")
    }
}

#[cfg(feature = "BP128")]
impl CompressedSliceBuilder<u32, HashMap<u32, u8>> for BP128Buider {
    type CompressedSlice = ClusterBP128;

    #[inline(always)]
    fn construct(&self, src: HashMap<u32, u8>) -> Self::CompressedSlice {
        src.into()
    }

    #[inline(always)]
    fn read(&self, _input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        unimplemented!()
    }
}

#[cfg(feature = "BP128")]
impl CompressedSliceBuilder<u32, SortedPositionNimberMap<u32>> for BP128Buider {
    type CompressedSlice = ClusterBP128;

    #[inline(always)]
    fn construct(&self, src: SortedPositionNimberMap<u32>) -> Self::CompressedSlice {
        Self::CompressedSlice::from_sorted(&src.positions, &src.nimbers)
    }

    #[inline(always)]
    fn read(&self, _input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        unimplemented!()
    }
}


// --------------- CMPH ----------------

#[cfg(feature = "CMPH")]
impl NimbersProvider<u32> for ClusterCMPH {
    #[inline(always)]
    fn get_nimber(&self, position: &u32) -> Option<u8> {
        Some(ClusterCMPH::get(self, *position) as u8)
    }
}

#[cfg(feature = "CMPH")]
impl CompressedSlice<u32> for ClusterCMPH {

    #[inline(always)]
    fn size_bytes(&self) -> usize {
        ClusterCMPH::size_bytes(self)
    }

    #[inline(always)]
    fn write(&self, _output: &mut dyn io::Write) -> io::Result<()> {
        unimplemented!()
    }
}

#[cfg(feature = "CMPH")]
#[derive(Copy, Clone)]
pub struct CMPHBuider {
    /// an average number of keys per bucket; it can be tuned to obtain different trade-offs between generation time and representation size
    pub lambda: u8
}

#[cfg(feature = "CMPH")]
impl fmt::Display for CMPHBuider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CHD{}", self.lambda)
    }
}

/*impl CompressedSliceBuilder<u32, HashMap<u32, u8>> for CMPHBuider {
    type CompressedSlice = ClusterCMPH;

    #[inline(always)]
    fn construct(&self, src: HashMap<u32, u8>) -> Self::CompressedSlice {
        src.into()
    }
}*/

#[cfg(feature = "CMPH")]
impl CompressedSliceBuilder<u32, SortedPositionNimberMap<u32>> for CMPHBuider {
    type CompressedSlice = ClusterCMPH;

    #[inline(always)]
    fn construct(&self, src: SortedPositionNimberMap<u32>) -> Self::CompressedSlice {
        Self::CompressedSlice::from_kv_lambda(&src.positions, &src.nimbers, self.lambda)
    }

    #[inline(always)]
    fn read(&self, _input: &mut dyn io::Read) -> io::Result<Self::CompressedSlice> where Self::CompressedSlice: std::marker::Sized {
        unimplemented!()
    }
}