normal-form 0.1.1

A trait to easily find the canonical/normal form of graph structures.
Documentation
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
use crate::{set::Map, Set};
use derivative::Derivative;
use std::fmt;
use std::ops::Deref;

#[derive(Clone, Copy)]
pub struct Bound {
	pub offset: usize,
	pub depth: usize,
}

impl PartialEq for Bound {
	fn eq(&self, other: &Self) -> bool {
		self.offset == other.offset
	}
}

impl Eq for Bound {}

impl Bound {
	fn new(offset: usize) -> Self {
		Self { offset, depth: 0 }
	}
}

/// Coloring.
///
/// A coloring for the set `S` is a list `(W_1, W_2, ..., W_m)` such
/// that `{ W_1, W_2, ..., W_m }` is a coloring of `S`.
/// Each `W_i` is called a *cell* of the coloring.
#[derive(Derivative)]
#[derivative(Clone(bound = ""), PartialEq(bound = ""), Eq(bound = ""))]
pub struct Coloring<S: Set + ?Sized> {
	/// Elements of the partitioned set,
	/// ordered by cell index first, `S::Item::cmp` second (in each cell).
	elements: Vec<S::Item>,

	/// Bounds of the coloring.
	///
	/// If the partitioned set is { e_1, ..., e_n } then
	/// the list of bounds [ i_1, ..., i_k ] produces the coloring
	/// { { e_0, ..., e_{i_0} }, { e_{i_0 + 1}, .., e_{i_1}  }, ..., { e_{i_k + 1}, .. e_n } }
	///
	/// The resulting coloring contains `k` members, the length of the list.
	bounds: Vec<Bound>,
}

impl<S: Set + ?Sized> Coloring<S> {
	/// Creates a new unit coloring of the input `set`.
	pub fn new(set: &S) -> Self {
		let mut elements: Vec<_> = set.iter().collect();
		elements.sort_unstable();
		Self {
			elements,
			bounds: Vec::new(),
		}
	}

	#[cfg(test)]
	fn from_parts(elements: Vec<S::Item>, bounds: Vec<Bound>) -> Self {
		Self { elements, bounds }
	}

	/// Creates a new coloring from a map associating each item to an initial
	/// color of type `C`.
	pub fn from_map<C: Ord>(set: &S, map: &S::Map<C>) -> Self {
		// Sort elements by color.
		let mut elements: Vec<_> = set.iter().collect();
		elements.sort_unstable_by_key(|e| map.get(e).unwrap());

		// Create colors.
		let mut bounds = Vec::new();
		for (i, w) in elements.windows(2).enumerate() {
			if map.get(&w[0]) != map.get(&w[1]) {
				bounds.push(Bound::new(i + 1))
			}
		}

		let mut result = Self { elements, bounds };

		result.sort_cells();

		result
	}

	fn reset_bounds(&mut self) {
		for b in &mut self.bounds {
			b.depth = 0
		}
	}

	/// Sort the elements in each cell.
	///
	/// This invariant should always be preserved,
	/// so this function is called to enforce it whenever an operation
	/// may have broken it.
	fn sort_cells(&mut self) {
		let mut start = 0;
		for &end in &self.bounds {
			self.elements[start..end.offset].sort_unstable();
			start = end.offset
		}
		self.elements[start..].sort_unstable()
	}

	pub fn color_index_of(&self, item: &S::Item) -> Option<usize> {
		for (i, color) in self.colors().enumerate() {
			if color.contains(item) {
				return Some(i);
			}
		}

		None
	}

	pub fn colors(&self) -> Colors<S> {
		Colors {
			coloring: self,
			i: 0,
		}
	}

	/// Returns the number of colors in the coloring.
	pub fn len(&self) -> usize {
		if self.is_empty() {
			0
		} else {
			self.bounds.len() + 1
		}
	}

	pub fn is_empty(&self) -> bool {
		self.elements.is_empty()
	}

