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
use std::{cmp::Ordering, collections::HashMap, hash::BuildHasher};

pub trait AmadeusOrd {
	fn amadeus_cmp(&self, other: &Self) -> Ordering;
}

macro_rules! ord {
	($($t:ty)*) => {$(
		impl AmadeusOrd for $t {
			fn amadeus_cmp(&self, other: &Self) -> Ordering {
				Ord::cmp(self, other)
			}
		}
	)*}
}
ord!(bool u8 i8 u16 i16 u32 i32 u64 i64 String);

impl AmadeusOrd for f32 {
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		Ord::cmp(
			&ordered_float::OrderedFloat(*self),
			&ordered_float::OrderedFloat(*other),
		)
	}
}
impl AmadeusOrd for f64 {
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		Ord::cmp(
			&ordered_float::OrderedFloat(*self),
			&ordered_float::OrderedFloat(*other),
		)
	}
}

impl<T> AmadeusOrd for &T
where
	T: AmadeusOrd + ?Sized,
{
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		(**self).amadeus_cmp(&**other)
	}
}

impl<T> AmadeusOrd for Box<T>
where
	T: AmadeusOrd,
{
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		(**self).amadeus_cmp(&**other)
	}
}

/// Sort `None` as larger than any non-`None` value
impl<T> AmadeusOrd for Option<T>
where
	T: AmadeusOrd,
{
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		match (self, other) {
			(Some(a), Some(b)) => a.amadeus_cmp(b),
			(None, None) => Ordering::Equal,
			(None, Some(_)) => Ordering::Greater,
			(Some(_), None) => Ordering::Less,
		}
	}
}

impl<T> AmadeusOrd for Vec<T>
where
	T: AmadeusOrd,
{
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		for i in 0..self.len().min(other.len()) {
			match self[i].amadeus_cmp(&other[i]) {
				Ordering::Equal => (),
				res => return res,
			}
		}
		self.len().cmp(&other.len())
	}
}

impl<K, V, S> AmadeusOrd for HashMap<K, V, S>
where
	K: AmadeusOrd,
	V: AmadeusOrd,
	S: BuildHasher,
{
	fn amadeus_cmp(&self, other: &Self) -> Ordering {
		let mut keys: Vec<(&K, &V, bool)> = self
			.iter()
			.map(|(k, v)| (k, v, false))
			.chain(other.iter().map(|(k, v)| (k, v, true)))
			.collect();
		keys.sort_by(|(a, _, _), (b, _, _)| a.amadeus_cmp(b));
		let mut keys = &*keys;
		while keys.len() >= 2 {
			let ((a_k, a_v, a_r), (b_k, b_v, b_r)) = (keys[0], keys[1]);
			if !a_r && b_r {
				match a_k.amadeus_cmp(b_k) {
					Ordering::Equal => (),
					res => return res,
				}
				match a_v.amadeus_cmp(b_v) {
					Ordering::Equal => (),
					res => return res,
				}
				keys = &keys[2..];
			} else if !a_r {
				return Ordering::Greater;
			} else {
				return Ordering::Less;
			}
		}
		if keys.len() == 1 {
			if !keys[0].2 {
				Ordering::Greater
			} else {
				Ordering::Less
			}
		} else {
			Ordering::Equal
		}
	}
}

macro_rules! ord {
	($($i:tt)*) => {$(
		impl<T> AmadeusOrd for [T; $i]
		where
			T: AmadeusOrd
		{
			fn amadeus_cmp(&self, other: &Self) -> Ordering {
				for (a,b) in self.iter().zip(other.iter()) {
					match a.amadeus_cmp(b) {
						Ordering::Equal => (),
						res => return res,
					}
				}
				Ordering::Equal
			}
		}
	)*};
}
array!(ord);

macro_rules! ord {
	($len:tt $($t:ident $i:tt)*) => {
		impl<$($t,)*> AmadeusOrd for ($($t,)*) where $($t: AmadeusOrd,)* {
			#[allow(unused_variables)]
			fn amadeus_cmp(&self, other: &Self) -> Ordering {
				Ordering::Equal
					$(.then_with(|| self.$i.amadeus_cmp(&other.$i)))*
			}
		}
	};
}
tuple!(ord);