wzrd

An ICCCM & EWMH compliant X11 reparenting, dynamic window manager, written in Rust
git clone git://git.deurzen.net/wzrd
Log | Files | Refs | LICENSE

cycle.rs (34067B)


      1 use crate::change::Direction;
      2 use crate::identify::Ident;
      3 use crate::identify::Identify;
      4 use crate::identify::Index;
      5 use crate::util::BuildIdHasher;
      6 use crate::util::Util;
      7 
      8 use std::cell::Cell;
      9 use std::cell::RefCell;
     10 use std::cmp::Ordering;
     11 use std::collections::HashMap;
     12 use std::collections::VecDeque;
     13 
     14 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
     15 pub enum InsertPos {
     16     BeforeActive,
     17     AfterActive,
     18     BeforeIndex(Index),
     19     AfterIndex(Index),
     20     BeforeIdent(Ident),
     21     AfterIdent(Ident),
     22     Front,
     23     Back,
     24 }
     25 
     26 #[derive(Clone, Copy)]
     27 pub enum Selector<'a, T> {
     28     AtActive,
     29     AtIndex(Index),
     30     AtIdent(Ident),
     31     First,
     32     Last,
     33     ForCond(&'a dyn Fn(&T) -> bool),
     34 }
     35 
     36 #[derive(Clone, Copy, PartialEq)]
     37 enum StackAction {
     38     Insert,
     39     Remove,
     40 }
     41 
     42 #[derive(Debug, Clone, PartialEq)]
     43 struct HistoryStack {
     44     stack: VecDeque<Ident>,
     45 }
     46 
     47 impl HistoryStack {
     48     fn new() -> Self {
     49         HistoryStack {
     50             stack: VecDeque::with_capacity(30),
     51         }
     52     }
     53 
     54     fn clear(&mut self) {
     55         self.stack.clear();
     56     }
     57 
     58     fn push_back(
     59         &mut self,
     60         id: Ident,
     61     ) {
     62         self.stack.push_back(id);
     63     }
     64 
     65     fn pop_back(&mut self) -> Option<Ident> {
     66         self.stack.pop_back()
     67     }
     68 
     69     fn remove_id(
     70         &mut self,
     71         id: Ident,
     72     ) {
     73         if let Some(index) = self.stack.iter().rposition(|&i| i == id) {
     74             self.stack.remove(index);
     75         }
     76     }
     77 
     78     fn as_vecdeque(&self) -> VecDeque<Ident> {
     79         self.stack.clone()
     80     }
     81 
     82     fn as_vec(&self) -> Vec<Ident> {
     83         self.stack.iter().cloned().collect::<Vec<Ident>>()
     84     }
     85 }
     86 
     87 #[derive(Debug, Clone, PartialEq)]
     88 pub struct Cycle<T>
     89 where
     90     T: Identify + std::fmt::Debug,
     91 {
     92     index: Cell<Index>,
     93 
     94     elements: VecDeque<T>,
     95     indices: HashMap<Ident, Index, BuildIdHasher>,
     96 
     97     unwindable: bool,
     98     stack: RefCell<HistoryStack>,
     99 }
    100 
    101 impl<T> Cycle<T>
    102 where
    103     T: Identify + std::fmt::Debug,
    104 {
    105     pub fn new(
    106         elements: Vec<T>,
    107         unwindable: bool,
    108     ) -> Self {
    109         Self {
    110             indices: elements
    111                 .iter()
    112                 .enumerate()
    113                 .map(|(i, e)| (e.id(), i))
    114                 .collect(),
    115 
    116             index: Cell::new(Util::last_index(elements.iter())),
    117             elements: elements.into(),
    118 
    119             unwindable,
    120             stack: RefCell::new(HistoryStack::new()),
    121         }
    122     }
    123 
    124     #[inline]
    125     fn index(&self) -> Option<Index> {
    126         if self.index.get() < self.elements.len() {
    127             Some(self.index.get())
    128         } else {
    129             None
    130         }
    131     }
    132 
    133     pub fn clear(&mut self) {
    134         self.index.set(0);
    135         self.elements.clear();
    136         self.indices.clear();
    137         self.stack.borrow_mut().clear();
    138     }
    139 
    140     #[inline]
    141     pub fn next_will_wrap(
    142         &self,
    143         dir: Direction,
    144     ) -> bool {
    145         self.index.get() == Util::last_index(self.elements.iter()) && dir == Direction::Forward
    146             || self.index.get() == 0 && dir == Direction::Backward
    147     }
    148 
    149     #[inline]
    150     pub fn is_empty(&self) -> bool {
    151         self.elements.is_empty()
    152     }
    153 
    154     #[inline]
    155     pub fn contains(
    156         &self,
    157         element: &T,
    158     ) -> bool {
    159         self.elements.contains(element)
    160     }
    161 
    162     #[inline]
    163     pub fn active_index(&self) -> Index {
    164         self.index.get()
    165     }
    166 
    167     #[inline]
    168     pub fn next_index(
    169         &self,
    170         dir: Direction,
    171     ) -> Index {
    172         self.next_index_from(self.index.get(), dir)
    173     }
    174 
    175     #[inline]
    176     pub fn next_element(
    177         &self,
    178         dir: Direction,
    179     ) -> Option<&T> {
    180         let next_index = self.next_index(dir);
    181         self.get_for(&Selector::AtIndex(next_index))
    182     }
    183 
    184     pub fn cycle_active(
    185         &self,
    186         dir: Direction,
    187     ) -> Option<&T> {
    188         self.push_active_to_stack();
    189         self.index.set(self.next_index(dir));
    190         self.active_element()
    191     }
    192 
    193     pub fn index_for(
    194         &self,
    195         sel: &Selector<'_, T>,
    196     ) -> Option<Index> {
    197         match sel {
    198             Selector::AtActive => Some(self.index.get()),
    199             Selector::AtIndex(index) => {
    200                 if *index < self.len() {
    201                     return Some(*index);
    202                 }
    203 
    204                 None
    205             },
    206             Selector::AtIdent(id) => {
    207                 if let Some(index) = self.id_to_index(*id) {
    208                     return self.index_for(&Selector::AtIndex(index));
    209                 }
    210 
    211                 None
    212             },
    213             Selector::First => Some(0),
    214             Selector::Last => Some(self.elements.len() - 1),
    215             Selector::ForCond(f) => self.by(f).map(|(i, _)| i),
    216         }
    217     }
    218 
    219     #[inline]
    220     pub fn active_element(&self) -> Option<&T> {
    221         self.elements.get(self.index.get())
    222     }
    223 
    224     #[inline]
    225     pub fn active_element_mut(&mut self) -> Option<&mut T> {
    226         self.elements.get_mut(self.index.get())
    227     }
    228 
    229     pub fn rotate(
    230         &mut self,
    231         dir: Direction,
    232     ) {
    233         if !self.elements.is_empty() {
    234             match dir {
    235                 Direction::Forward => self.elements.rotate_right(1),
    236                 Direction::Backward => self.elements.rotate_left(1),
    237             };
    238 
    239             self.indices.clear();
    240             for (i, id) in self.elements.iter().enumerate().map(|(i, e)| (i, e.id())) {
    241                 self.indices.insert(id, i);
    242             }
    243         }
    244     }
    245 
    246     pub fn drag_active(
    247         &mut self,
    248         dir: Direction,
    249     ) -> Option<&T> {
    250         match (self.index.get(), self.next_index(dir), dir) {
    251             (0, _, Direction::Backward) => self.rotate(dir),
    252             (_, 0, Direction::Forward) => self.rotate(dir),
    253             (active, next, _) => {
    254                 let active_id = self.elements.get(active).unwrap().id();
    255                 let next_id = self.elements.get(next).unwrap().id();
    256 
    257                 self.elements.swap(active, next);
    258 
    259                 *self.indices.get_mut(&active_id).unwrap() = next;
    260                 *self.indices.get_mut(&next_id).unwrap() = active;
    261             },
    262         };
    263 
    264         self.cycle_active(dir)
    265     }
    266 
    267     #[inline]
    268     pub fn len(&self) -> usize {
    269         self.elements.len()
    270     }
    271 
    272     pub fn insert_at(
    273         &mut self,
    274         insert_pos: &InsertPos,
    275         element: T,
    276     ) {
    277         match insert_pos {
    278             InsertPos::BeforeActive => self.insert(self.index.get(), element),
    279             InsertPos::AfterActive => {
    280                 self.insert_at(&InsertPos::AfterIndex(self.index.get()), element)
    281             },
    282             InsertPos::BeforeIndex(index) => self.insert(*index, element),
    283             InsertPos::Front => self.push_front(element),
    284             InsertPos::Back => self.push_back(element),
    285             InsertPos::AfterIndex(index) => {
    286                 let next_index = index + 1;
    287 
    288                 if next_index > self.elements.len() {
    289                     self.push_back(element)
    290                 } else {
    291                     self.insert(next_index, element)
    292                 }
    293             },
    294             InsertPos::BeforeIdent(id) => {
    295                 if let Some(index) = self.id_to_index(*id) {
    296                     self.insert_at(&InsertPos::BeforeIndex(index), element);
    297                 }
    298             },
    299             InsertPos::AfterIdent(id) => {
    300                 if let Some(index) = self.id_to_index(*id) {
    301                     self.insert_at(&InsertPos::AfterIndex(index), element);
    302                 }
    303             },
    304         }
    305     }
    306 
    307     pub fn insert(
    308         &mut self,
    309         index: Index,
    310         element: T,
    311     ) {
    312         self.push_active_to_stack();
    313         self.sync_indices(index, StackAction::Insert);
    314         self.indices.insert((&element).id(), index);
    315         self.elements.insert(index, element);
    316         self.index.set(index);
    317     }
    318 
    319     pub fn push_front(
    320         &mut self,
    321         element: T,
    322     ) {
    323         self.push_active_to_stack();
    324         self.sync_indices(0, StackAction::Insert);
    325         self.indices.insert((&element).id(), 0);
    326         self.elements.push_front(element);
    327         self.index.set(0);
    328     }
    329 
    330     pub fn push_back(
    331         &mut self,
    332         element: T,
    333     ) {
    334         let end = self.elements.len();
    335 
    336         self.push_active_to_stack();
    337         self.indices.insert((&element).id(), end);
    338         self.elements.push_back(element);
    339         self.index.set(end);
    340     }
    341 
    342     #[inline]
    343     pub fn iter(&self) -> std::collections::vec_deque::Iter<'_, T> {
    344         self.elements.iter()
    345     }
    346 
    347     #[inline]
    348     pub fn iter_mut(&mut self) -> std::collections::vec_deque::IterMut<'_, T> {
    349         self.elements.iter_mut()
    350     }
    351 
    352     #[inline]
    353     pub fn get(
    354         &self,
    355         index: Index,
    356     ) -> Option<&T> {
    357         self.elements.get(index)
    358     }
    359 
    360     #[inline]
    361     pub fn get_mut(
    362         &mut self,
    363         index: Index,
    364     ) -> Option<&mut T> {
    365         self.elements.get_mut(index)
    366     }
    367 
    368     pub fn get_for(
    369         &self,
    370         sel: &Selector<'_, T>,
    371     ) -> Option<&T> {
    372         match sel {
    373             Selector::AtActive => self.active_element(),
    374             Selector::AtIndex(index) => self.elements.get(*index),
    375             Selector::First => self.elements.get(0),
    376             Selector::Last => self.elements.get(Util::last_index(self.elements.iter())),
    377             Selector::ForCond(f) => self.by(f).map(|(_, e)| e),
    378             Selector::AtIdent(id) => {
    379                 if let Some(index) = self.id_to_index(*id) {
    380                     return self.get_for(&Selector::AtIndex(index));
    381                 }
    382 
    383                 None
    384             },
    385         }
    386     }
    387 
    388     pub fn get_for_mut(
    389         &mut self,
    390         sel: &Selector<'_, T>,
    391     ) -> Option<&mut T> {
    392         match sel {
    393             Selector::AtActive => self.active_element_mut(),
    394             Selector::AtIndex(index) => self.elements.get_mut(*index),
    395             Selector::First => self.elements.get_mut(0),
    396             Selector::Last => self
    397                 .elements
    398                 .get_mut(Util::last_index(self.elements.iter())),
    399             Selector::ForCond(f) => self.by_mut(f).map(|(_, e)| e),
    400             Selector::AtIdent(id) => {
    401                 if let Some(index) = self.id_to_index(*id) {
    402                     return self.get_for_mut(&Selector::AtIndex(index));
    403                 }
    404 
    405                 None
    406             },
    407         }
    408     }
    409 
    410     pub fn get_all_for(
    411         &self,
    412         sel: &Selector<'_, T>,
    413     ) -> Vec<&T> {
    414         match sel {
    415             Selector::AtActive => self.active_element().into_iter().collect(),
    416             Selector::AtIndex(index) => self.elements.get(*index).into_iter().collect(),
    417             Selector::First => self.elements.get(0).into_iter().collect(),
    418             Selector::Last => self
    419                 .elements
    420                 .get(Util::last_index(self.elements.iter()))
    421                 .into_iter()
    422                 .collect(),
    423             Selector::ForCond(f) => self.elements.iter().filter(|e| f(*e)).collect(),
    424             Selector::AtIdent(id) => {
    425                 if let Some(index) = self.id_to_index(*id) {
    426                     return self.get_all_for(&Selector::AtIndex(index));
    427                 }
    428 
    429                 vec![]
    430             },
    431         }
    432     }
    433 
    434     pub fn get_all_for_mut(
    435         &mut self,
    436         sel: &Selector<'_, T>,
    437     ) -> Vec<&mut T> {
    438         match sel {
    439             Selector::AtActive => self.active_element_mut().into_iter().collect(),
    440             Selector::AtIndex(index) => self.elements.get_mut(*index).into_iter().collect(),
    441             Selector::First => self.elements.get_mut(0).into_iter().collect(),
    442             Selector::Last => self
    443                 .elements
    444                 .get_mut(Util::last_index(self.elements.iter()))
    445                 .into_iter()
    446                 .collect(),
    447             Selector::ForCond(f) => self.elements.iter_mut().filter(|e| f(*e)).collect(),
    448             Selector::AtIdent(id) => {
    449                 if let Some(index) = self.id_to_index(*id) {
    450                     return self.get_all_for_mut(&Selector::AtIndex(index));
    451                 }
    452 
    453                 vec![]
    454             },
    455         }
    456     }
    457 
    458     pub fn on_active<F: Fn(&T)>(
    459         &self,
    460         f: F,
    461     ) {
    462         if let Some(element) = self.active_element() {
    463             f(element);
    464         }
    465     }
    466 
    467     pub fn on_active_mut<F: FnMut(&mut T)>(
    468         &mut self,
    469         mut f: F,
    470     ) {
    471         if let Some(element) = self.active_element_mut() {
    472             f(element);
    473         }
    474     }
    475 
    476     pub fn on_all<F: Fn(&T)>(
    477         &self,
    478         f: F,
    479     ) {
    480         for element in self.elements.iter() {
    481             f(element);
    482         }
    483     }
    484 
    485     pub fn on_all_mut<F: FnMut(&mut T)>(
    486         &mut self,
    487         mut f: F,
    488     ) {
    489         for element in self.elements.iter_mut() {
    490             f(element);
    491         }
    492     }
    493 
    494     pub fn on_all_for<F: Fn(&T)>(
    495         &self,
    496         f: F,
    497         sel: &Selector<'_, T>,
    498     ) {
    499         for element in self.get_all_for(sel) {
    500             f(element);
    501         }
    502     }
    503 
    504     pub fn on_all_for_mut<F: FnMut(&mut T)>(
    505         &mut self,
    506         mut f: F,
    507         sel: &Selector<'_, T>,
    508     ) {
    509         for element in self.get_all_for_mut(sel) {
    510             f(element);
    511         }
    512     }
    513 
    514     pub fn activate_for(
    515         &self,
    516         sel: &Selector<'_, T>,
    517     ) -> Option<&T> {
    518         match sel {
    519             Selector::AtActive => self.active_element(),
    520             Selector::AtIndex(index) => {
    521                 self.push_active_to_stack();
    522                 self.index.set(*index);
    523                 self.active_element()
    524             },
    525             Selector::AtIdent(id) => {
    526                 if let Some(index) = self.id_to_index(*id) {
    527                     return self.activate_for(&Selector::AtIndex(index));
    528                 }
    529 
    530                 None
    531             },
    532             Selector::First => {
    533                 self.push_active_to_stack();
    534                 self.index.set(0);
    535                 self.active_element()
    536             },
    537             Selector::Last => {
    538                 self.push_active_to_stack();
    539                 self.index.set(Util::last_index(self.elements.iter()));
    540                 self.active_element()
    541             },
    542             Selector::ForCond(f) => {
    543                 if let Some((index, _)) = self.by(f) {
    544                     self.push_active_to_stack();
    545                     self.index.set(index);
    546                     Some(&self.elements[index])
    547                 } else {
    548                     None
    549                 }
    550             },
    551         }
    552     }
    553 
    554     pub fn remove_for(
    555         &mut self,
    556         sel: &Selector<'_, T>,
    557     ) -> Option<T> {
    558         let (index, element) = match sel {
    559             Selector::AtActive => (self.index.get(), self.elements.remove(self.index.get())),
    560             Selector::AtIndex(index) => (*index, self.elements.remove(*index)),
    561             Selector::AtIdent(id) => {
    562                 if let Some(index) = self.id_to_index(*id) {
    563                     return self.remove_for(&Selector::AtIndex(index));
    564                 }
    565 
    566                 return None;
    567             },
    568             Selector::First => (0, self.elements.remove(0)),
    569             Selector::Last => {
    570                 let end = Util::last_index(self.elements.iter());
    571                 (end, self.elements.remove(end))
    572             },
    573             Selector::ForCond(f) => {
    574                 if let Some((index, _)) = self.by(f) {
    575                     (index, self.elements.remove(index))
    576                 } else {
    577                     return None;
    578                 }
    579             },
    580         };
    581 
    582         self.remove_element(index, &element);
    583         element
    584     }
    585 
    586     pub fn swap(
    587         &mut self,
    588         sel1: &Selector<'_, T>,
    589         sel2: &Selector<'_, T>,
    590     ) {
    591         let index1 = self.index_for(sel1);
    592 
    593         if let Some(index1) = index1 {
    594             let index2 = self.index_for(sel2);
    595 
    596             if let Some(index2) = index2 {
    597                 self.elements.swap(index1, index2);
    598             }
    599         }
    600     }
    601 
    602     fn next_index_from(
    603         &self,
    604         index: Index,
    605         dir: Direction,
    606     ) -> Index {
    607         let end = Util::last_index(self.elements.iter());
    608 
    609         match dir {
    610             Direction::Forward => {
    611                 if index == end {
    612                     0
    613                 } else {
    614                     index + 1
    615                 }
    616             },
    617             Direction::Backward => {
    618                 if index == 0 {
    619                     end
    620                 } else {
    621                     index - 1
    622                 }
    623             },
    624         }
    625     }
    626 
    627     fn sync_indices(
    628         &mut self,
    629         pivot_index: Index,
    630         action: StackAction,
    631     ) {
    632         for index in pivot_index..self.elements.len() {
    633             let id = self.elements.get(index).unwrap().id();
    634 
    635             match action {
    636                 StackAction::Remove => *self.indices.get_mut(&id).unwrap() -= 1,
    637                 StackAction::Insert => *self.indices.get_mut(&id).unwrap() += 1,
    638             }
    639         }
    640 
    641         if action == StackAction::Remove {
    642             match pivot_index.cmp(&self.index.get()) {
    643                 Ordering::Equal => {
    644                     if let Some(id) = self.pop_from_stack() {
    645                         if let Some(index) = self.id_to_index(id) {
    646                             self.index.set(index);
    647                             return;
    648                         }
    649                     }
    650 
    651                     self.index.set(Util::last_index(self.elements.iter()));
    652                 },
    653                 Ordering::Less => {
    654                     let index = self.index.get();
    655 
    656                     if index > 0 {
    657                         self.index.set(index - 1);
    658                     }
    659                 },
    660                 Ordering::Greater => {},
    661             }
    662         }
    663     }
    664 
    665     fn by(
    666         &self,
    667         cond: impl Fn(&T) -> bool,
    668     ) -> Option<(Index, &T)> {
    669         self.elements.iter().enumerate().find(|(_, e)| cond(*e))
    670     }
    671 
    672     fn by_mut(
    673         &mut self,
    674         cond: impl Fn(&T) -> bool,
    675     ) -> Option<(Index, &mut T)> {
    676         self.elements.iter_mut().enumerate().find(|(_, e)| cond(*e))
    677     }
    678 
    679     fn index_of(
    680         &self,
    681         element: T,
    682     ) -> Option<Index> {
    683         self.id_to_index(element.id())
    684     }
    685 
    686     fn index_to_id(
    687         &self,
    688         index: Index,
    689     ) -> Option<Ident> {
    690         if let Some(element) = self.elements.get(index) {
    691             return Some(element.id());
    692         }
    693 
    694         None
    695     }
    696 
    697     fn id_to_index(
    698         &self,
    699         id: Ident,
    700     ) -> Option<Index> {
    701         if let Some(index) = self.indices.get(&id) {
    702             return Some(*index);
    703         }
    704 
    705         None
    706     }
    707 
    708     pub fn stack(&self) -> VecDeque<Ident> {
    709         self.stack.borrow().as_vecdeque()
    710     }
    711 
    712     pub fn stack_after_focus(&self) -> Vec<Ident> {
    713         let mut stack: Vec<Ident> = self.stack.borrow().as_vec();
    714 
    715         if let Some(index) = self.index() {
    716             if let Some(id) = self.index_to_id(index) {
    717                 if let Some(found_index) = stack.iter().rposition(|i| *i == id) {
    718                     stack.remove(found_index);
    719                 }
    720 
    721                 stack.push(id);
    722             }
    723         }
    724 
    725         stack
    726     }
    727 
    728     fn push_index_to_stack(
    729         &self,
    730         index: Option<Index>,
    731     ) {
    732         if !self.unwindable {
    733             return;
    734         }
    735 
    736         if let Some(index) = index {
    737             if let Some(id) = self.index_to_id(index) {
    738                 let mut stack = self.stack.borrow_mut();
    739                 stack.remove_id(id);
    740                 stack.push_back(id);
    741             }
    742         }
    743     }
    744 
    745     #[inline]
    746     fn push_active_to_stack(&self) {
    747         if !self.unwindable {
    748             return;
    749         }
    750 
    751         self.push_index_to_stack(self.index());
    752     }
    753 
    754     fn remove_element(
    755         &mut self,
    756         index: Index,
    757         element: &Option<T>,
    758     ) {
    759         if let Some(element) = element {
    760             let id = element.id();
    761 
    762             self.indices.remove(&id);
    763             self.remove_from_stack(id);
    764             self.sync_indices(index, StackAction::Remove);
    765         }
    766     }
    767 
    768     #[inline]
    769     fn remove_from_stack(
    770         &self,
    771         id: Ident,
    772     ) {
    773         if !self.unwindable {
    774             return;
    775         }
    776 
    777         self.stack.borrow_mut().remove_id(id);
    778     }
    779 
    780     #[inline]
    781     fn pop_from_stack(&self) -> Option<Ident> {
    782         if !self.unwindable {
    783             return None;
    784         }
    785 
    786         self.stack.borrow_mut().pop_back()
    787     }
    788 }
    789 
    790 impl<T: PartialEq + Identify + std::fmt::Debug> Cycle<T> {
    791     pub fn equivalent_selectors(
    792         &self,
    793         sel1: &Selector<'_, T>,
    794         sel2: &Selector<'_, T>,
    795     ) -> bool {
    796         match (self.index_for(&sel1), self.index_for(&sel2)) {
    797             (Some(e), Some(f)) => e == f,
    798             _ => false,
    799         }
    800     }
    801 }
    802 
    803 impl<T: Clone + Identify + std::fmt::Debug> Cycle<T> {
    804     #[allow(dead_code)]
    805     pub fn as_vec(&self) -> Vec<T> {
    806         self.iter().cloned().collect()
    807     }
    808 }
    809 
    810 impl<T: Identify + std::fmt::Debug> std::ops::Index<Index> for Cycle<T> {
    811     type Output = T;
    812 
    813     fn index(
    814         &self,
    815         index: Index,
    816     ) -> &Self::Output {
    817         &self.elements[index]
    818     }
    819 }
    820 
    821 impl<T: Identify + std::fmt::Debug> std::ops::IndexMut<Index> for Cycle<T> {
    822     fn index_mut(
    823         &mut self,
    824         index: Index,
    825     ) -> &mut Self::Output {
    826         &mut self.elements[index]
    827     }
    828 }
    829 
    830 #[cfg(test)]
    831 mod tests {
    832     use super::*;
    833 
    834     mod i32 {
    835         impl super::Identify for i32 {
    836             fn id(&self) -> super::Ident {
    837                 *self as super::Ident
    838             }
    839         }
    840     }
    841 
    842     #[test]
    843     fn removing_element_before_focus() {
    844         let mut cycle = Cycle::new(vec![0, 10, 20, 30, 40, 50, 60], false);
    845 
    846         assert_eq!(cycle.index.get(), 6);
    847 
    848         cycle.remove_for(&Selector::AtIndex(2));
    849 
    850         assert_eq!(cycle.index.get(), 5);
    851         assert_eq!(cycle.indices.get(&0), Some(&0));
    852         assert_eq!(cycle.indices.get(&10), Some(&1));
    853         assert_eq!(cycle.indices.get(&20), None);
    854         assert_eq!(cycle.indices.get(&30), Some(&2));
    855         assert_eq!(cycle.indices.get(&40), Some(&3));
    856         assert_eq!(cycle.indices.get(&50), Some(&4));
    857         assert_eq!(cycle.indices.get(&60), Some(&5));
    858 
    859         cycle.remove_for(&Selector::AtIndex(2));
    860 
    861         assert_eq!(cycle.index.get(), 4);
    862         assert_eq!(cycle.indices.get(&0), Some(&0));
    863         assert_eq!(cycle.indices.get(&10), Some(&1));
    864         assert_eq!(cycle.indices.get(&20), None);
    865         assert_eq!(cycle.indices.get(&30), None);
    866         assert_eq!(cycle.indices.get(&40), Some(&2));
    867         assert_eq!(cycle.indices.get(&50), Some(&3));
    868         assert_eq!(cycle.indices.get(&60), Some(&4));
    869 
    870         cycle.remove_for(&Selector::AtIndex(2));
    871 
    872         assert_eq!(cycle.index.get(), 3);
    873         assert_eq!(cycle.indices.get(&0), Some(&0));
    874         assert_eq!(cycle.indices.get(&10), Some(&1));
    875         assert_eq!(cycle.indices.get(&20), None);
    876         assert_eq!(cycle.indices.get(&30), None);
    877         assert_eq!(cycle.indices.get(&40), None);
    878         assert_eq!(cycle.indices.get(&50), Some(&2));
    879         assert_eq!(cycle.indices.get(&60), Some(&3));
    880 
    881         cycle.remove_for(&Selector::AtIndex(2));
    882 
    883         assert_eq!(cycle.index.get(), 2);
    884         assert_eq!(cycle.indices.get(&0), Some(&0));
    885         assert_eq!(cycle.indices.get(&10), Some(&1));
    886         assert_eq!(cycle.indices.get(&20), None);
    887         assert_eq!(cycle.indices.get(&30), None);
    888         assert_eq!(cycle.indices.get(&40), None);
    889         assert_eq!(cycle.indices.get(&50), None);
    890         assert_eq!(cycle.indices.get(&60), Some(&2));
    891 
    892         cycle.remove_for(&Selector::AtIndex(2));
    893 
    894         assert_eq!(cycle.index.get(), 1);
    895         assert_eq!(cycle.indices.get(&0), Some(&0));
    896         assert_eq!(cycle.indices.get(&10), Some(&1));
    897         assert_eq!(cycle.indices.get(&20), None);
    898         assert_eq!(cycle.indices.get(&30), None);
    899         assert_eq!(cycle.indices.get(&40), None);
    900         assert_eq!(cycle.indices.get(&50), None);
    901         assert_eq!(cycle.indices.get(&60), None);
    902 
    903         cycle.remove_for(&Selector::AtIndex(2));
    904 
    905         assert_eq!(cycle.index.get(), 1);
    906         assert_eq!(cycle.indices.get(&0), Some(&0));
    907         assert_eq!(cycle.indices.get(&10), Some(&1));
    908         assert_eq!(cycle.indices.get(&20), None);
    909         assert_eq!(cycle.indices.get(&30), None);
    910         assert_eq!(cycle.indices.get(&40), None);
    911         assert_eq!(cycle.indices.get(&50), None);
    912         assert_eq!(cycle.indices.get(&60), None);
    913 
    914         cycle.remove_for(&Selector::AtIndex(1));
    915 
    916         assert_eq!(cycle.index.get(), 0);
    917         assert_eq!(cycle.indices.get(&0), Some(&0));
    918         assert_eq!(cycle.indices.get(&10), None);
    919         assert_eq!(cycle.indices.get(&20), None);
    920         assert_eq!(cycle.indices.get(&30), None);
    921         assert_eq!(cycle.indices.get(&40), None);
    922         assert_eq!(cycle.indices.get(&50), None);
    923         assert_eq!(cycle.indices.get(&60), None);
    924 
    925         cycle.remove_for(&Selector::AtIndex(0));
    926 
    927         assert_eq!(cycle.index.get(), 0);
    928         assert_eq!(cycle.indices.get(&0), None);
    929         assert_eq!(cycle.indices.get(&10), None);
    930         assert_eq!(cycle.indices.get(&20), None);
    931         assert_eq!(cycle.indices.get(&30), None);
    932         assert_eq!(cycle.indices.get(&40), None);
    933         assert_eq!(cycle.indices.get(&50), None);
    934         assert_eq!(cycle.indices.get(&60), None);
    935     }
    936 
    937     #[test]
    938     fn removing_last_element_at_focus() {
    939         let mut cycle = Cycle::new(vec![0, 10, 20, 30, 40, 50, 60], false);
    940 
    941         assert_eq!(cycle.index.get(), 6);
    942 
    943         cycle.remove_for(&Selector::AtIndex(6));
    944 
    945         assert_eq!(cycle.index.get(), 5);
    946         assert_eq!(cycle.indices.get(&0), Some(&0));
    947         assert_eq!(cycle.indices.get(&10), Some(&1));
    948         assert_eq!(cycle.indices.get(&20), Some(&2));
    949         assert_eq!(cycle.indices.get(&30), Some(&3));
    950         assert_eq!(cycle.indices.get(&40), Some(&4));
    951         assert_eq!(cycle.indices.get(&50), Some(&5));
    952         assert_eq!(cycle.indices.get(&60), None);
    953 
    954         cycle.remove_for(&Selector::AtIndex(6));
    955 
    956         assert_eq!(cycle.index.get(), 5);
    957         assert_eq!(cycle.indices.get(&0), Some(&0));
    958         assert_eq!(cycle.indices.get(&10), Some(&1));
    959         assert_eq!(cycle.indices.get(&20), Some(&2));
    960         assert_eq!(cycle.indices.get(&30), Some(&3));
    961         assert_eq!(cycle.indices.get(&40), Some(&4));
    962         assert_eq!(cycle.indices.get(&50), Some(&5));
    963         assert_eq!(cycle.indices.get(&60), None);
    964 
    965         cycle.remove_for(&Selector::AtIndex(5));
    966 
    967         assert_eq!(cycle.index.get(), 4);
    968         assert_eq!(cycle.indices.get(&0), Some(&0));
    969         assert_eq!(cycle.indices.get(&10), Some(&1));
    970         assert_eq!(cycle.indices.get(&20), Some(&2));
    971         assert_eq!(cycle.indices.get(&30), Some(&3));
    972         assert_eq!(cycle.indices.get(&40), Some(&4));
    973         assert_eq!(cycle.indices.get(&50), None);
    974         assert_eq!(cycle.indices.get(&60), None);
    975 
    976         cycle.remove_for(&Selector::AtIndex(4));
    977 
    978         assert_eq!(cycle.index.get(), 3);
    979         assert_eq!(cycle.indices.get(&0), Some(&0));
    980         assert_eq!(cycle.indices.get(&10), Some(&1));
    981         assert_eq!(cycle.indices.get(&20), Some(&2));
    982         assert_eq!(cycle.indices.get(&30), Some(&3));
    983         assert_eq!(cycle.indices.get(&40), None);
    984         assert_eq!(cycle.indices.get(&50), None);
    985         assert_eq!(cycle.indices.get(&60), None);
    986 
    987         cycle.remove_for(&Selector::AtIndex(3));
    988 
    989         assert_eq!(cycle.index.get(), 2);
    990         assert_eq!(cycle.indices.get(&0), Some(&0));
    991         assert_eq!(cycle.indices.get(&10), Some(&1));
    992         assert_eq!(cycle.indices.get(&20), Some(&2));
    993         assert_eq!(cycle.indices.get(&30), None);
    994         assert_eq!(cycle.indices.get(&40), None);
    995         assert_eq!(cycle.indices.get(&50), None);
    996         assert_eq!(cycle.indices.get(&60), None);
    997 
    998         cycle.remove_for(&Selector::AtIndex(2));
    999 
   1000         assert_eq!(cycle.index.get(), 1);
   1001         assert_eq!(cycle.indices.get(&0), Some(&0));
   1002         assert_eq!(cycle.indices.get(&10), Some(&1));
   1003         assert_eq!(cycle.indices.get(&20), None);
   1004         assert_eq!(cycle.indices.get(&30), None);
   1005         assert_eq!(cycle.indices.get(&40), None);
   1006         assert_eq!(cycle.indices.get(&50), None);
   1007         assert_eq!(cycle.indices.get(&60), None);
   1008 
   1009         cycle.remove_for(&Selector::AtIndex(1));
   1010 
   1011         assert_eq!(cycle.index.get(), 0);
   1012         assert_eq!(cycle.indices.get(&0), Some(&0));
   1013         assert_eq!(cycle.indices.get(&10), None);
   1014         assert_eq!(cycle.indices.get(&20), None);
   1015         assert_eq!(cycle.indices.get(&30), None);
   1016         assert_eq!(cycle.indices.get(&40), None);
   1017         assert_eq!(cycle.indices.get(&50), None);
   1018         assert_eq!(cycle.indices.get(&60), None);
   1019 
   1020         cycle.remove_for(&Selector::AtIndex(0));
   1021 
   1022         assert_eq!(cycle.index.get(), 0);
   1023         assert_eq!(cycle.indices.get(&0), None);
   1024         assert_eq!(cycle.indices.get(&10), None);
   1025         assert_eq!(cycle.indices.get(&20), None);
   1026         assert_eq!(cycle.indices.get(&30), None);
   1027         assert_eq!(cycle.indices.get(&40), None);
   1028         assert_eq!(cycle.indices.get(&50), None);
   1029         assert_eq!(cycle.indices.get(&60), None);
   1030 
   1031         cycle.remove_for(&Selector::AtIndex(0));
   1032 
   1033         assert_eq!(cycle.index.get(), 0);
   1034         assert_eq!(cycle.indices.get(&0), None);
   1035         assert_eq!(cycle.indices.get(&10), None);
   1036         assert_eq!(cycle.indices.get(&20), None);
   1037         assert_eq!(cycle.indices.get(&30), None);
   1038         assert_eq!(cycle.indices.get(&40), None);
   1039         assert_eq!(cycle.indices.get(&50), None);
   1040         assert_eq!(cycle.indices.get(&60), None);
   1041     }
   1042 
   1043     #[test]
   1044     fn removing_first_element_at_focus() {
   1045         let mut cycle = Cycle::new(vec![0, 10, 20, 30, 40, 50, 60], false);
   1046 
   1047         assert_eq!(cycle.index.get(), 6);
   1048         cycle.activate_for(&Selector::AtIndex(0));
   1049         assert_eq!(cycle.index.get(), 0);
   1050 
   1051         cycle.remove_for(&Selector::AtIndex(0));
   1052 
   1053         assert_eq!(cycle.index.get(), 5);
   1054         assert_eq!(cycle.indices.get(&0), None);
   1055         assert_eq!(cycle.indices.get(&10), Some(&0));
   1056         assert_eq!(cycle.indices.get(&20), Some(&1));
   1057         assert_eq!(cycle.indices.get(&30), Some(&2));
   1058         assert_eq!(cycle.indices.get(&40), Some(&3));
   1059         assert_eq!(cycle.indices.get(&50), Some(&4));
   1060         assert_eq!(cycle.indices.get(&60), Some(&5));
   1061 
   1062         cycle.activate_for(&Selector::AtIndex(0));
   1063         assert_eq!(cycle.index.get(), 0);
   1064 
   1065         cycle.remove_for(&Selector::AtIndex(0));
   1066 
   1067         assert_eq!(cycle.index.get(), 4);
   1068         assert_eq!(cycle.indices.get(&0), None);
   1069         assert_eq!(cycle.indices.get(&10), None);
   1070         assert_eq!(cycle.indices.get(&20), Some(&0));
   1071         assert_eq!(cycle.indices.get(&30), Some(&1));
   1072         assert_eq!(cycle.indices.get(&40), Some(&2));
   1073         assert_eq!(cycle.indices.get(&50), Some(&3));
   1074         assert_eq!(cycle.indices.get(&60), Some(&4));
   1075 
   1076         cycle.activate_for(&Selector::AtIndex(0));
   1077         assert_eq!(cycle.index.get(), 0);
   1078 
   1079         cycle.remove_for(&Selector::AtIndex(0));
   1080 
   1081         assert_eq!(cycle.index.get(), 3);
   1082         assert_eq!(cycle.indices.get(&0), None);
   1083         assert_eq!(cycle.indices.get(&10), None);
   1084         assert_eq!(cycle.indices.get(&20), None);
   1085         assert_eq!(cycle.indices.get(&30), Some(&0));
   1086         assert_eq!(cycle.indices.get(&40), Some(&1));
   1087         assert_eq!(cycle.indices.get(&50), Some(&2));
   1088         assert_eq!(cycle.indices.get(&60), Some(&3));
   1089 
   1090         cycle.activate_for(&Selector::AtIndex(0));
   1091         assert_eq!(cycle.index.get(), 0);
   1092 
   1093         cycle.remove_for(&Selector::AtIndex(0));
   1094 
   1095         assert_eq!(cycle.index.get(), 2);
   1096         assert_eq!(cycle.indices.get(&0), None);
   1097         assert_eq!(cycle.indices.get(&10), None);
   1098         assert_eq!(cycle.indices.get(&20), None);
   1099         assert_eq!(cycle.indices.get(&30), None);
   1100         assert_eq!(cycle.indices.get(&40), Some(&0));
   1101         assert_eq!(cycle.indices.get(&50), Some(&1));
   1102         assert_eq!(cycle.indices.get(&60), Some(&2));
   1103 
   1104         cycle.activate_for(&Selector::AtIndex(0));
   1105         assert_eq!(cycle.index.get(), 0);
   1106 
   1107         cycle.remove_for(&Selector::AtIndex(0));
   1108 
   1109         assert_eq!(cycle.index.get(), 1);
   1110         assert_eq!(cycle.indices.get(&0), None);
   1111         assert_eq!(cycle.indices.get(&10), None);
   1112         assert_eq!(cycle.indices.get(&20), None);
   1113         assert_eq!(cycle.indices.get(&30), None);
   1114         assert_eq!(cycle.indices.get(&40), None);
   1115         assert_eq!(cycle.indices.get(&50), Some(&0));
   1116         assert_eq!(cycle.indices.get(&60), Some(&1));
   1117 
   1118         cycle.activate_for(&Selector::AtIndex(0));
   1119         assert_eq!(cycle.index.get(), 0);
   1120 
   1121         cycle.remove_for(&Selector::AtIndex(0));
   1122 
   1123         assert_eq!(cycle.index.get(), 0);
   1124         assert_eq!(cycle.indices.get(&0), None);
   1125         assert_eq!(cycle.indices.get(&10), None);
   1126         assert_eq!(cycle.indices.get(&20), None);
   1127         assert_eq!(cycle.indices.get(&30), None);
   1128         assert_eq!(cycle.indices.get(&40), None);
   1129         assert_eq!(cycle.indices.get(&50), None);
   1130         assert_eq!(cycle.indices.get(&60), Some(&0));
   1131 
   1132         cycle.activate_for(&Selector::AtIndex(0));
   1133         assert_eq!(cycle.index.get(), 0);
   1134 
   1135         cycle.remove_for(&Selector::AtIndex(0));
   1136 
   1137         assert_eq!(cycle.index.get(), 0);
   1138         assert_eq!(cycle.indices.get(&0), None);
   1139         assert_eq!(cycle.indices.get(&10), None);
   1140         assert_eq!(cycle.indices.get(&20), None);
   1141         assert_eq!(cycle.indices.get(&30), None);
   1142         assert_eq!(cycle.indices.get(&40), None);
   1143         assert_eq!(cycle.indices.get(&50), None);
   1144         assert_eq!(cycle.indices.get(&60), None);
   1145 
   1146         cycle.activate_for(&Selector::AtIndex(0));
   1147         assert_eq!(cycle.index.get(), 0);
   1148 
   1149         cycle.remove_for(&Selector::AtIndex(0));
   1150 
   1151         assert_eq!(cycle.index.get(), 0);
   1152         assert_eq!(cycle.indices.get(&0), None);
   1153         assert_eq!(cycle.indices.get(&10), None);
   1154         assert_eq!(cycle.indices.get(&20), None);
   1155         assert_eq!(cycle.indices.get(&30), None);
   1156         assert_eq!(cycle.indices.get(&40), None);
   1157         assert_eq!(cycle.indices.get(&50), None);
   1158         assert_eq!(cycle.indices.get(&60), None);
   1159     }
   1160 }