Debian Codegraph @

src/column.rs

raw symbols SCIP
1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 #[derive(Clone, Debug, PartialEq, Eq)]
9 pub(crate) enum Column<N> {
10 Empty,
11 Blocked,
12 Reserved(N),
13 Ancestor(N),
14 Parent(N),
15 }
16
17 impl<N> Column<N>
18 where
19 N: Clone,
20 {
21 pub(crate) fn matches(&self, n: &N) -> bool
22 where
23 N: Eq,
24 {
25 match self {
26 Column::Empty | Column::Blocked => false,
27 Column::Reserved(o) => n == o,
28 Column::Ancestor(o) => n == o,
29 Column::Parent(o) => n == o,
30 }
31 }
32
33 fn variant(&self) -> usize {
34 match self {
35 Column::Empty => 0,
36 Column::Blocked => 1,
37 Column::Reserved(_) => 2,
38 Column::Ancestor(_) => 3,
39 Column::Parent(_) => 4,
40 }
41 }
42
43 pub(crate) fn merge(&mut self, other: &Column<N>) {
44 if other.variant() > self.variant() {
45 *self = other.clone();
46 }
47 }
48
49 fn reset(&mut self) {
50 match self {
51 Column::Blocked => *self = Column::Empty,
52 _ => {}
53 }
54 }
55 }
56
57 pub(crate) trait ColumnsExt<N> {
58 fn find(&self, node: &N) -> Option<usize>;
59 fn find_empty(&self, index: usize) -> Option<usize>;
60 fn first_empty(&self) -> Option<usize>;
61 fn new_empty(&mut self) -> usize;
62 fn reset(&mut self);
63 }
64
65 impl<N> ColumnsExt<N> for Vec<Column<N>>
66 where
67 N: Clone + Eq,
68 {
69 fn find(&self, node: &N) -> Option<usize> {
70 for (index, column) in self.iter().enumerate() {
71 if column.matches(node) {
72 return Some(index);
73 }
74 }
75 None
76 }
77
78 fn find_empty(&self, index: usize) -> Option<usize> {
79 if self.get(index) == Some(&Column::Empty) {
80 return Some(index);
81 }
82 self.first_empty()
83 }
84
85 fn first_empty(&self) -> Option<usize> {
86 for (i, column) in self.iter().enumerate() {
87 if *column == Column::Empty {
88 return Some(i);
89 }
90 }
91 None
92 }
93
94 fn new_empty(&mut self) -> usize {
95 self.push(Column::Empty);
96 self.len() - 1
97 }
98
99 fn reset(&mut self) {
100 for column in self.iter_mut() {
101 column.reset();
102 }
103 while let Some(Column::Empty) = self.last() {
104 self.pop();
105 }
106 }
107 }