	/// Checks if the coloring is a unit coloring.
	pub fn is_unit(&self) -> bool {
		self.bounds.is_empty()
	}

	pub fn is_discrete(&self) -> bool {
		debug_assert!(self.len() <= self.elements.len());
		self.len() == self.elements.len()
	}

	pub fn color_start(&self, i: usize) -> Option<usize> {
		if i == 0 {
			Some(0)
		} else {
			Some(self.bounds.get(&(i - 1))?.offset)
		}
	}

	/// Returns the `i`th cell of the coloring, partitioning the given
	/// input `set`.
	pub fn get(&self, i: usize) -> Option<&[S::Item]> {
		let start = if i == 0 {
			0
		} else {
			self.bounds.get(&(i - 1))?.offset
		};

		match self.bounds.get(&i) {
			Some(end) => Some(&self.elements[start..end.offset]),
			None => Some(&self.elements[start..]),
		}
	}

	/// Returns the `i`th cell of the coloring, partitioning the given
	/// input `set`.
	fn get_mut(&mut self, i: usize) -> Option<&mut [S::Item]> {
		let start = if i == 0 {
			0
		} else {
			self.bounds.get(&(i - 1))?.offset
		};

		match self.bounds.get(&i) {
			Some(end) => Some(&mut self.elements[start..end.offset]),
			None => Some(&mut self.elements[start..]),
		}
	}
}

impl<S: Set + ?Sized> fmt::Debug for Coloring<S>
where
	S::Item: fmt::Debug,
{
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{{")?;

		for (i, color) in self.colors().enumerate() {
			if i > 0 {
				write!(f, ", ")?;
			}

			write!(f, "{:?}", color)?;
		}

		write!(f, "}}")
	}
}

pub struct Colors<'a, S: Set + ?Sized> {
	coloring: &'a Coloring<S>,
	i: usize,
}

impl<'a, S: Set + ?Sized> Iterator for Colors<'a, S> {
	type Item = &'a [S::Item];

	fn next(&mut self) -> Option<Self::Item> {
		if self.i < self.coloring.len() {
			let cell = self.coloring.get(self.i).unwrap();
			self.i += 1;
			Some(cell)
		} else {
			None
		}
	}
}

trait StartedRange {
	fn start(&self) -> usize;
}

impl StartedRange for std::ops::Range<usize> {
	fn start(&self) -> usize {
		self.start
	}
}

impl StartedRange for std::ops::RangeInclusive<usize> {
	fn start(&self) -> usize {
		*self.start()
	}
}

impl StartedRange for std::ops::RangeFrom<usize> {
	fn start(&self) -> usize {
		self.start
	}
}

/// A reversible ordered coloring.
#[derive(Derivative)]
#[derivative(Clone(bound = "S::Map<usize>: Clone"))]
pub struct ReversibleColoring<S: Set + ?Sized> {
	/// Coloring.
	coloring: Coloring<S>,

	/// Associates each element to the index of its cell in the coloring.
	reverse: S::Map<usize>,

	depth: usize,
}

impl<S: Set + ?Sized> PartialEq for ReversibleColoring<S> {
	fn eq(&self, other: &Self) -> bool {
		self.coloring == other.coloring
	}
}

impl<S: Set + ?Sized> Eq for ReversibleColoring<S> {}

impl<S: Set + ?Sized> fmt::Debug for ReversibleColoring<S>
where
	S::Item: fmt::Debug,
{
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.coloring.fmt(f)
	}
}

impl<S: Set + ?Sized> ReversibleColoring<S> {
	/// Creates a new reversible coloring.
	pub fn new(set: &S) -> Self {
		Self {
			coloring: Coloring::new(set),
			reverse: set.map(|_| 0),
			depth: 0,
		}
	}

	pub fn from_coloring(set: &S, mut coloring: Coloring<S>) -> Self {
		coloring.reset_bounds();
		let reverse = set.map(|item| coloring.color_index_of(item).unwrap());
		Self {
			coloring,
			reverse,
			depth: 0,
		}
	}

