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

use super::BufValue;

pub enum BufKeyVal {
  None,
  Array(usize),
  Map(String),
}

pub struct PtrType {
  pub key: String,
  pub val: BufKeyVal,
}

pub type HeapInnerMap = HashMap<String, BufValue>;
pub type Pointer = HashMap<String, PtrType>;

pub struct Heap {
  data: HeapInnerMap,
  pointer: Pointer,
}

impl Heap {
  pub fn new() -> Self {
    Heap {
      data: HashMap::new(),
      pointer: HashMap::new(),
    }
  }

  pub fn inner(&mut self) -> &mut HeapInnerMap {
    &mut self.data
  }

  pub fn set(&mut self, key: String, val: BufValue) -> Option<()> {
    if !key.starts_with("$") {
      return None;
    }
    self.data.insert(key, val);
    Some(())
  }

  pub fn set_ptr(&mut self, key: String, val: BufKeyVal) -> Option<()> {
    if !key.starts_with("*") {
      return None;
    }
    if let BufKeyVal::None = val {
      return None;
    }
    self.pointer.insert(key.clone(), PtrType { key, val });
    Some(())
  }

  pub fn get(&self, key: &String) -> Option<&BufValue> {
    let (ky, typ) = if key.starts_with("*") {
      let ptr = self.pointer.get(key)?;
      (&ptr.key, &ptr.val)
    } else {
      (key, &BufKeyVal::None)
    };

    let val = self.data.get(ky)?;

    if let BufKeyVal::None = typ {
      Some(val)
    } else {
      match (val, typ) {
        (BufValue::Array(val), BufKeyVal::Array(index)) => Some(&val[*index]),
        (BufValue::Object(val), BufKeyVal::Map(key)) => {
          val.get(key).map_or_else(|| None, |x| Some(x.deref()))
        }
        _ => None,
      }
    }
  }

  pub fn remove(&mut self, key: String) -> Option<()> {
    if key.starts_with("*") {
      self.pointer.remove(&key);
      return Some(());
    } else if key.starts_with("$") {
      self.data.remove(&key);
      return Some(());
    }
    None
  }
}