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
use core;
pub use super::ir_interpret::{IRInterpreter, Context, push_base};
use super::super::alloc;
use super::super::alloc::{SliceWrapper, SliceWrapperMut, Allocator};
use super::interface;
use super::input_pair::{InputPair, InputReference, InputReferenceMut};
use super::util::{floatX, FastLog2u16};
use super::find_stride;
use super::weights::{Weights, BLEND_FIXED_POINT_PRECISION};

const DEFAULT_CM_SPEED_INDEX: usize = 8;
const NUM_SPEEDS_TO_TRY: usize = 16;
const SPEEDS_TO_SEARCH: [u16; NUM_SPEEDS_TO_TRY]= [0,
                                                   1, 1, 1,
                                                   2,
                                                   4,
                                                   8,
                                                   16,
                                                   16,
                                                   32,
                                                   64,
                                                   128, 128,
                                                   512,
                                                   1664,
                                                   1664,
                                                   ];
const MAXES_TO_SEARCH: [u16; NUM_SPEEDS_TO_TRY] = [32,
                                                   32, 128, 16384,
                                                   1024,
                                                   1024,
                                                   8192,
                                                   48,
                                                   8192,
                                                   4096,
                                                   16384,
                                                   256, 16384,
                                                   16384,
                                                   16384,
                                                   16384,
                                                   ];
const NIBBLE_PRIOR_SIZE: usize = 16 * NUM_SPEEDS_TO_TRY;
// the high nibble, followed by the low nibbles
const CONTEXT_MAP_PRIOR_SIZE: usize = 256 * NIBBLE_PRIOR_SIZE * 17;
const STRIDE_PRIOR_SIZE: usize = 256 * 256 * NIBBLE_PRIOR_SIZE * 2;
#[derive(Clone,Copy, Debug)]
pub struct SpeedAndMax(pub u16, pub u16);

pub fn speed_to_tuple(inp:[SpeedAndMax;2]) -> [(u16,u16);2] {
   [(inp[0].0, inp[0].1), (inp[1].0, inp[1].1)]
}

fn get_stride_cdf_low(data: &mut [u16], stride_prior: u8, cm_prior: usize, high_nibble: u8) -> &mut [u16] {
    let index: usize =  1 + 2 * (cm_prior as usize | ((stride_prior as usize & 0xf) << 8) | ((high_nibble as usize) << 12));
    data.split_at_mut(NUM_SPEEDS_TO_TRY * index << 4).1.split_at_mut(16 * NUM_SPEEDS_TO_TRY).0
}

fn get_stride_cdf_high(data: &mut [u16], stride_prior: u8, cm_prior: usize) -> &mut [u16] {
    let index: usize = 2 * (cm_prior as usize | ((stride_prior as usize) << 8));
    data.split_at_mut(NUM_SPEEDS_TO_TRY * index << 4).1.split_at_mut(16 * NUM_SPEEDS_TO_TRY).0
}

fn get_cm_cdf_low(data: &mut [u16], cm_prior: usize, high_nibble: u8) -> &mut [u16] {
    let index: usize = (high_nibble as usize + 1) + 17 * cm_prior as usize;
    data.split_at_mut(NUM_SPEEDS_TO_TRY * index << 4).1.split_at_mut(16 * NUM_SPEEDS_TO_TRY).0
}

