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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
use crate::{IndexTreeMap, Item, Key, Node, Value, ARRAY_SIZE};
use std::cmp::Ordering::{Equal, Greater, Less};
impl<K: Key, V: Value> IndexTreeMap<K, V> {
/// Returns a reference to the value corresponding to the key.
///
/// The key may be any borrowed form of the map’s key type, but the
/// ordering on the borrowed form must match the ordering on the key type.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get(&1), Some(&"a".to_string()));
/// assert_eq!(tree.get(&2), None);
/// ```
pub fn get(&self, key: &K) -> Option<&V> {
match self.root.get_item(key) {
None => None,
Some(item) => match &item.value {
None => None,
Some(value) => Some(value),
},
}
}
/// Returns the key-value pair corresponding to the supplied key.
///
/// The supplied key may be any borrowed form of the map’s key type, but
/// the ordering on the borrowed form must match the ordering on the key type.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_key_value(&1), (Some(&1), Some(&"a".to_string())));
/// assert_eq!(tree.get_key_value(&2), (None, None));
/// ```
pub fn get_key_value(&self, key: &K) -> (Option<&K>, Option<&V>) {
match self.root.get_item(key) {
None => (None, None),
Some(item) => (
Some(item.key.as_ref()),
match &item.value {
None => None,
Some(value) => Some(value.as_ref()),
},
),
}
}
/// Returns a reference to the value corresponding to the index.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_from_index(0), Some(&"a".to_string()));
/// assert_eq!(tree.get_from_index(1), None);
/// ```
pub fn get_from_index(&self, id: usize) -> Option<&V> {
match self.root.get_item_from_index(id) {
None => None,
Some(item) => match &item.value {
None => None,
Some(value) => Some(value),
},
}
}
/// Returns a reference to the key corresponding to the index.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_key_from_index(0), Some(&1));
/// assert_eq!(tree.get_key_from_index(1), None);
/// ```
pub fn get_key_from_index(&self, id: usize) -> Option<&K> {
if self.contains_index(id) {
match self.root.get_item_from_index(id) {
None => None,
Some(item) => Some(item.key.as_ref()),
}
} else {
None
}
}
/// Returns a reference to the key-value pair corresponding to the index.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_key_value_from_index(0), (Some(&1), Some(&"a".to_string())));
/// assert_eq!(tree.get_key_value_from_index(1), (None, None));
/// ```
pub fn get_key_value_from_index(&self, id: usize) -> (Option<&K>, Option<&V>) {
if self.contains_index(id) {
match self.root.get_item_from_index(id) {
None => return (None, None),
Some(item) => {
return (
Some(item.key.as_ref()),
match &item.value {
None => None,
Some(value) => Some(value.as_ref()),
},
)
}
}
}
(None, None)
}
/// Returns a reference to the first key in the map.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_first_key(), Some(&1));
/// ```
pub fn get_first_key(&self) -> Option<&K> {
self.get_key_from_index(0)
}
/// Returns a reference to the first value in the map.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_first_value(), Some(&"a".to_string()));
/// ```
pub fn get_first_value(&self) -> Option<&V> {
self.get_from_index(0)
}
/// Returns a reference to the first key-value pair in the map.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// assert_eq!(tree.get_first_key_value(), (Some(&1),Some(&"a".to_string())));
/// ```
pub fn get_first_key_value(&self) -> (Option<&K>, Option<&V>) {
self.get_key_value_from_index(0)
}
/// Returns a reference to the last key in the map.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// tree.insert(2, "b".to_string());
/// assert_eq!(tree.get_last_key(), Some(&2));
/// ```
pub fn get_last_key(&self) -> Option<&K> {
self.get_key_from_index(self.size - 1)
}
/// Returns a reference to the first value in the map.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// tree.insert(2, "b".to_string());
/// assert_eq!(tree.get_last_value(), Some(&"b".to_string()));
/// ```
pub fn get_last_value(&self) -> Option<&V> {
self.get_from_index(self.size - 1)
}
/// Returns a reference to the last key-value pair in the map.
///
/// # Example
///
/// Basic usage:
/// ```rust
/// use indextreemap::IndexTreeMap;
///
/// let mut tree = IndexTreeMap::new();
/// tree.insert(1, "a".to_string());
/// tree.insert(2, "b".to_string());
/// assert_eq!(tree.get_last_key_value(), (Some(&2),Some(&"b".to_string())));
/// ```
pub fn get_last_key_value(&self) -> (Option<&K>, Option<&V>) {
self.get_key_value_from_index(self.size - 1)
}
}
impl<K: Key, V: Value> Node<K, V> {
pub fn get_item(&self, key: &K) -> Option<&Item<K, V>> {
for (index, item) in self.items.iter().enumerate() {
match item {
None => return None,
Some(item) => match key.compare_keys(item.key.as_ref()) {
Less => {
if index == 0 {
match &self.children[0] {
None => return None,
Some(child) => return child.get_item(key),
}
}
}
Equal => return Some(item.as_ref()),
Greater => {
if index == ARRAY_SIZE - 1 {
match &self.children[1] {
None => return None,
Some(child) => return child.get_item(key),
}
}
}
},
}
}
None
}
pub fn get_item_from_index(&self, mut id: usize) -> Option<&Item<K, V>> {
if let Some(child) = self.children[0].as_deref() {
if id < self.children_size[0] {
return child.get_item_from_index(id);
} else {
id -= self.children_size[0]
}
}
if id < ARRAY_SIZE {
match self.items[id].as_deref() {
None => return None,
Some(item) => return Some(item),
}
} else {
id -= ARRAY_SIZE;
}
match self.children[1].as_deref() {
None => return None,
Some(child) => {
if id < self.children_size[1] {
return child.get_item_from_index(id);
}
}
}
None
}
}