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
use std::collections::HashMap;
use std::convert::TryFrom;
use std::ops::Range;

use crate::byte_vec::{DataVecReader, DataVecWriter};
use crate::data_type::DataType;
use crate::{
    has_end, has_start, has_step, skip_byte_sizes, skip_size, skip_sizes, skip_type,
    skip_type_and_byte_size, skip_type_and_size, ExpressionValue, Result, BYTE_SIZE_LENGTH,
    DATA_TYPE_LENGTH, FLOAT_LENGTH, INTEGER_LENGTH, SIZE_LENGTH,
};

pub trait With<T, U> {
    fn with(self, other: Option<U>) -> Option<(T, U)>;
}

impl<T, U> With<T, U> for Option<T> {
    fn with(self, other: Option<U>) -> Option<(T, U)> {
        self.and_then(|val| other.map(|val2| (val, val2)))
    }
}

pub fn hash_byte_slice(bytes: &[u8]) -> usize {
    let mut hash = 1;
    for i in bytes {
        hash ^= *i as usize;
    }

    hash
}

pub fn hash_of_character_list(start: usize, data: &[u8]) -> usize {
    let mut hash = 1;
    let reader = DataVecReader::new_from_slice(data);
    let char_count = reader.read_size(skip_type(start));

    let characters_start = skip_type_and_size(start);
    for i in 0..char_count {
        let character_ref_start = skip_sizes(characters_start, i);
        let character_ref = reader.read_size(character_ref_start);
        let character_length = reader.read_byte_size(skip_type(character_ref));

        let code_points_start = skip_type_and_byte_size(character_ref);
        for j in 0..character_length {
            let code_point = reader.read_byte_size(skip_byte_sizes(code_points_start, j));

            hash ^= code_point;
        }
    }

    hash
}

pub fn hash_expression_string_slice(value_start: usize, vec: &[u8]) -> usize {
    let length = DataVecReader::new_from_slice(vec).read_size(value_start + 1);
    let string_range = (value_start + 1 + SIZE_LENGTH)
        ..(value_start + 1 + SIZE_LENGTH + length * SIZE_LENGTH as usize);
    hash_byte_slice(&vec[string_range])
}

pub fn hash_expression_string(value_start: usize, vec: &Vec<u8>) -> usize {
    hash_expression_string_slice(value_start, &vec)
}

pub fn get_value_with_hash(
    target: usize,
    data: &[u8],
    key_count: usize,
    key_area_range: &Range<usize>,
) -> Result<Option<usize>> {
    let list_index = target % key_count;
    let mut key_index = skip_sizes(key_area_range.start, list_index);

    let reader = DataVecReader::new_from_slice(data);

    let mut pair_ref = reader.read_size(key_index);
    let mut key_ref = reader.read_size(skip_type(pair_ref));
    let mut key_type = DataType::try_from(data[key_ref])?;
    let mut key_value = match key_type {
        DataType::Symbol => reader.read_size(skip_type(key_ref)),
        DataType::CharacterList => hash_of_character_list(key_ref, data),
        _ => 0,
    };

    let mut found = false;

    if key_value != target {
        let max_it = key_count;
        let mut it_count = 0;
        while it_count < max_it {
            key_index = skip_size(key_index);
            if key_index >= key_area_range.end {
                key_index = key_area_range.start;
            }

            pair_ref = reader.read_size(key_index);
            key_ref = reader.read_size(skip_type(pair_ref));
            key_type = DataType::try_from(data[key_ref])?;
            key_value = match key_type {
                DataType::Symbol => reader.read_size(skip_type(key_ref)),
                DataType::CharacterList => hash_of_character_list(key_ref, data),
                _ => 0,
            };

            if key_value == target {
                found = true;
                break;
            }

            it_count += 1;
        }
    } else {
        found = true;
    }

    if found {
        Ok(Some(pair_ref))
    } else {
        Ok(None)
    }
}