fn get_cm_cdf_high(data: &mut [u16], cm_prior: usize) -> &mut [u16] {
    let index: usize = 17 * cm_prior as usize;
    data.split_at_mut(NUM_SPEEDS_TO_TRY * index << 4).1.split_at_mut(16 * NUM_SPEEDS_TO_TRY).0
}
fn init_cdfs(cdfs: &mut [u16]) {
    assert_eq!(cdfs.len() % (16 * NUM_SPEEDS_TO_TRY), 0);
    let mut total_index = 0usize;
    let len = cdfs.len();
    loop {
        for cdf_index in 0..16 {
            let vec = cdfs.split_at_mut(total_index).1.split_at_mut(NUM_SPEEDS_TO_TRY).0;
            for item in vec {
                *item = 4 + 4 * cdf_index as u16;
            }
            total_index += NUM_SPEEDS_TO_TRY;
        }
        if total_index == len {
            break;
        }
    }
}
fn compute_combined_cost(singleton_cost: &mut [floatX;NUM_SPEEDS_TO_TRY],
                cdfs: &[u16],
                mixing_cdf: [u16;16],
                nibble_u8: u8,
                _weights: &mut [Weights; NUM_SPEEDS_TO_TRY]) {
    assert_eq!(cdfs.len(), 16 * NUM_SPEEDS_TO_TRY);
    let nibble = nibble_u8 as usize & 0xf;
    let mut stride_pdf = [0u16; NUM_SPEEDS_TO_TRY];
    stride_pdf.clone_from_slice(cdfs.split_at(NUM_SPEEDS_TO_TRY * nibble).1.split_at(NUM_SPEEDS_TO_TRY).0);
    let mut cm_pdf:u16 = mixing_cdf[nibble];
    if nibble_u8 != 0 {
        let mut tmp = [0u16; NUM_SPEEDS_TO_TRY];
        tmp.clone_from_slice(cdfs.split_at(NUM_SPEEDS_TO_TRY * (nibble - 1)).1.split_at(NUM_SPEEDS_TO_TRY).0);
        for i in 0..NUM_SPEEDS_TO_TRY {
            stride_pdf[i] -= tmp[i];
        }
        cm_pdf -= mixing_cdf[nibble - 1]
    }
    let mut stride_max = [0u16; NUM_SPEEDS_TO_TRY];
    stride_max.clone_from_slice(cdfs.split_at(NUM_SPEEDS_TO_TRY * 15).1);
    let cm_max = mixing_cdf[15];
    for i in 0..NUM_SPEEDS_TO_TRY {
        if stride_pdf[i] == 0 { 
            assert!(stride_pdf[i] != 0);
        }
        if stride_max[i] == 0 {
            assert!(stride_max[i] != 0);
        }
        let w;
        w = (1<<(BLEND_FIXED_POINT_PRECISION - 2)) ; // a quarter of weight to stride
        let combined_pdf = w * u32::from(stride_pdf[i]) + ((1<<BLEND_FIXED_POINT_PRECISION) - w) * u32::from(cm_pdf);
        let combined_max = w * u32::from(stride_max[i]) + ((1<<BLEND_FIXED_POINT_PRECISION) - w) * u32::from(cm_max);
        let del = FastLog2u16((combined_pdf >> BLEND_FIXED_POINT_PRECISION) as u16) - FastLog2u16((combined_max >> BLEND_FIXED_POINT_PRECISION) as u16);
        singleton_cost[i] -= del;
    }
}
fn compute_cost(singleton_cost: &mut [floatX;NUM_SPEEDS_TO_TRY],
                cdfs: &[u16],
                nibble_u8: u8) {
    assert_eq!(cdfs.len(), 16 * NUM_SPEEDS_TO_TRY);
    let nibble = nibble_u8 as usize & 0xf;
    let mut pdf = [0u16; NUM_SPEEDS_TO_TRY];
    pdf.clone_from_slice(cdfs.split_at(NUM_SPEEDS_TO_TRY * nibble).1.split_at(NUM_SPEEDS_TO_TRY).0);
    if nibble_u8 != 0 {
        let mut tmp = [0u16; NUM_SPEEDS_TO_TRY];
        tmp.clone_from_slice(cdfs.split_at(NUM_SPEEDS_TO_TRY * (nibble - 1)).1.split_at(NUM_SPEEDS_TO_TRY).0);
        for i in 0..NUM_SPEEDS_TO_TRY {
            pdf[i] -= tmp[i];
        }
    }
    let mut max = [0u16; NUM_SPEEDS_TO_TRY];
    max.clone_from_slice(cdfs.split_at(NUM_SPEEDS_TO_TRY * 15).1);
    for i in 0..NUM_SPEEDS_TO_TRY {
        if pdf[i] == 0 { 
            assert!(pdf[i] != 0);
        }
        if max[i] == 0 {
            assert!(max[i] != 0);
        }
        let del = FastLog2u16(pdf[i]) - FastLog2u16(max[i]);
        singleton_cost[i] -= del;
    }
}
fn update_cdf(cdfs: &mut [u16],
              nibble_u8: u8) {
    assert_eq!(cdfs.len(), 16 * NUM_SPEEDS_TO_TRY);
    let mut overall_index = nibble_u8 as usize * NUM_SPEEDS_TO_TRY;
    for _nibble in (nibble_u8 as usize & 0xf) .. 16 {
        for speed_index in 0..NUM_SPEEDS_TO_TRY {
            cdfs[overall_index + speed_index] += SPEEDS_TO_SEARCH[speed_index];
        }
        overall_index += NUM_SPEEDS_TO_TRY;
    }
    overall_index = 0;
    for nibble in 0 .. 16 {
        for speed_index in 0..NUM_SPEEDS_TO_TRY {
            if nibble == 0 {
                assert!(cdfs[overall_index + speed_index] != 0);
            } else {
                assert!(cdfs[overall_index + speed_index]  - cdfs[overall_index + speed_index - NUM_SPEEDS_TO_TRY]  != 0);
            }
        }
        overall_index += NUM_SPEEDS_TO_TRY;
    }
    for max_index in 0..NUM_SPEEDS_TO_TRY {
        if cdfs[15 * NUM_SPEEDS_TO_TRY + max_index] >= MAXES_TO_SEARCH[max_index] {
            const CDF_BIAS:[u16;16] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
            for nibble_index in 0..16  {
                let tmp = &mut cdfs[nibble_index * NUM_SPEEDS_TO_TRY + max_index];
                *tmp = (tmp.wrapping_add(CDF_BIAS[nibble_index])).wrapping_sub(tmp.wrapping_add(CDF_BIAS[nibble_index]) >> 2);
            }
        }
    }
    overall_index = 0;
    for nibble in 0 .. 16 {
        for speed_index in 0..NUM_SPEEDS_TO_TRY {
            if nibble == 0 {
                assert!(cdfs[overall_index + speed_index] != 0);
            } else {
                assert!(cdfs[overall_index + speed_index]  - cdfs[overall_index + speed_index - NUM_SPEEDS_TO_TRY]  != 0);
            }
        }
        overall_index += NUM_SPEEDS_TO_TRY;
    }
}