	pub fn color_index_of(&self, item: &S::Item) -> Option<usize> {
		self.reverse.get(item).cloned()
	}

	fn check(&self) -> bool {
		for (i, color) in self.colors().enumerate() {
			if color.windows(2).any(|a| a[0] > a[1]) {
				// not sorted
				return false;
			}

			for t in color {
				let reverse = self.color_index_of(t);
				if reverse != Some(i) {
					return false;
				}
			}
		}

		true
	}

	/// Returns the permutation represented by this coloring, if any.
	///
	/// If the coloring *discrete* then it defines a mapping
	/// `p : x -> self.reverse(x)` that is a permutation of the partitioned set.
	/// In that case, `Some(p)` is returned.
	/// Otherwise, if the coloring is not discrete, `None` is returned.
	pub fn as_permutation(&self) -> Option<&S::Map<usize>> {
		if self.coloring.is_discrete() {
			Some(&self.reverse)
		} else {
			None
		}
	}

	/// Refine the coloring such that `item` is in its own cell of size 1.
	///
	/// Returns `true` if the individualization succeeded,
	/// or `false` if `item` was already individualized.
	pub fn individualize(&mut self, item: &S::Item) -> bool {
		let color_index = self.color_index_of(item).unwrap();
		let color_start = self.color_start(color_index).unwrap();
		let color = self.coloring.get_mut(color_index).unwrap();

		if color.len() > 1 {
			let i = color.binary_search(item).unwrap();
			color.swap(0, i);
			color[1..].sort_unstable();
			self.coloring.bounds.insert(
				color_index,
				Bound {
					offset: color_start + 1,
					depth: self.depth,
				},
			);
			self.reverse.map(|t, c| {
				if c < color_index || t == item {
					c
				} else {
					c + 1
				}
			});

			debug_assert!(self.check());
			true
		} else {
			false
		}
	}

	pub fn deindividualize(&mut self, item: &S::Item) -> bool {
		let color_index = self.color_index_of(item).unwrap();
		let color = self.coloring.get_mut(color_index).unwrap();

		if color.len() == 1 && color_index < self.coloring.bounds.len() {
			self.coloring.bounds.remove(color_index);
			self.coloring.get_mut(color_index).unwrap().sort_unstable();
			self.reverse
				.map(|_, c| if c <= color_index { c } else { c - 1 });
			true
		} else {
			false
		}
	}

	pub(crate) fn begin(&mut self) {
		self.depth += 1;
	}

	pub(crate) fn restore(&mut self, n: usize) {
		let restored_depth = self.depth - n;
		self.retain_bounds(|b| b.depth <= restored_depth);
		self.depth = restored_depth;
	}

	pub(crate) fn depth(&self) -> usize {
		self.depth
	}

	fn retain_bounds<P>(&mut self, predicate: P) -> bool
	where
		P: FnMut(&Bound) -> bool,
	{
		let len = self.len();
		self.coloring.bounds.retain(predicate);
		if self.len() != len {
			self.coloring.sort_cells();
			for (c, color) in self.coloring.colors().enumerate() {
				for i in color {
					self.reverse.set(i, c);
				}
			}

			debug_assert!(self.check());
			true
		} else {
			false
		}
	}

	/// Checks if `self` is a finer than or equal to `other`, assuming they
	/// target the same set.
	pub fn is_finer_or_equal_to(&self, other: &Self) -> bool {
		let mut last_index = 0;

		for cell in self.coloring.colors() {
			let (first_item, cell_rest) = cell.split_first().unwrap();
			let index = other.color_index_of(first_item).unwrap();

			if index < last_index {
				return false;
			}

			if !cell_rest
				.iter()
				.all(|item| other.color_index_of(item).unwrap() == index)
			{
				return false;
			}

			last_index = index
		}

		true
	}