pub fn insert_associative_list_keys(
    data: &mut Vec<u8>,
    key_area_range: Range<usize>,
    key_refs: &Vec<usize>,
) -> Result {
    // make sure values are zeroed
    for i in key_area_range.clone() {
        data[i] = 0;
    }

    for key in key_refs.iter() {
        let pair_ref = *key;
        let key_start = pair_ref + 1;
        // convert key data to usize
        let key_ref = DataVecReader::new(data).read_size(key_start);

        let key_type = DataType::try_from(data[key_ref])?;
        let symbol_value = match key_type {
            DataType::Symbol => DataVecReader::new(data).read_size(key_ref + 1),
            DataType::CharacterList => hash_of_character_list(key_ref, &data) as usize,
            _ => 0,
        };

        let mut key_index = (symbol_value % key_refs.len()) * SIZE_LENGTH + key_area_range.start;

        let mut pair_ref_data: Vec<u8> = vec![];
        DataVecWriter::new(&mut pair_ref_data).push_size(pair_ref);

        let existing = DataVecReader::new(data).read_size(key_index);

        if existing != 0 {
            // probe
            let max_it = key_refs.len();
            let mut it_count = 0;
            while it_count < max_it {
                key_index += SIZE_LENGTH;
                if key_index >= key_area_range.end {
                    key_index = key_area_range.start;
                }

                let existing = DataVecReader::new(data).read_size(key_index);
                if existing == 0 {
                    // make ref relative
                    break;
                }

                // fail safe
                it_count += 1;
            }
        }

        DataVecWriter::new(data).write_data(key_index, &pair_ref_data);
    }

    Ok(())
}

pub trait ExpressionValueConsumer {
    fn insert_at_value_cursor(&mut self, data: u8) -> Result<()>;
    fn insert_all_at_value_cursor(&mut self, data: &[u8]) -> Result<()>;
    fn insert_at_ref_cursor(&mut self, r: usize);
    fn get_value_cursor(&self) -> usize;
    fn get_data_mut(&mut self) -> &mut Vec<u8>;
    fn get_symbol_table_mut(&mut self) -> &mut HashMap<String, usize>;
    fn get_expression_table_mut(&mut self) -> &mut Vec<usize>;
    fn get_expression_map_mut(&mut self) -> &mut HashMap<String, usize>;
}

pub trait CopyValue {
    fn copy_value(&mut self, value: &ExpressionValue) -> Result<()>;
}

