1use core::{
6 mem::{
7 replace,
8 swap,
9 },
10 ops::{
11 Range,
12 RangeBounds,
13 },
14 ptr::null,
15};
16
17use core::sync::atomic::{
18 AtomicPtr,
19 Ordering,
20};
21
22use crate::{
23 nodes::chunk::Chunk,
24 sync::Lock,
25 util::{
26 to_range,
27 PoolRef,
28 Ref,
29 },
30 vector::{
31 Iter,
32 IterMut,
33 RRBPool,
34 Vector,
35 VectorInner::{
36 Full,
37 Inline,
38 Single,
39 },
40 RRB,
41 },
42};
43
44pub enum Focus<'a, A> {
112 #[doc(hidden)]
113 Single(&'a [A]),
114 #[doc(hidden)]
115 Full(TreeFocus<A>),
116}
117
118impl<'a, A> Focus<'a, A>
119where A: Clone + 'a
120{
121 pub fn new(vector: &'a Vector<A>) -> Self {
125 match &vector.vector {
126 Inline(_, chunk) => Focus::Single(chunk),
127 Single(_, chunk) => Focus::Single(chunk),
128 Full(_, tree) => Focus::Full(TreeFocus::new(tree)),
129 }
130 }
131
132 pub fn len(&self) -> usize {
136 match self {
137 Focus::Single(chunk) => chunk.len(),
138 Focus::Full(tree) => tree.len(),
139 }
140 }
141
142 pub fn is_empty(&self) -> bool { self.len() == 0 }
146
147 pub fn get(&mut self, index: usize) -> Option<&A> {
149 match self {
150 Focus::Single(chunk) => chunk.get(index),
151 Focus::Full(tree) => tree.get(index),
152 }
153 }
154
155 pub fn index(&mut self, index: usize) -> &A {
159 self.get(index).expect("index out of bounds")
160 }
161
162 pub fn chunk_at(&mut self, index: usize) -> (Range<usize>, &[A]) {
167 let len = self.len();
168 if index >= len {
169 panic!("vector::Focus::chunk_at: index out of bounds");
170 }
171 match self {
172 Focus::Single(chunk) => (0..len, chunk),
173 Focus::Full(tree) => tree.get_chunk(index),
174 }
175 }
176
177 pub fn narrow<R>(self, range: R) -> Self
199 where R: RangeBounds<usize> {
200 let r = to_range(&range, self.len());
201 if r.start >= r.end || r.start >= self.len() {
202 panic!("vector::Focus::narrow: range out of bounds");
203 }
204 match self {
205 Focus::Single(chunk) => Focus::Single(&chunk[r]),
206 Focus::Full(tree) => Focus::Full(tree.narrow(r)),
207 }
208 }
209
210 pub fn split_at(self, index: usize) -> (Self, Self) {
239 if index >= self.len() {
240 panic!("vector::Focus::split_at: index out of bounds");
241 }
242 match self {
243 Focus::Single(chunk) => {
244 let (left, right) = chunk.split_at(index);
245 (Focus::Single(left), Focus::Single(right))
246 }
247 Focus::Full(tree) => {
248 let (left, right) = tree.split_at(index);
249 (Focus::Full(left), Focus::Full(right))
250 }
251 }
252 }
253}
254
255impl<'a, A> IntoIterator for Focus<'a, A>
256where A: Clone + 'a
257{
258 type IntoIter = Iter<'a, A>;
259 type Item = &'a A;
260
261 fn into_iter(self) -> Self::IntoIter { Iter::from_focus(self) }
262}
263
264impl<'a, A> Clone for Focus<'a, A>
265where A: Clone + 'a
266{
267 fn clone(&self) -> Self {
268 match self {
269 Focus::Single(chunk) => Focus::Single(chunk),
270 Focus::Full(tree) => Focus::Full(tree.clone()),
271 }
272 }
273}
274
275pub struct TreeFocus<A> {
276 tree: RRB<A>,
277 view: Range<usize>,
278 middle_range: Range<usize>,
279 target_range: Range<usize>,
280 target_ptr: *const Chunk<A>,
281}
282
283impl<A> Clone for TreeFocus<A> {
284 fn clone(&self) -> Self {
285 let tree = self.tree.clone();
286 TreeFocus {
287 view: self.view.clone(),
288 middle_range: self.middle_range.clone(),
289 target_range: 0..0,
290 target_ptr: null(),
291 tree,
292 }
293 }
294}
295
296#[allow(unsafe_code)]
297#[cfg(threadsafe)]
298unsafe impl<A> Send for TreeFocus<A> {}
299#[allow(unsafe_code)]
300#[cfg(threadsafe)]
301unsafe impl<A> Sync for TreeFocus<A> {}
302
303#[inline]
304fn contains<A: Ord>(range: &Range<A>, index: &A) -> bool {
305 *index >= range.start && *index < range.end
306}
307
308impl<A> TreeFocus<A>
309where A: Clone
310{
311 fn new(tree: &RRB<A>) -> Self {
312 let middle_start = tree.outer_f.len() + tree.inner_f.len();
313 let middle_end = middle_start + tree.middle.len();
314 TreeFocus {
315 tree: tree.clone(),
316 view: 0..tree.length,
317 middle_range: middle_start..middle_end,
318 target_range: 0..0,
319 target_ptr: null(),
320 }
321 }
322
323 fn len(&self) -> usize { self.view.end - self.view.start }
324
325 fn narrow(self, mut view: Range<usize>) -> Self {
326 view.start += self.view.start;
327 view.end += self.view.start;
328 TreeFocus {
329 view,
330 middle_range: self.middle_range.clone(),
331 target_range: 0..0,
332 target_ptr: null(),
333 tree: self.tree,
334 }
335 }
336
337 fn split_at(self, index: usize) -> (Self, Self) {
338 let len = self.len();
339 let left = self.clone().narrow(0..index);
340 let right = self.narrow(index..len);
341 (left, right)
342 }
343
344 fn physical_index(&self, index: usize) -> usize {
345 debug_assert!(index < self.view.end);
346 self.view.start + index
347 }
348
349 fn logical_range(&self, range: &Range<usize>) -> Range<usize> {
350 (range.start - self.view.start)..(range.end - self.view.start)
351 }
352
353 fn set_focus(&mut self, index: usize) {
354 if index < self.middle_range.start {
355 let outer_len = self.tree.outer_f.len();
356 if index < outer_len {
357 self.target_range = 0..outer_len;
358 self.target_ptr = &*self.tree.outer_f;
359 }
360 else {
361 self.target_range = outer_len..self.middle_range.start;
362 self.target_ptr = &*self.tree.inner_f;
363 }
364 }
365 else if index >= self.middle_range.end {
366 let outer_start = self.middle_range.end + self.tree.inner_b.len();
367 if index < outer_start {
368 self.target_range = self.middle_range.end..outer_start;
369 self.target_ptr = &*self.tree.inner_b;
370 }
371 else {
372 self.target_range = outer_start..self.tree.length;
373 self.target_ptr = &*self.tree.outer_b;
374 }
375 }
376 else {
377 let tree_index = index - self.middle_range.start;
378 let (range, ptr) =
379 self.tree.middle.lookup_chunk(self.tree.middle_level, 0, tree_index);
380 self.target_range = (range.start + self.middle_range.start)
381 ..(range.end + self.middle_range.start);
382 self.target_ptr = ptr;
383 }
384 }
385
386 #[allow(unsafe_code)]
387 fn get_focus(&self) -> &Chunk<A> { unsafe { &*self.target_ptr } }
388
389 pub fn get(&mut self, index: usize) -> Option<&A> {
390 if index >= self.len() {
391 return None;
392 }
393 let phys_index = self.physical_index(index);
394 if !contains(&self.target_range, &phys_index) {
395 self.set_focus(phys_index);
396 }
397 let target_phys_index = phys_index - self.target_range.start;
398 Some(&self.get_focus()[target_phys_index])
399 }
400
401 pub fn get_chunk(&mut self, index: usize) -> (Range<usize>, &[A]) {
402 let phys_index = self.physical_index(index);
403 if !contains(&self.target_range, &phys_index) {
404 self.set_focus(phys_index);
405 }
406 let mut slice: &[A] = self.get_focus();
407 let mut left = 0;
408 let mut right = 0;
409 if self.target_range.start < self.view.start {
410 left = self.view.start - self.target_range.start;
411 }
412 if self.target_range.end > self.view.end {
413 right = self.target_range.end - self.view.end;
414 }
415 slice = &slice[left..(slice.len() - right)];
416 let phys_range =
417 (self.target_range.start + left)..(self.target_range.end - right);
418 (self.logical_range(&phys_range), slice)
419 }
420}
421
422pub enum FocusMut<'a, A> {
478 #[doc(hidden)]
479 Single(RRBPool<A>, &'a mut [A]),
480 #[doc(hidden)]
481 Full(RRBPool<A>, TreeFocusMut<'a, A>),
482}
483
484impl<'a, A> FocusMut<'a, A>
485where A: Clone + 'a
486{
487 pub fn new(vector: &'a mut Vector<A>) -> Self {
489 match &mut vector.vector {
490 Inline(pool, chunk) => FocusMut::Single(pool.clone(), chunk),
491 Single(pool, chunk) => FocusMut::Single(
492 pool.clone(),
493 PoolRef::make_mut(&pool.value_pool, chunk).as_mut_slice(),
494 ),
495 Full(pool, tree) => FocusMut::Full(pool.clone(), TreeFocusMut::new(tree)),
496 }
497 }
498
499 pub fn len(&self) -> usize {
501 match self {
502 FocusMut::Single(_, chunk) => chunk.len(),
503 FocusMut::Full(_, tree) => tree.len(),
504 }
505 }
506
507 pub fn is_empty(&self) -> bool { self.len() == 0 }
509
510 pub fn get(&mut self, index: usize) -> Option<&A> {
512 self.get_mut(index).map(|r| &*r)
513 }
514
515 pub fn get_mut(&mut self, index: usize) -> Option<&mut A> {
517 match self {
518 FocusMut::Single(_, chunk) => chunk.get_mut(index),
519 FocusMut::Full(pool, tree) => tree.get(pool, index),
520 }
521 }
522
523 pub fn index(&mut self, index: usize) -> &A { &*self.index_mut(index) }
527
528 #[allow(clippy::should_implement_trait)] pub fn index_mut(&mut self, index: usize) -> &mut A {
533 self.get_mut(index).expect("index out of bounds")
534 }
535
536 pub fn set(&mut self, index: usize, value: A) -> Option<A> {
541 match self.get_mut(index) {
542 Some(ref mut pos) => Some(replace(pos, value)),
543 None => None,
544 }
545 }
546
547 pub fn swap(&mut self, a: usize, b: usize) {
553 if a == b {
554 return;
555 }
556 self.pair(a, b, |left, right| swap(left, right));
557 }
558
559 #[allow(unsafe_code)]
577 pub fn pair<F, B>(&mut self, a: usize, b: usize, mut f: F) -> B
578 where F: FnMut(&mut A, &mut A) -> B {
579 if a == b {
580 panic!("vector::FocusMut::pair: indices cannot be equal!");
581 }
582 let pa: *mut A = self.index_mut(a);
583 let pb: *mut A = self.index_mut(b);
584 unsafe { f(&mut *pa, &mut *pb) }
585 }
586
587 #[allow(unsafe_code)]
605 pub fn triplet<F, B>(&mut self, a: usize, b: usize, c: usize, mut f: F) -> B
606 where F: FnMut(&mut A, &mut A, &mut A) -> B {
607 if a == b || b == c || a == c {
608 panic!("vector::FocusMut::triplet: indices cannot be equal!");
609 }
610 let pa: *mut A = self.index_mut(a);
611 let pb: *mut A = self.index_mut(b);
612 let pc: *mut A = self.index_mut(c);
613 unsafe { f(&mut *pa, &mut *pb, &mut *pc) }
614 }
615
616 pub fn chunk_at(&mut self, index: usize) -> (Range<usize>, &mut [A]) {
621 let len = self.len();
622 if index >= len {
623 panic!("vector::FocusMut::chunk_at: index out of bounds");
624 }
625 match self {
626 FocusMut::Single(_, chunk) => (0..len, chunk),
627 FocusMut::Full(pool, tree) => {
628 let (range, chunk) = tree.get_chunk(pool, index);
629 (range, chunk)
630 }
631 }
632 }
633
634 pub fn narrow<R>(self, range: R) -> Self
656 where R: RangeBounds<usize> {
657 let r = to_range(&range, self.len());
658 if r.start > r.end || r.start > self.len() {
659 panic!("vector::FocusMut::narrow: range out of bounds");
660 }
661 match self {
662 FocusMut::Single(pool, chunk) => FocusMut::Single(pool, &mut chunk[r]),
663 FocusMut::Full(pool, tree) => FocusMut::Full(pool, tree.narrow(r)),
664 }
665 }
666
667 #[allow(clippy::redundant_clone)]
702 pub fn split_at(self, index: usize) -> (Self, Self) {
703 if index > self.len() {
704 panic!("vector::FocusMut::split_at: index out of bounds");
705 }
706 match self {
707 FocusMut::Single(pool, chunk) => {
708 let (left, right) = chunk.split_at_mut(index);
709 (FocusMut::Single(pool.clone(), left), FocusMut::Single(pool, right))
710 }
711 FocusMut::Full(pool, tree) => {
712 let (left, right) = tree.split_at(index);
713 (FocusMut::Full(pool.clone(), left), FocusMut::Full(pool, right))
714 }
715 }
716 }
717
718 pub fn unmut(self) -> Focus<'a, A> {
720 match self {
721 FocusMut::Single(_, chunk) => Focus::Single(chunk),
722 FocusMut::Full(_, mut tree) => Focus::Full(TreeFocus {
723 tree: {
724 let t = tree.tree.lock().unwrap();
725 (*t).clone()
726 },
727 view: tree.view.clone(),
728 middle_range: tree.middle_range.clone(),
729 target_range: 0..0,
730 target_ptr: null(),
731 }),
732 }
733 }
734}
735
736impl<'a, A> IntoIterator for FocusMut<'a, A>
737where A: Clone + 'a
738{
739 type IntoIter = IterMut<'a, A>;
740 type Item = &'a mut A;
741
742 fn into_iter(self) -> Self::IntoIter { IterMut::from_focus(self) }
743}
744
745impl<'a, A> Into<Focus<'a, A>> for FocusMut<'a, A>
746where A: Clone + 'a
747{
748 fn into(self) -> Focus<'a, A> { self.unmut() }
749}
750
751pub struct TreeFocusMut<'a, A> {
752 tree: Lock<&'a mut RRB<A>>,
753 view: Range<usize>,
754 middle_range: Range<usize>,
755 target_range: Range<usize>,
756 target_ptr: AtomicPtr<Chunk<A>>,
757}
758
759impl<'a, A> TreeFocusMut<'a, A>
760where A: Clone + 'a
761{
762 fn new(tree: &'a mut RRB<A>) -> Self {
763 let middle_start = tree.outer_f.len() + tree.inner_f.len();
764 let middle_end = middle_start + tree.middle.len();
765 TreeFocusMut {
766 view: 0..tree.length,
767 tree: Lock::new(tree),
768 middle_range: middle_start..middle_end,
769 target_range: 0..0,
770 target_ptr: AtomicPtr::default(),
771 }
772 }
773
774 fn len(&self) -> usize { self.view.end - self.view.start }
775
776 fn narrow(self, mut view: Range<usize>) -> Self {
777 view.start += self.view.start;
778 view.end += self.view.start;
779 TreeFocusMut {
780 view,
781 middle_range: self.middle_range.clone(),
782 target_range: 0..0,
783 target_ptr: AtomicPtr::default(),
784 tree: self.tree,
785 }
786 }
787
788 fn split_at(self, index: usize) -> (Self, Self) {
789 let len = self.len();
790 debug_assert!(index <= len);
791 #[allow(unsafe_code)]
792 let left = TreeFocusMut {
793 view: self.view.start..(self.view.start + index),
794 middle_range: self.middle_range.clone(),
795 target_range: 0..0,
796 target_ptr: AtomicPtr::default(),
797 tree: self.tree.clone(),
798 };
799 let right = TreeFocusMut {
800 view: (self.view.start + index)..(self.view.start + len),
801 middle_range: self.middle_range.clone(),
802 target_range: 0..0,
803 target_ptr: AtomicPtr::default(),
804 tree: self.tree,
805 };
806 (left, right)
807 }
808
809 fn physical_index(&self, index: usize) -> usize {
810 debug_assert!(index < self.view.end);
811 self.view.start + index
812 }
813
814 fn logical_range(&self, range: &Range<usize>) -> Range<usize> {
815 (range.start - self.view.start)..(range.end - self.view.start)
816 }
817
818 fn set_focus(&mut self, pool: &RRBPool<A>, index: usize) {
819 let mut tree = self.tree.lock().expect(
820 "sp_im::vector::Focus::set_focus: unable to acquire exclusive lock on \
821 Vector",
822 );
823 if index < self.middle_range.start {
824 let outer_len = tree.outer_f.len();
825 if index < outer_len {
826 self.target_range = 0..outer_len;
827 self.target_ptr.store(
828 PoolRef::make_mut(&pool.value_pool, &mut tree.outer_f),
829 Ordering::Relaxed,
830 );
831 }
832 else {
833 self.target_range = outer_len..self.middle_range.start;
834 self.target_ptr.store(
835 PoolRef::make_mut(&pool.value_pool, &mut tree.inner_f),
836 Ordering::Relaxed,
837 );
838 }
839 }
840 else if index >= self.middle_range.end {
841 let outer_start = self.middle_range.end + tree.inner_b.len();
842 if index < outer_start {
843 self.target_range = self.middle_range.end..outer_start;
844 self.target_ptr.store(
845 PoolRef::make_mut(&pool.value_pool, &mut tree.inner_b),
846 Ordering::Relaxed,
847 );
848 }
849 else {
850 self.target_range = outer_start..tree.length;
851 self.target_ptr.store(
852 PoolRef::make_mut(&pool.value_pool, &mut tree.outer_b),
853 Ordering::Relaxed,
854 );
855 }
856 }
857 else {
858 let tree_index = index - self.middle_range.start;
859 let level = tree.middle_level;
860 let middle = Ref::make_mut(&mut tree.middle);
861 let (range, ptr) = middle.lookup_chunk_mut(pool, level, 0, tree_index);
862 self.target_range = (range.start + self.middle_range.start)
863 ..(range.end + self.middle_range.start);
864 self.target_ptr.store(ptr, Ordering::Relaxed);
865 }
866 }
867
868 #[allow(unsafe_code)]
869 fn get_focus(&mut self) -> &mut Chunk<A> {
870 unsafe { &mut *self.target_ptr.load(Ordering::Relaxed) }
871 }
872
873 pub fn get(&mut self, pool: &RRBPool<A>, index: usize) -> Option<&mut A> {
874 if index >= self.len() {
875 return None;
876 }
877 let phys_index = self.physical_index(index);
878 if !contains(&self.target_range, &phys_index) {
879 self.set_focus(pool, phys_index);
880 }
881 let target_phys_index = phys_index - self.target_range.start;
882 Some(&mut self.get_focus()[target_phys_index])
883 }
884
885 pub fn get_chunk(
886 &mut self,
887 pool: &RRBPool<A>,
888 index: usize,
889 ) -> (Range<usize>, &mut [A]) {
890 let phys_index = self.physical_index(index);
891 if !contains(&self.target_range, &phys_index) {
892 self.set_focus(pool, phys_index);
893 }
894 let mut left = 0;
895 let mut right = 0;
896 if self.target_range.start < self.view.start {
897 left = self.view.start - self.target_range.start;
898 }
899 if self.target_range.end > self.view.end {
900 right = self.target_range.end - self.view.end;
901 }
902 let phys_range =
903 (self.target_range.start + left)..(self.target_range.end - right);
904 let log_range = self.logical_range(&phys_range);
905 let slice_len = self.get_focus().len();
906 let slice =
907 &mut (self.get_focus().as_mut_slice())[left..(slice_len - right)];
908 (log_range, slice)
909 }
910}