	pub fn refine<F, C: Ord>(&mut self, f: F) -> bool
	where
		F: Fn(&S::Item) -> C,
	{
		self.refine_with(&mut Vec::new(), f)
	}

	/// Refine this coloring using the given sub-coloring.
	///
	/// The `refined_colors` array will be expended to include the newly added colors indexes.
	/// Colors that have not been refined are not added to the array, even if their index changes.
	/// If the array already contains old color indexes, they will be updated in place to
	/// the new color index.
	pub fn refine_with<F, C: Ord>(&mut self, refined_colors: &mut Vec<usize>, f: F) -> bool
	where
		F: Fn(&S::Item) -> C,
	{
		#[allow(clippy::too_many_arguments)]
		fn refine_color<S: Set + ?Sized, F, C: Ord, R>(
			coloring: &mut Coloring<S>,
			reverse: &mut S::Map<usize>,
			depth: usize,
			refined_colors: &mut Vec<usize>,
			already_refined_len: usize,
			f: F,
			range: R,
			old_color_index: usize,
			mut new_color_index: usize,
		) -> usize
		where
			F: Fn(&S::Item) -> C,
			R: StartedRange + Clone,
			Vec<S::Item>: std::ops::IndexMut<R, Output = [S::Item]>
				+ std::ops::IndexMut<usize, Output = S::Item>,
		{
			coloring.elements[range.clone()].sort_unstable_by_key(|i| f(i));
			reverse.set(&coloring.elements[range.start()], new_color_index);
			for (i, w) in coloring.elements[range.clone()].windows(2).enumerate() {
				if f(&w[0]) != f(&w[1]) {
					refined_colors.push(new_color_index);
					new_color_index += 1;
					coloring.bounds.push(Bound {
						offset: range.start() + i + 1,
						depth,
					})
				}
				reverse.set(&w[1], new_color_index);
			}

			if old_color_index != new_color_index {
				let mut present = false;
				for c in &mut refined_colors[..already_refined_len] {
					if *c == old_color_index {
						*c = new_color_index;
						present = true;
						break;
					}
				}

				if !present {
					refined_colors.push(new_color_index);
				}
			}

			new_color_index
		}

		let already_refined_len = refined_colors.len();
		let len = self.len();
		let mut old_bounds = Vec::new();
		std::mem::swap(&mut old_bounds, &mut self.coloring.bounds);
		let mut start = 0;
		let mut old_color_index = 0;
		let mut new_color_index = 0;
		for end in old_bounds {
			new_color_index = refine_color(
				&mut self.coloring,
				&mut self.reverse,
				self.depth,
				refined_colors,
				already_refined_len,
				&f,
				start..end.offset,
				old_color_index,
				new_color_index,
			);

			self.coloring.bounds.push(end);
			old_color_index += 1;
			new_color_index += 1;
			start = end.offset
		}

		refine_color(
			&mut self.coloring,
			&mut self.reverse,
			self.depth,
			refined_colors,
			already_refined_len,
			&f,
			start..,
			old_color_index,
			new_color_index,
		);

		self.coloring.sort_cells();
		debug_assert!(self.check());
		self.len() != len
	}

	pub fn make_equitable<'i, F, I>(&mut self, set: &S, neighbors: F)
	where
		F: Fn(&S::Item) -> I,
		I: IntoIterator<Item = &'i S::Item>,
		S::Item: 'i,
	{
		let mut stack = Vec::new();
		let mut map = set.map(|_| 0);

		self.make_equitable_with(&mut stack, &mut map, neighbors)
	}

