1
1
use ordered_varint::Variable;
2
use rand::prelude::SliceRandom;
3
use rand::thread_rng;
4

            
5
1
fn main() {
6
1
    // Generate some random values
7
1
    let mut rng = thread_rng();
8
1
    let mut original_values = vec![];
9
246
    for power in 1..246 {
10
245
        if power > 123 {
11
122
            original_values.push(-(2_i128.pow(power - 123) + 1));
12
123
        } else {
13
123
            original_values.push(2_i128.pow(power) - 1);
14
123
        }
15
    }
16

            
17
1
    original_values.shuffle(&mut rng);
18
1

            
19
1
    // Encode the values.
20
1
    let mut encoded = original_values
21
1
        .iter()
22
245
        .map(|value| value.to_variable_vec().unwrap())
23
1
        .collect::<Vec<_>>();
24
1

            
25
1
    // Sort the original vec and the encoded vec.
26
1
    original_values.sort_unstable();
27
1
    encoded.sort();
28
1

            
29
1
    // Decode the encoded values.
30
1
    let decoded = encoded
31
1
        .iter()
32
245
        .map(|encoded| i128::decode_variable(encoded.as_slice()).unwrap())
33
1
        .collect::<Vec<_>>();
34
1

            
35
1
    // This assert proves that the encoded values are encoded such that sorting
36
1
    // works identially on the encoded and decoded values.
37
1
    assert_eq!(original_values, decoded);
38

            
39
    // Print some summary information
40
245
    let total_encoded_bytes: usize = encoded.iter().map(|encoded| encoded.len()).sum();
41
1
    let total_original_bytes = original_values.len() * std::mem::size_of::<u128>();
42
1
    println!("Original bytes: {total_original_bytes}");
43
1
    println!("Encoded bytes: {total_encoded_bytes}");
44
245
    for (original, encoded) in original_values.into_iter().zip(encoded.into_iter()) {
45
245
        println!("{original} encodes as {encoded:02x?}");
46
245
    }
47
1
}
48

            
49
1
#[test]
50
1
fn runs() {
51
1
    main();
52
1
}