impl<T> CopyValue for T
where
    T: ExpressionValueConsumer,
{
    fn copy_value(&mut self, value: &ExpressionValue) -> Result<()> {
        let mut ref_pos = self.get_value_cursor();

        let data = value.get_data();
        let symbol_table = value.get_symbol_table();

        let start = self.get_value_cursor();

        let mut i = 0;
        while i < data.len() {
            let d = *data.get(i).unwrap();

            match DataType::try_from(d)? {
                DataType::Unit => {
                    self.insert_at_value_cursor(d)?;

                    i += 1;
                }
                DataType::Integer => {
                    if i + 1 >= data.len() {
                        return Err("Expecting number value, found end of data.".into());
                    }

                    let data_slice = &data[i..(i + 1 + INTEGER_LENGTH)];
                    self.insert_all_at_value_cursor(data_slice)?;

                    i += 1 + INTEGER_LENGTH;
                }
                DataType::Float => {
                    if i + 1 >= data.len() {
                        return Err("Expecting number value, found end of data.".into());
                    }

                    let data_slice = &data[i..(i + 1 + FLOAT_LENGTH)];
                    self.insert_all_at_value_cursor(data_slice)?;

                    i += 1 + FLOAT_LENGTH;
                }
                DataType::Character => {
                    let reader = DataVecReader::new(&data);
                    let length = reader.read_byte_size(skip_type(i));
                    let data_slice = &data[i..(skip_byte_sizes(i, 2 + length))];

                    self.insert_all_at_value_cursor(data_slice)?;

                    i += 2 + length * BYTE_SIZE_LENGTH;
                }
                DataType::CharacterList => {
                    // update ref cursor to point at character list
                    ref_pos = self.get_value_cursor();

                    let reader = DataVecReader::new(&data);

                    let length = reader.read_size(skip_type(i));

                    let mut value_refs: Vec<u8> = vec![];
                    let mut writer = DataVecWriter::new(&mut value_refs);

                    writer = writer
                        .push_data_type(DataType::CharacterList)
                        .push_size(length);

                    let sizes_start = skip_type_and_size(i);
                    for j in 0..length {
                        let j_start = skip_sizes(sizes_start, j);
                        let r = reader.read_size(j_start) + start;

                        writer = writer.push_size(r);
                    }

                    self.insert_all_at_value_cursor(&value_refs[..])?;

                    i += DATA_TYPE_LENGTH + SIZE_LENGTH * (1 + length);
                }
                DataType::Symbol | DataType::ExternalMethod => {
                    match data.get(i + 1) {
                        None => {
                            return Err("Expecting symbol value, found end of data".into());
                        }
                        Some(value) => {
                            // get existing string
                            match symbol_table.iter().find(|(_, v)| **v as u8 == *value) {
                                None => Err(format!("Unresolved symbol value {}", value))?,
                                Some((symbol_string, _)) => {
                                    // check for existing value
                                    let val = match self.get_symbol_table_mut().get(symbol_string) {
                                        // insert new
                                        None => {
                                            let val = self.get_symbol_table_mut().len();
                                            self.get_symbol_table_mut()
                                                .insert(symbol_string.clone(), val);
                                            val
                                        }
                                        // use existing value
                                        Some(val) => *val,
                                    };

                                    self.insert_at_value_cursor(d)?;

                                    let mut data: Vec<u8> = vec![];
                                    DataVecWriter::new(&mut data).push_size(val);
                                    self.insert_all_at_value_cursor(&data)?;
                                }
                            };
                        }
                    }

                    i += 1 + SIZE_LENGTH;
                }
                DataType::Expression => {
                    // insert type
                    self.insert_at_value_cursor(d)?;

                    match data.get(i + 1) {
                        None => {
                            return Err("Expecting symbol value, found end of data".into());
                        }
                        Some(value) => {
                            // get existing string
                            match symbol_table.iter().find(|(_, v)| **v as u8 == *value) {
                                None => {
                                    return Err(format!("Unresolved symbol value {}", value).into());
                                }
                                Some((symbol_string, _)) => {
                                    // check for existing value
                                    match self.get_symbol_table_mut().get(symbol_string) {
                                        // insert new
                                        None => {
                                            let val = self.get_symbol_table_mut().len();
                                            self.get_symbol_table_mut()
                                                .insert(symbol_string.clone(), val);
                                        }
                                        // symbol already exists, do nothing
                                        Some(_) => (),
                                    };

                                    let index =
                                        match self.get_expression_map_mut().get(symbol_string) {
                                            None => {
                                                // insert invalid expression into table and map
                                                let index =
                                                    self.get_expression_table_mut().len() + 1;
                                                self.get_expression_table_mut().push(0);
                                                self.get_expression_map_mut()
                                                    .insert(symbol_string.clone(), index);
                                                index
                                            }
                                            Some(i) => *i,
                                        };

                                    let mut index_data = vec![];
                                    DataVecWriter::new(&mut index_data).push_size(index);

                                    self.insert_all_at_value_cursor(&index_data[..])?;
                                }
                            }
                        }
                    }

                    i += 1 + SIZE_LENGTH;
                }
                DataType::Range => {
                    // update ref cursor to point at range
                    ref_pos = self.get_value_cursor();

                    self.insert_at_value_cursor(d)?;

                    let reader = DataVecReader::new(data);

                    let flags = reader.read_byte_size(skip_type(i)) as u8;
                    let sizes_to_copy = [has_start(flags), has_end(flags), has_step(flags)]
                        .iter()
                        .filter(|b| **b)
                        .count();

                    // insert flags
                    self.insert_at_value_cursor(data[i + 1])?;

                    let mut value_refs: Vec<u8> = vec![];
                    let mut writer = DataVecWriter::new(&mut value_refs);

                    let sizes_start = skip_type_and_byte_size(i);
                    for j in 0..sizes_to_copy {
                        let j_start = skip_sizes(sizes_start, j);
                        let r = reader.read_size(j_start) + start;

                        writer = writer.push_size(r);
                    }

                    self.insert_all_at_value_cursor(&value_refs[..])?;

                    i += 2 + SIZE_LENGTH * sizes_to_copy;
                }
                DataType::Pair => {
                    // update ref cursor to point at pair
                    ref_pos = self.get_value_cursor();

                    self.insert_at_value_cursor(d)?;

                    // add start to references of pair
                    // re-serialize and append to data
                    let reader = DataVecReader::new(data);
                    let key_ref = reader.read_size(i + 1) + start;
                    let value_ref = reader.read_size(i + 1 + SIZE_LENGTH) + start;
                    let mut value_refs: Vec<u8> = vec![];

                    DataVecWriter::new(&mut value_refs)
                        .push_size(key_ref)
                        .push_size(value_ref);

                    self.insert_all_at_value_cursor(&value_refs[..])?;

                    i += 1 + 2 * SIZE_LENGTH;
                }
                DataType::Partial => {
                    // update ref cursor to point at partial
                    ref_pos = self.get_value_cursor();

                    self.insert_at_value_cursor(d)?;

                    // add start to references of partial
                    // re-serialize and append to data
                    let reader = DataVecReader::new(data);
                    let key_ref = reader.read_size(i + 1) + start;
                    let value_ref = reader.read_size(i + 1 + SIZE_LENGTH) + start;
                    let mut value_refs: Vec<u8> = vec![];

                    DataVecWriter::new(&mut value_refs)
                        .push_size(key_ref)
                        .push_size(value_ref);

                    self.insert_all_at_value_cursor(&value_refs[..])?;

                    i += 1 + 2 * SIZE_LENGTH;
                }
                DataType::List => {
                    let value_start = i;
                    // data type, length and counts
                    let value_end = i + 1 + 2 * SIZE_LENGTH;

                    // update ref cursor to point at list
                    ref_pos = self.get_value_cursor();

                    let reader = DataVecReader::new(&data);

                    let length = reader.read_size(i + 1);
                    let key_count = reader.read_size(i + 1 + SIZE_LENGTH);
                    let list_start = value_end;

                    // write data type and counts
                    self.insert_all_at_value_cursor(&data[value_start..value_end])?;

                    // add total size of elements
                    let mut key_refs: Vec<usize> = vec![];
                    let mut value_refs: Vec<u8> = vec![];

                    for j in 0..length {
                        let element_start = list_start + j * SIZE_LENGTH;
                        let value_ref = DataVecReader::new(data).read_size(element_start) + start;
                        DataVecWriter::new(&mut value_refs).push_size(value_ref);

                        let abs_ref = value_ref;
                        if DataType::try_from(self.get_data_mut()[abs_ref])? == DataType::Pair {
                            let key_ref =
                                DataVecReader::new(self.get_data_mut()).read_size(abs_ref + 1);
                            let key_type = DataType::try_from(self.get_data_mut()[key_ref])?;
                            if key_type == DataType::Symbol || key_type == DataType::CharacterList {
                                key_refs.push(value_ref);
                            }
                        }
                    }

                    let key_start = list_start + length * SIZE_LENGTH + start;
                    let key_area = key_start..(key_start + key_count * SIZE_LENGTH);

                    insert_associative_list_keys(&mut self.get_data_mut(), key_area, &key_refs)?;

                    self.insert_all_at_value_cursor(&value_refs[..])?;

                    i += 1 + (2 + length + key_count) * SIZE_LENGTH;
                }
                _ => {
                    return Err(
                        format!("Cannot copy value of {} type.", DataType::try_from(d)?).into(),
                    );
                }
            }
        }

        self.insert_at_ref_cursor(ref_pos);

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::{hash_byte_slice, hash_of_character_list, DataType, DataVecWriter};

    #[test]
    fn hash_byte_slice_equals_hash_character_list() {
        let data = DataVecWriter::write_with(|w| {
            w.push_data_type(DataType::Character) // 0
                .push_byte_size(1)
                .push_character("b")
                .push_data_type(DataType::Character) // 3
                .push_byte_size(1)
                .push_character("e")
                .push_data_type(DataType::Character) // 6
                .push_byte_size(1)
                .push_character("a")
                .push_data_type(DataType::Character) // 9
                .push_byte_size(1)
                .push_character("r")
                .push_data_type(DataType::CharacterList) // 12
                .push_size(4) // 13
                .push_size(0) // 17
                .push_size(3) // 21
                .push_size(6) // 25
                .push_size(9)
        });

        let c = hash_of_character_list(12, &data);
        let b = hash_byte_slice(&("bear".as_bytes()));

        assert_eq!(c, b);
    }

    #[test]
    fn hash_byte_slice_equals_hash_character_list_grapheme_clusters() {
        let data = DataVecWriter::write_with(|w| {
            w.push_data_type(DataType::Character) // 0
                .push_byte_size(1) // 1
                .push_character("z") // 2
                .push_data_type(DataType::Character) // 3
                .push_byte_size(4) // 4
                .push_character("🐼") // 5
                .push_data_type(DataType::Character) // 9
                .push_byte_size(5) // 10
                .push_character("ö̲") // 11
                .push_data_type(DataType::CharacterList) // 16
                .push_size(3)
                .push_size(0)
                .push_size(3)
                .push_size(9)
        });

        let c = hash_of_character_list(16, &data);
        let b = hash_byte_slice(&("z🐼ö̲".as_bytes()));

        assert_eq!(c, b);
    }
}