|
| 1 | +use std::{collections::HashMap, fmt::Debug}; |
| 2 | + |
| 3 | +use scope_chat::async_list::AsyncListIndex; |
| 4 | + |
| 5 | +use super::refcacheslice::{self, CacheReferencesSlice, Exists}; |
| 6 | + |
| 7 | +pub struct CacheReferences<I: Clone + Eq + PartialEq> { |
| 8 | + // dense segments are unordered (spooky!) slices of content we do! know about. |
| 9 | + // the u64 in the hashmap represents a kind of "segment identifier" |
| 10 | + dense_segments: HashMap<u64, CacheReferencesSlice<I>>, |
| 11 | + |
| 12 | + top_bounded_identifier: Option<u64>, |
| 13 | + bottom_bounded_identifier: Option<u64>, |
| 14 | +} |
| 15 | + |
| 16 | +impl<I: Clone + Eq + PartialEq> Default for CacheReferences<I> { |
| 17 | + fn default() -> Self { |
| 18 | + Self::new() |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl<I: Clone + Eq + PartialEq> CacheReferences<I> { |
| 23 | + pub fn new() -> Self { |
| 24 | + Self { |
| 25 | + dense_segments: HashMap::new(), |
| 26 | + top_bounded_identifier: None, |
| 27 | + bottom_bounded_identifier: None, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn append_bottom(&mut self, identifier: I) { |
| 32 | + let mut id = None; |
| 33 | + |
| 34 | + for (segment_id, segment) in self.dense_segments.iter() { |
| 35 | + if let Exists::Yes(_) = segment.get(AsyncListIndex::RelativeToBottom(0)) { |
| 36 | + if id.is_some() { |
| 37 | + panic!("There should only be one bottom bound segment"); |
| 38 | + } |
| 39 | + |
| 40 | + id = Some(*segment_id) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if let Some(id) = id { |
| 45 | + self.dense_segments.get_mut(&id).unwrap().append_bottom(identifier); |
| 46 | + } else { |
| 47 | + self.insert(AsyncListIndex::RelativeToBottom(0), identifier, false, true); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn top_bound(&self) -> Option<I> { |
| 52 | + let index = self.top_bounded_identifier?; |
| 53 | + let top_bound = self.dense_segments.get(&index).unwrap(); |
| 54 | + |
| 55 | + assert!(top_bound.is_bounded_at_top); |
| 56 | + |
| 57 | + Some(top_bound.item_references.first().unwrap().clone()) |
| 58 | + } |
| 59 | + |
| 60 | + pub fn bottom_bound(&self) -> Option<I> { |
| 61 | + let index = self.bottom_bounded_identifier?; |
| 62 | + let bottom_bound = self.dense_segments.get(&index).unwrap(); |
| 63 | + |
| 64 | + assert!(bottom_bound.is_bounded_at_bottom); |
| 65 | + |
| 66 | + Some(bottom_bound.item_references.last().unwrap().clone()) |
| 67 | + } |
| 68 | + |
| 69 | + pub fn get(&self, index: AsyncListIndex<I>) -> Exists<I> { |
| 70 | + for segment in self.dense_segments.values() { |
| 71 | + let result = segment.get(index.clone()); |
| 72 | + |
| 73 | + if let Exists::Yes(value) = result { |
| 74 | + return Exists::Yes(value); |
| 75 | + } else if let Exists::No = result { |
| 76 | + return Exists::No; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + Exists::Unknown |
| 81 | + } |
| 82 | + |
| 83 | + /// you mut **KNOW** that the item you are inserting is not: |
| 84 | + /// - directly next to (Before or After) **any** item in the list |
| 85 | + /// - the first or last item in the list |
| 86 | + pub fn insert_detached(&mut self, item: I) { |
| 87 | + self.dense_segments.insert( |
| 88 | + rand::random(), |
| 89 | + CacheReferencesSlice { |
| 90 | + is_bounded_at_top: false, |
| 91 | + is_bounded_at_bottom: false, |
| 92 | + |
| 93 | + item_references: vec![item], |
| 94 | + }, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + pub fn insert(&mut self, index: AsyncListIndex<I>, item: I, is_top: bool, is_bottom: bool) { |
| 99 | + // insert routine is really complex: |
| 100 | + // an insert can "join" together 2 segments |
| 101 | + // an insert can append to a segment |
| 102 | + // or an insert can construct a new segment |
| 103 | + |
| 104 | + let mut segments = vec![]; |
| 105 | + |
| 106 | + for (i, segment) in self.dense_segments.iter() { |
| 107 | + if let Some(position) = segment.can_insert(index.clone()) { |
| 108 | + segments.push((position, *i)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if segments.is_empty() { |
| 113 | + let id = rand::random(); |
| 114 | + |
| 115 | + self.dense_segments.insert( |
| 116 | + id, |
| 117 | + CacheReferencesSlice { |
| 118 | + is_bounded_at_top: is_top, |
| 119 | + is_bounded_at_bottom: is_bottom, |
| 120 | + |
| 121 | + item_references: vec![item], |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + if is_bottom { |
| 126 | + self.bottom_bounded_identifier = Some(id); |
| 127 | + } |
| 128 | + |
| 129 | + if is_top { |
| 130 | + self.top_bounded_identifier = Some(id); |
| 131 | + } |
| 132 | + } else if segments.len() == 1 { |
| 133 | + self.dense_segments.get_mut(&segments[0].1).unwrap().insert(index.clone(), item, is_bottom, is_top); |
| 134 | + |
| 135 | + if is_top { |
| 136 | + self.top_bounded_identifier = Some(segments[0].1) |
| 137 | + } |
| 138 | + if is_bottom { |
| 139 | + self.bottom_bounded_identifier = Some(segments[0].1) |
| 140 | + } |
| 141 | + } else if segments.len() == 2 { |
| 142 | + assert!(!is_top); |
| 143 | + assert!(!is_bottom); |
| 144 | + |
| 145 | + let (li, ri) = match (segments[0], segments[1]) { |
| 146 | + ((refcacheslice::Position::After, lp), (refcacheslice::Position::Before, rp)) => (lp, rp), |
| 147 | + ((refcacheslice::Position::Before, rp), (refcacheslice::Position::After, lp)) => (lp, rp), |
| 148 | + |
| 149 | + _ => panic!("How are there two candidates that aren't (Before, After) or (After, Before)?"), |
| 150 | + }; |
| 151 | + |
| 152 | + let (left, right) = if li < ri { |
| 153 | + let right = self.dense_segments.remove(&ri).unwrap(); |
| 154 | + let left = self.dense_segments.remove(&li).unwrap(); |
| 155 | + |
| 156 | + (left, right) |
| 157 | + } else { |
| 158 | + let left = self.dense_segments.remove(&li).unwrap(); |
| 159 | + let right = self.dense_segments.remove(&ri).unwrap(); |
| 160 | + |
| 161 | + (left, right) |
| 162 | + }; |
| 163 | + |
| 164 | + let mut merged = left.item_references; |
| 165 | + |
| 166 | + merged.push(item); |
| 167 | + |
| 168 | + merged.extend(right.item_references); |
| 169 | + |
| 170 | + let id = rand::random(); |
| 171 | + |
| 172 | + self.dense_segments.insert( |
| 173 | + id, |
| 174 | + CacheReferencesSlice { |
| 175 | + is_bounded_at_top: left.is_bounded_at_top, |
| 176 | + is_bounded_at_bottom: right.is_bounded_at_bottom, |
| 177 | + |
| 178 | + item_references: merged, |
| 179 | + }, |
| 180 | + ); |
| 181 | + |
| 182 | + if left.is_bounded_at_top { |
| 183 | + self.top_bounded_identifier = Some(id); |
| 184 | + } |
| 185 | + |
| 186 | + if right.is_bounded_at_bottom { |
| 187 | + self.bottom_bounded_identifier = Some(id); |
| 188 | + } |
| 189 | + } else { |
| 190 | + panic!("Impossible state") |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +impl<I: Clone + Eq + PartialEq + Debug> Debug for CacheReferences<I> { |
| 196 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 197 | + f.debug_struct("CacheReferences") |
| 198 | + .field("top_bounded_segment", &self.top_bounded_identifier) |
| 199 | + .field("bottom_bounded_segment", &self.bottom_bounded_identifier) |
| 200 | + .field("dense_segments", &self.dense_segments) |
| 201 | + .finish() |
| 202 | + } |
| 203 | +} |
0 commit comments