fn extract_single_cdf(cdf_bundle:&[u16], index:usize) -> [u16;16] {
    assert_eq!(cdf_bundle.len(), 16 * NUM_SPEEDS_TO_TRY);
    assert!(index < NUM_SPEEDS_TO_TRY);
    [
        cdf_bundle[index + 0 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 1 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 2 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 3 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 4 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 5 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 6 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 7 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 8 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 9 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 10 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 11 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 12 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 13 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 14 * NUM_SPEEDS_TO_TRY],
        cdf_bundle[index + 15 * NUM_SPEEDS_TO_TRY],
        ]
}

fn min_cost_index_for_speed(cost: &[floatX]) -> usize {
    assert_eq!(cost.len(), NUM_SPEEDS_TO_TRY);
    let mut min_cost = cost[0];
    let mut best_choice = 0;
    for i in 1..NUM_SPEEDS_TO_TRY {
        if cost[i] < min_cost {
            best_choice = i;
            min_cost = cost[i];
        }
    }
    best_choice
}
fn min_cost_speed_max(cost: &[floatX]) -> SpeedAndMax {
    let best_choice = min_cost_index_for_speed(cost);
    SpeedAndMax(
        SPEEDS_TO_SEARCH[best_choice],
        MAXES_TO_SEARCH[best_choice])
}

fn min_cost_value(cost: &[floatX]) -> floatX {
    let best_choice = min_cost_index_for_speed(cost);
    cost[best_choice]
}

const SINGLETON_COMBINED_STRATEGY: usize = 2;
const SINGLETON_STRIDE_STRATEGY: usize = 1;
const SINGLETON_CM_STRATEGY: usize = 0;
    