	/// Make this coloring equitable.
	///
	/// The stack is empty when this function returns.
	/// The map must be initialized so that each element is mapped (the value
	/// does not matter).
	pub fn make_equitable_with<'i, F, I>(
		&mut self,
		stack: &mut Vec<usize>,
		map: &mut S::Map<usize>,
		neighbors: F,
	) where
		F: Fn(&S::Item) -> I,
		I: IntoIterator<Item = &'i S::Item>,
		S::Item: 'i,
	{
		debug_assert_eq!(map.len(), self.reverse.len());
		stack.clear();
		stack.extend(0..self.len());

		while !stack.is_empty() && !self.is_discrete() {
			let color = stack.pop().unwrap();

			// For the given color, we associate for each element
			// the number of edges that connects to a element of
			// this color.
			map.map(|i, _| {
				let mut count = 0;
				for j in neighbors(i) {
					let j_color = self.color_index_of(j).unwrap();
					if j_color == color {
						count += 1
					}
				}
				count
			});

			// Refine the partition using the computed information.
			// This will also update the stack with the new colors.
			self.refine_with(stack, |i| map.get(i));
		}

		stack.clear()
	}
}

impl<S: Set + ?Sized> Deref for ReversibleColoring<S> {
	type Target = Coloring<S>;

	fn deref(&self) -> &Self::Target {
		&self.coloring
	}
}

#[cfg(test)]
mod tests {
	// macro_rules! coloring {
	// 	{ $([ $($i:expr),* ]),* } => {
	// 		{
	// 			let mut elements = Vec::new();
	// 			let mut bounds = Vec::new();

	// 			$(
	// 				if !elements.is_empty() {
	// 					bounds.push($crate::coloring::Bound::new(elements.len()));
	// 				}

	// 				$(
	// 					elements.push($i);
	// 				)*
	// 			)*

	// 			$crate::coloring::Coloring::from_parts(
	// 				elements,
	// 				bounds
	// 			)
	// 		}
	// 	};
	// }

	macro_rules! rcoloring {
		{ $set:tt : $([ $($i:expr),* ]),* } => {
			{
				let mut elements = Vec::new();
				let mut bounds = Vec::new();

				$(
					if !elements.is_empty() {
						bounds.push($crate::coloring::Bound::new(elements.len()));
					}

					$(
						elements.push($i);
					)*
				)*

				$crate::coloring::ReversibleColoring::from_coloring(&$set, $crate::coloring::Coloring::from_parts(
					elements,
					bounds
				))
			}
		};
	}

	#[test]
	fn individualize_01() {
		let mut coloring = rcoloring! { 1usize : [ 0 ] };
		coloring.individualize(&0);
		assert_eq!(coloring, rcoloring! { 1usize : [ 0 ] })
	}

	#[test]
	fn deindividualize_01() {
		let mut coloring = rcoloring! { 1usize : [ 0 ] };
		coloring.deindividualize(&0);
		assert_eq!(coloring, rcoloring! { 1usize : [ 0 ] })
	}

	#[test]
	fn individualize_02() {
		let mut coloring = rcoloring! { 2usize : [ 0, 1 ] };
		coloring.individualize(&0);
		assert_eq!(coloring, rcoloring! { 2usize : [ 0 ], [ 1 ] })
	}

	#[test]
	fn deindividualize_02() {
		let mut coloring = rcoloring! { 2usize : [ 0 ], [ 1 ] };
		coloring.deindividualize(&0);
		assert_eq!(coloring, rcoloring! { 2usize : [ 0, 1 ] })
	}

	#[test]
	fn individualize_03() {
		let mut coloring = rcoloring! { 2usize : [ 0, 1 ] };
		coloring.individualize(&1);
		assert_eq!(coloring, rcoloring! { 2usize : [ 1 ], [ 0 ] })
	}

	#[test]
	fn deindividualize_03() {
		let mut coloring = rcoloring! { 2usize : [ 1 ], [ 0 ] };
		coloring.deindividualize(&1);
		assert_eq!(coloring, rcoloring! { 2usize : [ 0, 1 ] })
	}

	#[test]
	fn individualize_04() {
		let mut coloring = rcoloring! { 3usize : [ 0, 1, 2 ] };
		coloring.individualize(&0);
		assert_eq!(coloring, rcoloring! { 3usize : [ 0 ], [ 1, 2 ] })
	}

