1use std::collections::HashSet;
9
10use serde::Deserialize;
11
12use crate::Group;
13use crate::Vertex;
14
15#[derive(Default, Debug, Clone, Deserialize)]
17#[serde(transparent)]
18pub struct VertexListWithOptions {
19 list: Vec<(Vertex, VertexOptions)>,
20}
21
22#[derive(Debug, Clone, Deserialize)]
26#[non_exhaustive]
27pub struct VertexOptions {
28 #[serde(default = "Default::default")]
36 pub reserve_size: u32,
37
38 #[serde(default = "default_desired_group")]
49 pub desired_group: Group,
50}
51
52const fn default_desired_group() -> Group {
53 Group::NON_MASTER
54}
55
56impl Default for VertexOptions {
57 fn default() -> Self {
58 Self {
59 reserve_size: 0,
60 desired_group: default_desired_group(),
61 }
62 }
63}
64
65impl<'a> From<&'a [Vertex]> for VertexListWithOptions {
66 fn from(list: &'a [Vertex]) -> Self {
67 Self {
69 list: list
70 .iter()
71 .map(|v| (v.clone(), VertexOptions::default()))
72 .collect(),
73 }
74 }
75}
76
77impl From<Vec<Vertex>> for VertexListWithOptions {
78 fn from(list: Vec<Vertex>) -> Self {
79 Self {
81 list: list
82 .into_iter()
83 .map(|v| (v, VertexOptions::default()))
84 .collect(),
85 }
86 }
87}
88
89impl From<Vec<(Vertex, VertexOptions)>> for VertexListWithOptions {
90 fn from(list: Vec<(Vertex, VertexOptions)>) -> Self {
91 Self { list }
92 }
93}
94
95impl VertexListWithOptions {
96 pub fn vertex_options(&self) -> Vec<(Vertex, VertexOptions)> {
98 self.list.clone()
99 }
100
101 pub fn vertexes(&self) -> Vec<Vertex> {
103 self.list.iter().map(|i| i.0.clone()).collect()
104 }
105
106 pub fn sort_by_group(mut self) -> Self {
108 self.list.sort_by_key(|(_v, opts)| opts.desired_group.0);
109 self
110 }
111
112 pub fn vertexes_by_group(&self, group: Group) -> Vec<Vertex> {
114 self.list
115 .iter()
116 .filter_map(|(v, o)| {
117 if o.desired_group == group {
118 Some(v.clone())
119 } else {
120 None
121 }
122 })
123 .collect()
124 }
125
126 pub fn filter(self, predicate: &dyn Fn(&Vertex, &VertexOptions) -> bool) -> Self {
128 self.list
129 .into_iter()
130 .filter(|v| predicate(&v.0, &v.1))
131 .collect::<Vec<_>>()
132 .into()
133 }
134
135 pub fn try_filter(
137 self,
138 predicate: &dyn Fn(&Vertex, &VertexOptions) -> anyhow::Result<bool>,
139 ) -> anyhow::Result<Self> {
140 Ok(self
141 .list
142 .into_iter()
143 .filter_map(|v| match predicate(&v.0, &v.1) {
144 Err(e) => Some(Err(e)),
145 Ok(true) => Some(Ok(v)),
146 Ok(false) => None,
147 })
148 .collect::<anyhow::Result<Vec<_>>>()?
149 .into())
150 }
151
152 pub fn is_empty(&self) -> bool {
154 self.list.is_empty()
155 }
156
157 pub fn push(&mut self, head_opts: (Vertex, VertexOptions)) {
159 self.list.push(head_opts);
160 }
161
162 pub fn with_desired_group(mut self, group: Group) -> Self {
164 for (_v, opts) in self.list.iter_mut() {
165 opts.desired_group = group;
166 }
167 self
168 }
169
170 pub fn chain(mut self, other: impl Into<Self>) -> Self {
172 let other = other.into();
173 let existed: HashSet<_> = self.vertexes().into_iter().collect();
174 for (v, o) in other.list {
175 if !existed.contains(&v) {
176 self.list.push((v, o))
177 }
178 }
179 self
180 }
181
182 pub fn len(&self) -> usize {
184 self.list.len()
185 }
186
187 pub fn min_desired_group(&self) -> Option<Group> {
189 self.list.iter().map(|v| v.1.desired_group).min()
190 }
191
192 pub fn max_desired_group(&self) -> Option<Group> {
194 self.list.iter().map(|v| v.1.desired_group).max()
195 }
196}