rusty_source_map/array_set.rs
1use std::collections::HashMap;
2
3#[derive(Clone, Default)]
4pub struct ArraySet {
5 array: Vec<String>,
6 set: HashMap<String, usize>,
7}
8
9impl ArraySet {
10 /// What is the element at the given index?
11 /// # Examples
12 /// ```
13 /// use rusty_source_map::array_set;
14 /// let set = array_set::ArraySet::new();
15 ///
16 /// assert_eq!(set.size(), 0);
17 /// ```
18 pub fn new() -> Self {
19 Default::default()
20 }
21
22 /// What is the element at the given index?
23 /// # Examples
24 /// ```
25 /// use rusty_source_map::array_set;
26 /// let set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
27 ///
28 /// assert_eq!(set.size(), 1);
29 /// ```
30 pub fn from_array(arr: Vec<String>, allow_duplicates: bool) -> Self {
31 let mut array_set = ArraySet::new();
32 for i in arr {
33 array_set.add(i, allow_duplicates);
34 }
35 array_set
36 }
37
38 /// What is the element at the given index?
39 /// # Examples
40 /// ```
41 /// use rusty_source_map::array_set;
42 /// let set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
43 ///
44 /// assert_eq!(set.size(), 1);
45 /// ```
46 pub fn size(&self) -> usize {
47 self.set.len()
48 }
49
50 /// What is the element at the given index?
51 /// # Examples
52 /// ```
53 /// use rusty_source_map::array_set;
54 /// let mut set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
55 /// set.add("string".to_owned(), false);
56 ///
57 /// assert_eq!(set.size(), 2);
58 /// ```
59 pub fn add(&mut self, data: String, allow_duplicates: bool) {
60 let is_duplicate = self.has(data.clone());
61 let idx = self.array.len();
62
63 if !is_duplicate || allow_duplicates {
64 self.array.push(data.clone());
65 }
66 if !is_duplicate {
67 self.set.insert(data, idx);
68 }
69 }
70
71 /// What is the element at the given index?
72 /// # Examples
73 /// ```
74 /// use rusty_source_map::array_set;
75 /// let set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
76 /// assert_eq!(set.has("a".to_owned()), true);
77 /// ```
78 pub fn has(&self, data: String) -> bool {
79 self.set.contains_key(data.as_str())
80 }
81
82 /// What is the element at the given index?
83 /// # Examples
84 /// ```
85 /// use rusty_source_map::array_set;
86 /// let set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
87 /// assert_eq!(set.index_of("a".to_owned()), Some(0));
88 /// ```
89 pub fn index_of(&self, data: String) -> Option<usize> {
90 self.set.get(data.as_str()).cloned()
91 }
92
93 /// What is the element at the given index?
94 /// # Examples
95 /// ```
96 /// use rusty_source_map::array_set;
97 /// let set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
98 /// assert_eq!(set.at(0), Some("a".to_owned()));
99 /// ```
100 pub fn at(&self, idx: i32) -> Option<String> {
101 if idx >= 0 && idx < self.array.len() as i32 {
102 self.array.get(idx as usize).cloned()
103 } else {
104 None
105 }
106 }
107
108 ///
109 /// Returns the array representation of this set (which has the proper indices
110 /// indicated by indexOf). Note that this is a copy of the internal array used
111 /// for storing the members so that no one can mess with internal state.
112 ///
113 /// # Examples
114 /// ```
115 /// use rusty_source_map::array_set;
116 /// let set = array_set::ArraySet::from_array(vec!["a".to_owned()], false);
117 /// assert_eq!(set.to_vec().len(), 1);
118 /// ```
119 pub fn to_vec(&self) -> Vec<String> {
120 self.array.clone()
121 }
122}