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
use std::{
    collections::HashMap,
    fmt,
    ops::{Index, IndexMut},
};

use crate::parse::{serde::Serialize, Token};

#[derive(Clone)]
pub enum DepType {
    Exist,
    Calculate,
}

#[derive(Clone)]
pub enum Manipulation {
    Equal,
    Multiple,
}

#[derive(Clone)]
pub struct NumericPatch {
    pub source: Vec<String>,
    pub dep_type: DepType,
    pub manipulation: Manipulation,
}

#[derive(Clone, Default)]
pub struct LinkedHashMap {
    raw_list: Vec<String>,
    raw_hashmap: HashMap<String, Token>,
    pub patches: HashMap<String, NumericPatch>,
}

impl LinkedHashMap {
    pub fn new() -> Self {
        LinkedHashMap {
            raw_list: Vec::new(),
            raw_hashmap: HashMap::new(),
            patches: HashMap::new(),
        }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        LinkedHashMap {
            raw_list: Vec::with_capacity(capacity),
            raw_hashmap: HashMap::with_capacity(capacity),
            patches: HashMap::new(),
        }
    }

    pub fn push_back<T: Into<Token>>(&mut self, key_str: &str, value: T) -> bool {
        let key = key_str.to_string();

        if self.raw_hashmap.contains_key(&key) {
            return false;
        }

        self.raw_list.push(key.clone());
        self.raw_hashmap.insert(key, value.into());
        true
    }

    pub fn update<T: Into<Token>>(&mut self, key_str: &str, value: T) -> bool {
        let key = key_str.to_string();

        if !self.raw_hashmap.contains_key(&key) {
            return false;
        }

        self.raw_hashmap.insert(key, value.into());
        true
    }

    pub fn contains(&self, key: &str) -> bool {
        self.raw_list.contains(&key.to_string())
    }

    pub fn keys(&self) -> &Vec<String> {
        &self.raw_list
    }

    pub fn iter(&self) -> SeqIter {
        SeqIter {
            index: 0,
            ele: self,
        }
    }
}

impl Index<&str> for LinkedHashMap {
    type Output = Token;
    fn index(&self, index: &str) -> &Self::Output {
        &self.raw_hashmap[&index.to_string()]
    }
}

impl IndexMut<&str> for LinkedHashMap {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        self.raw_hashmap.get_mut(&index.to_string()).unwrap()
    }
}

pub struct SeqIter<'a> {
    index: usize,
    ele: &'a LinkedHashMap,
}

impl<'a> Iterator for SeqIter<'a> {
    type Item = &'a Token;

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.index;
        let keys = self.ele.keys();
        if index < keys.len() {
            self.index += 1;
            let key = &keys[index];
            let res = &self.ele[key];
            Some(res)
        } else {
            None
        }
    }
}

impl Serialize for LinkedHashMap {
    fn to_le_vec(&self) -> Vec<u8> {
        self.iter().flat_map(|token| token.to_le_vec()).collect()
    }
}

impl fmt::Debug for LinkedHashMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("LinkedHashMap")?;
        f.debug_map()
            .entries(
                self.iter()
                    .enumerate()
                    .map(|(index, token)| (&self.keys()[index], token)),
            )
            .finish()
    }
}