aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/dependency_resolver/rule_watch_chain.rs
blob: aa2d7f7279446690c42f6d967a28b25035ab3663 (plain)
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
//! ref: composer/src/Composer/DependencyResolver/RuleWatchChain.php

use crate::dependency_resolver::RuleWatchNode;

/// An extension of SplDoublyLinkedList with seek and removal of current element.
#[derive(Debug)]
pub struct RuleWatchChain {
    data: Vec<std::rc::Rc<std::cell::RefCell<RuleWatchNode>>>,
    current_offset: usize,
}

impl RuleWatchChain {
    pub fn new() -> Self {
        Self {
            data: Vec::new(),
            current_offset: 0,
        }
    }

    pub(crate) fn rewind(&mut self) {
        self.current_offset = 0;
    }

    pub(crate) fn next(&mut self) {
        self.current_offset += 1;
    }

    pub(crate) fn valid(&self) -> bool {
        self.current_offset < self.data.len()
    }

    pub(crate) fn current(&self) -> &std::rc::Rc<std::cell::RefCell<RuleWatchNode>> {
        &self.data[self.current_offset]
    }

    pub(crate) fn unshift(&mut self, node: std::rc::Rc<std::cell::RefCell<RuleWatchNode>>) {
        self.data.insert(0, node);
    }

    fn key(&self) -> usize {
        self.current_offset
    }

    fn offset_unset(&mut self, offset: usize) {
        self.data.remove(offset);
    }

    /// Moves the internal iterator to the specified offset.
    pub fn seek(&mut self, offset: usize) {
        self.rewind();
        for _ in 0..offset {
            self.next();
        }
    }

    /// Removes the current element from the list.
    pub fn remove(&mut self) {
        let offset = self.key();
        self.offset_unset(offset);
        self.seek(offset);
    }
}