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
use std::borrow::Cow;

use byteorder::{ByteOrder, LittleEndian};
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
pub struct ConnectionCostMatrix {
    pub costs_data: Cow<'static, [u8]>,
    pub backward_size: u32,
}

impl ConnectionCostMatrix {
    pub fn load_static(conn_data: &'static [u8]) -> ConnectionCostMatrix {
        let backward_size = LittleEndian::read_i16(&conn_data[2..4]);
        ConnectionCostMatrix {
            costs_data: Cow::Borrowed(&conn_data[4..]),
            backward_size: backward_size as u32,
        }
    }

    pub fn load(conn_data: &[u8]) -> ConnectionCostMatrix {
        let backward_size = LittleEndian::read_i16(&conn_data[2..4]);
        ConnectionCostMatrix {
            costs_data: Cow::Owned(conn_data[4..].to_vec()),
            backward_size: backward_size as u32,
        }
    }

    pub fn cost(&self, forward_id: u32, backward_id: u32) -> i32 {
        let cost_id = (backward_id + forward_id * self.backward_size) as usize;
        LittleEndian::read_i16(&self.costs_data[cost_id * 2..]) as i32
    }
}