pub struct ContextMapEntropy<'a,
                             Alloc:alloc::Allocator<u16> + alloc::Allocator<u32> + alloc::Allocator<floatX>,
                             > {
    input: InputPair<'a>,
    context_map: interface::PredictionModeContextMap<InputReferenceMut<'a>>,
    block_type: u8,
    cur_stride: u8,
    local_byte_offset: usize,
    weight: [[Weights; NUM_SPEEDS_TO_TRY];2],
    
    cm_priors: <Alloc as Allocator<u16>>::AllocatedMemory,
    stride_priors: <Alloc as Allocator<u16>>::AllocatedMemory,
    _stride_pyramid_leaves: [u8; find_stride::NUM_LEAF_NODES],
    singleton_costs: [[[floatX;NUM_SPEEDS_TO_TRY];2];3],
}
impl<'a,
     Alloc:alloc::Allocator<u16> + alloc::Allocator<u32> + alloc::Allocator<floatX>,
     > ContextMapEntropy<'a, Alloc> {
   pub fn new(m16: &mut Alloc,
              input: InputPair<'a>,
              stride: [u8; find_stride::NUM_LEAF_NODES],
              prediction_mode: interface::PredictionModeContextMap<InputReferenceMut<'a>>,
              cdf_detection_quality: u8) -> Self {
      let cdf_detect = cdf_detection_quality != 0;
      let mut ret = ContextMapEntropy::<Alloc>{
         input: input,
         context_map: prediction_mode,
         block_type: 0,
         cur_stride: 1,
         local_byte_offset: 0,
         cm_priors: if cdf_detect {<Alloc as Allocator<u16>>::alloc_cell(m16, CONTEXT_MAP_PRIOR_SIZE)} else {<Alloc as Allocator<u16>>::AllocatedMemory::default()},
         stride_priors: if cdf_detect {<Alloc as Allocator<u16>>::alloc_cell(m16, STRIDE_PRIOR_SIZE)} else {<Alloc as Allocator<u16>>::AllocatedMemory::default()},
         _stride_pyramid_leaves: stride,
         weight:[[Weights::new(); NUM_SPEEDS_TO_TRY],
                 [Weights::new(); NUM_SPEEDS_TO_TRY]],
         singleton_costs:[[[0.0 as floatX;NUM_SPEEDS_TO_TRY];2];3],
      };
      if cdf_detect {
        init_cdfs(ret.cm_priors.slice_mut());
        init_cdfs(ret.stride_priors.slice_mut());
      }
      ret
   }
   pub fn take_prediction_mode(&mut self) -> interface::PredictionModeContextMap<InputReferenceMut<'a>> {
       core::mem::replace(&mut self.context_map, interface::PredictionModeContextMap::<InputReferenceMut<'a>>{
          literal_context_map:InputReferenceMut::default(),
          predmode_speed_and_distance_context_map:InputReferenceMut::default(),
       })
   }
   pub fn prediction_mode_mut(&mut self) -> &mut interface::PredictionModeContextMap<InputReferenceMut<'a>> {
       &mut self.context_map
   }
   pub fn best_singleton_speeds(&self,
                                 cm: bool,
                                 combined: bool) -> ([SpeedAndMax;2], [floatX; 2]) {
        let cost_type_index = if combined {
            2usize
        } else if cm {
            0usize
        } else {
            1usize
        };
        let mut ret_cost = [self.singleton_costs[cost_type_index][0][0],
                            self.singleton_costs[cost_type_index][1][0]];
        let mut best_indexes = [0,0];
        for speed_index in 1..NUM_SPEEDS_TO_TRY {
            for highness in 0..2 {
                let cur_cost = self.singleton_costs[cost_type_index][highness][speed_index];
                if cur_cost < ret_cost[highness] {
                    best_indexes[highness] = speed_index;
                    ret_cost[highness] = cur_cost;
                }
            }
        }
        let ret_speed = [SpeedAndMax(SPEEDS_TO_SEARCH[best_indexes[0]],
                                 MAXES_TO_SEARCH[best_indexes[0]]),
                     SpeedAndMax(SPEEDS_TO_SEARCH[best_indexes[1]],
                                 MAXES_TO_SEARCH[best_indexes[1]])];
       (ret_speed, ret_cost)
    }
    pub fn best_speeds(&mut self, // mut due to helpers
                       cm:bool,
                       combined: bool) -> [SpeedAndMax;2] { 
       let mut ret = [SpeedAndMax(SPEEDS_TO_SEARCH[0],MAXES_TO_SEARCH[0]); 2];
       let cost_type_index = if combined {
           2usize
       } else if cm {
           0usize
       } else {
           1usize
       };
       for high in 0..2 {
           /*eprintln!("TRIAL {} {}", cm, combined);
           for i in 0..NUM_SPEEDS_TO_TRY {
               eprintln!("{},{} costs {:?}", SPEEDS_TO_SEARCH[i], MAXES_TO_SEARCH[i], self.singleton_costs[cost_type_index][high][i]);
           }*/
         ret[high] = min_cost_speed_max(&self.singleton_costs[cost_type_index][high][..]);
       }
       ret
   }
   pub fn best_speeds_costs(&mut self, // mut due to helpers
                            cm:bool,
                            combined: bool) -> [floatX;2] { 
       let cost_type_index = if combined {
           2usize
       } else if cm {
           0usize
       } else {
           1usize
       };
       let mut ret = [0.0 as floatX; 2];
       for high in 0..2 {
         ret[high] = min_cost_value(&self.singleton_costs[cost_type_index][high][..]);
       }
       ret
   }
   pub fn free(&mut self, alloc: &mut Alloc) {
        <Alloc as Allocator<u16>>::free_cell(alloc, core::mem::replace(&mut self.cm_priors, <Alloc as Allocator<u16>>::AllocatedMemory::default()));
        <Alloc as Allocator<u16>>::free_cell(alloc, core::mem::replace(&mut self.stride_priors, <Alloc as Allocator<u16>>::AllocatedMemory::default()));
   }
   fn update_cost_base(&mut self, stride_prior: u8, _selected_bits:u8, cm_prior: usize, literal: u8) {
       let upper_nibble = (literal >> 4);
       let lower_nibble = literal & 0xf;
       let provisional_cm_high_cdf: [u16; 16];
       let provisional_cm_low_cdf: [u16; 16];
       {
           let cm_cdf_high = get_cm_cdf_high(self.cm_priors.slice_mut(), cm_prior);
           compute_cost(&mut self.singleton_costs[SINGLETON_CM_STRATEGY][1],
                        cm_cdf_high, upper_nibble);
           // choose a fairly reasonable cm speed rather than a selected one
           let best_cm_index = DEFAULT_CM_SPEED_INDEX;// = min_cost_index_for_speed(&self.singleton_costs[SINGLETON_CM_STRATEGY][1]);
           provisional_cm_high_cdf = extract_single_cdf(cm_cdf_high, best_cm_index);
       }
       {
           let cm_cdf_low = get_cm_cdf_low(self.cm_priors.slice_mut(), cm_prior, upper_nibble);
           compute_cost(&mut self.singleton_costs[SINGLETON_CM_STRATEGY][0],
                        cm_cdf_low, lower_nibble);
           // choose a fairly reasonable cm speed rather than a selected one
           let best_cm_index = DEFAULT_CM_SPEED_INDEX;//min_cost_index_for_speed(&self.singleton_costs[SINGLETON_CM_STRATEGY][0]);
           provisional_cm_low_cdf = extract_single_cdf(cm_cdf_low, best_cm_index);
       }
       {
           let stride_cdf_high = get_stride_cdf_high(self.stride_priors.slice_mut(), stride_prior, cm_prior);
           compute_combined_cost(&mut self.singleton_costs[SINGLETON_COMBINED_STRATEGY][1],
                                 stride_cdf_high, provisional_cm_high_cdf, upper_nibble, &mut self.weight[1]);
           compute_cost(&mut self.singleton_costs[SINGLETON_STRIDE_STRATEGY][1],
                        stride_cdf_high, upper_nibble);
           update_cdf(stride_cdf_high, upper_nibble);
       }
       {
           let stride_cdf_low = get_stride_cdf_low(self.stride_priors.slice_mut(), stride_prior, cm_prior, upper_nibble);
           compute_combined_cost(&mut self.singleton_costs[SINGLETON_COMBINED_STRATEGY][0],
                                 stride_cdf_low, provisional_cm_low_cdf, lower_nibble, &mut self.weight[0]);
           compute_cost(&mut self.singleton_costs[SINGLETON_STRIDE_STRATEGY][0],
                        stride_cdf_low,
                        lower_nibble);
           update_cdf(stride_cdf_low, lower_nibble);
       }
       {
           let cm_cdf_high = get_cm_cdf_high(self.cm_priors.slice_mut(), cm_prior);
           update_cdf(cm_cdf_high, upper_nibble);
       }
       {
           let cm_cdf_low = get_cm_cdf_low(self.cm_priors.slice_mut(), cm_prior, upper_nibble);
           update_cdf(cm_cdf_low, lower_nibble);
       }
   }
}

