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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Paths and routes may not share certain features.
use n18hex::HexFace;
use n18map::HexAddress;
use n18tile::Connection;
/// A rule defines which elements of a path or route may not be shared.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ConflictRule {
/// No track segment (including hex faces) in common.
TrackOnly,
/// No track segment or city (including dits) in common.
TrackOrCity,
/// No track segment, or any city (including dits) on the same hex.
TrackOrCityHex,
/// No hexes in common.
Hex,
}
/// A specific element of a path or route that cannot be shared.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Conflict {
/// A face on a specific hex.
Face { addr: HexAddress, face: HexFace },
/// A specific track segment on a specific hex.
Track { addr: HexAddress, ix: usize },
/// A specific city on a specific hex.
City { addr: HexAddress, ix: usize },
/// A specific dit on a specific hex.
Dit { addr: HexAddress, ix: usize },
/// Any city on a specific hex.
CityHex { addr: HexAddress },
/// A specific hex.
Hex { addr: HexAddress },
}
// pub type RouteConflicts = rc_hash::RouteConflicts;
pub type RouteConflicts = rc_vec::RouteConflicts;
pub mod rc_hash {
use super::Conflict;
use std::collections::BTreeSet;
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct RouteConflicts {
rc: BTreeSet<Conflict>,
}
impl RouteConflicts {
pub fn new() -> Self {
RouteConflicts {
rc: BTreeSet::new(),
}
}
pub fn is_disjoint(&self, other: &Self) -> bool {
self.rc.is_disjoint(&other.rc)
}
pub fn merge(&self, other: &Self) -> Self {
RouteConflicts {
rc: self.rc.union(&other.rc).copied().collect(),
}
}
pub fn len(&self) -> usize {
self.rc.len()
}
pub fn is_empty(&self) -> bool {
self.rc.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &Conflict> {
self.rc.iter()
}
}
impl From<BTreeSet<Conflict>> for RouteConflicts {
fn from(set: BTreeSet<Conflict>) -> Self {
RouteConflicts { rc: set }
}
}
impl From<&BTreeSet<Conflict>> for RouteConflicts {
fn from(set: &BTreeSet<Conflict>) -> Self {
let rc = set.iter().copied().collect();
RouteConflicts { rc }
}
}
}
pub mod rc_vec {
use super::Conflict;
use std::collections::BTreeSet;
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct RouteConflicts {
rc: Vec<Conflict>,
}
impl RouteConflicts {
pub fn new() -> Self {
RouteConflicts { rc: Vec::new() }
}
pub fn is_disjoint(&self, other: &Self) -> bool {
use std::cmp::Ordering;
let a = &self.rc;
let b = &other.rc;
let la = a.len();
let lb = b.len();
let mut ixa = 0;
let mut ixb = 0;
loop {
if ixa >= la || ixb >= lb {
return true;
}
match a[ixa].cmp(&b[ixb]) {
Ordering::Less => ixa += 1,
Ordering::Greater => ixb += 1,
Ordering::Equal => return false,
}
}
}
// NOTE: alternate implementation, which requires itertools.
// pub fn merge(&self, other: &Self) -> Self {
// let conflict_set: std::collections::BTreeSet<Conflict> =
// self.rc.iter().merge(other.rc.iter()).map(|c| *c).collect();
// let mut result: Vec<Conflict> =
// conflict_set.into_iter().collect();
// result.sort();
// return Self { rc: result };
// }
pub fn merge(&self, other: &Self) -> Self {
// NOTE: both slices are already sorted.
use std::cmp::Ordering;
let a = &self.rc;
let b = &other.rc;
let la = a.len();
let lb = b.len();
let mut rc: Vec<Conflict> = Vec::with_capacity(la + lb);
let mut ixa = 0;
let mut ixb = 0;
loop {
if ixa >= la {
while ixb < lb {
rc.push(b[ixb]);
ixb += 1;
}
break;
} else if ixb >= lb {
while ixa < la {
rc.push(a[ixa]);
ixa += 1;
}
break;
}
match a[ixa].cmp(&b[ixb]) {
Ordering::Less => {
rc.push(a[ixa]);
ixa += 1
}
Ordering::Greater => {
rc.push(b[ixb]);
ixb += 1
}
Ordering::Equal => {
// We know that a[ixa] == b[ixb].
rc.push(a[ixa]);
ixa += 1;
ixb += 1;
}
}
}
RouteConflicts { rc }
}
pub fn len(&self) -> usize {
self.rc.len()
}
pub fn is_empty(&self) -> bool {
self.rc.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = &Conflict> {
self.rc.iter()
}
}
impl From<BTreeSet<Conflict>> for RouteConflicts {
fn from(set: BTreeSet<Conflict>) -> Self {
let mut rc: Vec<_> = set.iter().copied().collect();
rc.sort();
RouteConflicts { rc }
}
}
impl From<&BTreeSet<Conflict>> for RouteConflicts {
fn from(set: &BTreeSet<Conflict>) -> Self {
let mut rc: Vec<_> = set.iter().copied().collect();
rc.sort();
RouteConflicts { rc }
}
}
}
impl ConflictRule {
/// Returns the conflict that this connection adds to a path or route.
pub fn maybe_conflict(
&self,
addr: &HexAddress,
conn: &Connection,
) -> Option<Conflict> {
use ConflictRule::*;
use Connection::*;
// NOTE: not trivial, need to return the most general conflict.
match conn {
Track { end: _, .. } => match self {
Hex => Some(Conflict::Hex { addr: *addr }),
// NOTE: since every track segment connects to a hex face, and
// two track segments that connect to the same hex face are
// considered to share some track, we can ignore track segment
// conflicts and only record hex face conflicts.
// This will introduce errors if there are track segments that
// do not connect to a hex face.
_ => None,
},
Face { face } => match face {
// NOTE: since hex face conflicts are defined according to the
// map orientation, we always have an upper face and a lower
// face, and only need to record one of these two faces (but
// note that both faces will be passed to this function).
// Here, we choose to record the upper face.
HexFace::Top | HexFace::UpperLeft | HexFace::UpperRight => {
Some(Conflict::Face {
addr: *addr,
face: *face,
})
}
_ => None,
},
Dit { ix } => match self {
Hex => Some(Conflict::Hex { addr: *addr }),
TrackOrCityHex => Some(Conflict::CityHex { addr: *addr }),
TrackOrCity => Some(Conflict::Dit {
addr: *addr,
ix: *ix,
}),
TrackOnly => None,
},
City { ix } => match self {
Hex => Some(Conflict::Hex { addr: *addr }),
TrackOrCityHex => Some(Conflict::CityHex { addr: *addr }),
TrackOrCity => Some(Conflict::City {
addr: *addr,
ix: *ix,
}),
TrackOnly => None,
},
}
}
}
#[cfg(test)]
mod tests {
use super::ConflictRule;
#[test]
/// Check that the conflict rules have the desired ordering.
/// This is necessary for ensuring that route-combining constraints are
/// **at least as flexible** as the route-building constraints.
/// That is, `route_combining_rule <= route_building_rule`.
fn rule_ordering_1() {
use ConflictRule::*;
assert!(TrackOnly < TrackOrCity);
assert!(TrackOnly < TrackOrCityHex);
assert!(TrackOnly < Hex);
assert!(TrackOrCity < TrackOrCityHex);
assert!(TrackOrCity < Hex);
assert!(TrackOrCityHex < Hex);
}
}