	#[test]
	fn deindividualize_04() {
		let mut coloring = rcoloring! { 3usize : [ 0 ], [ 1, 2 ] };
		coloring.deindividualize(&0);
		assert_eq!(coloring, rcoloring! { 3usize : [ 0, 1, 2 ] })
	}

	#[test]
	fn individualize_05() {
		let mut coloring = rcoloring! { 3usize : [ 0, 1, 2 ] };
		coloring.individualize(&1);
		assert_eq!(coloring, rcoloring! { 3usize : [ 1 ], [ 0, 2 ] })
	}

	#[test]
	fn deindividualize_05() {
		let mut coloring = rcoloring! { 3usize : [ 1 ], [ 0, 2 ] };
		coloring.deindividualize(&1);
		assert_eq!(coloring, rcoloring! { 3usize : [ 0, 1, 2 ] })
	}

	#[test]
	fn individualize_06() {
		let mut coloring = rcoloring! { 3usize : [ 0, 1, 2 ] };
		coloring.individualize(&2);
		assert_eq!(coloring, rcoloring! { 3usize : [ 2 ], [ 0, 1 ] })
	}

	#[test]
	fn deindividualize_06() {
		let mut coloring = rcoloring! { 3usize : [ 2 ], [ 0, 1 ] };
		coloring.deindividualize(&2);
		assert_eq!(coloring, rcoloring! { 3usize : [ 0, 1, 2 ] })
	}

	#[test]
	fn refine_01() {
		let mut coloring = rcoloring! { 3usize : [ 0, 1, 2 ] };
		coloring.refine(|i| match i {
			0 => 0,
			1 => 1,
			2 => 2,
			_ => unreachable!(),
		});

		assert_eq!(coloring, rcoloring! { 3usize : [ 0 ], [ 1 ], [ 2 ] })
	}

	#[test]
	fn refine_02() {
		let mut coloring = rcoloring! { 3usize : [ 0, 1, 2 ] };
		coloring.refine(|i| match i {
			0 => 0,
			1 => 0,
			2 => 1,
			_ => unreachable!(),
		});

		assert_eq!(coloring, rcoloring! { 3usize : [ 0, 1 ], [ 2 ] })
	}

	#[test]
	fn refine_03() {
		let mut coloring = rcoloring! { 3usize : [ 0, 1, 2 ] };
		coloring.refine(|i| match i {
			0 => 1,
			1 => 1,
			2 => 0,
			_ => unreachable!(),
		});

		assert_eq!(coloring, rcoloring! { 3usize : [ 2 ], [ 0, 1 ] })
	}

	#[test]
	fn refine_04() {
		let mut coloring = rcoloring! { 4usize : [ 0, 1 ], [ 2, 3 ] };
		coloring.refine(|i| match i {
			0 => 0,
			1 => 1,
			2 => 0,
			3 => 1,
			_ => unreachable!(),
		});

		assert_eq!(coloring, rcoloring! { 4usize : [ 0 ], [ 1 ], [ 2 ], [ 3 ] })
	}

	#[test]
	fn refine_05() {
		let mut coloring = rcoloring! { 4usize : [ 0, 1 ], [ 2, 3 ] };
		coloring.refine(|i| match i {
			0 => 0,
			1 => 1,
			2 => 2,
			3 => 2,
			_ => unreachable!(),
		});

		assert_eq!(coloring, rcoloring! { 4usize : [ 0 ], [ 1 ], [ 2, 3 ] })
	}

	#[test]
	fn make_equitable_01() {
		let mut coloring = rcoloring! { 3usize : [ 0 ], [ 1, 2 ] };
		coloring.make_equitable(&3, |i| match i {
			0 => &[1usize] as &[_],
			1 => &[0],
			2 => &[],
			_ => unreachable!(),
		});

		assert_eq!(coloring, rcoloring! { 3 : [ 0 ], [ 2 ], [ 1 ] })
	}
}