impl<'a, 'b, Alloc: alloc::Allocator<u16> + alloc::Allocator<u32> + alloc::Allocator<floatX>> interface::CommandProcessor<'b> for ContextMapEntropy<'a, Alloc> {
    fn push(&mut self,
            val: interface::Command<InputReference<'b>>) {
        push_base(self, val)
    }
}

impl<'a, Alloc: alloc::Allocator<u16> + alloc::Allocator<u32> + alloc::Allocator<floatX>> IRInterpreter for ContextMapEntropy<'a, Alloc> {
    fn inc_local_byte_offset(&mut self, inc: usize) {
        self.local_byte_offset += inc;
    }
    fn local_byte_offset(&self) -> usize {
        self.local_byte_offset
    }
    fn update_block_type(&mut self, new_type: u8, stride: u8) {
        self.block_type = new_type;
        self.cur_stride = stride;
    }
    fn block_type(&self) -> u8 {
        self.block_type
    }
    fn literal_data_at_offset(&self, index:usize) -> u8 {
        self.input[index]
    }
    fn literal_context_map(&self) -> &[u8] {
        self.context_map.literal_context_map.slice()
    }
    fn prediction_mode(&self) -> ::interface::LiteralPredictionModeNibble {
        self.context_map.literal_prediction_mode()
    }
    fn update_cost(&mut self, stride_prior: [u8;8], stride_prior_offset: usize, selected_bits: u8, cm_prior: usize, literal: u8) {
        let stride = self.cur_stride as usize;
        self.update_cost_base(stride_prior[stride_prior_offset.wrapping_sub(stride) & 7], selected_bits, cm_prior, literal)
    }
}