@@ -2,7 +2,7 @@ use std::fmt;
2
2
use std:: ops:: Range ;
3
3
use std:: rc:: Rc ;
4
4
5
- use super :: { AsRangedCoord , Ranged } ;
5
+ use super :: { AsRangedCoord , DiscreteRanged , Ranged } ;
6
6
7
7
/// The category coordinate
8
8
pub struct Category < T : PartialEq > {
@@ -22,6 +22,25 @@ impl<T: PartialEq> Clone for Category<T> {
22
22
}
23
23
}
24
24
25
+ impl < T : PartialEq > std:: cmp:: PartialEq for Category < T > {
26
+ fn eq ( & self , other : & Self ) -> bool {
27
+ self . name == other. name && self . elements == other. elements && self . idx == other. idx
28
+ }
29
+ }
30
+
31
+ impl < T : std:: hash:: Hash + Eq > std:: hash:: Hash for Category < T > {
32
+ fn hash < H > ( & self , state : & mut H )
33
+ where
34
+ H : std:: hash:: Hasher ,
35
+ {
36
+ self . name . hash ( state) ;
37
+ self . idx . hash ( state) ;
38
+ self . elements . iter ( ) . for_each ( |ele| ele. hash ( state) ) ;
39
+ }
40
+ }
41
+
42
+ impl < T : Eq > std:: cmp:: Eq for Category < T > { }
43
+
25
44
impl < T : PartialEq + fmt:: Display > fmt:: Debug for Category < T > {
26
45
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
27
46
let element = & self . elements [ self . idx as usize ] ;
@@ -172,6 +191,34 @@ impl<T: PartialEq> Ranged for Category<T> {
172
191
}
173
192
}
174
193
194
+ impl < T : PartialEq > DiscreteRanged for Category < T > {
195
+ type RangeParameter = ( ) ;
196
+
197
+ fn get_range_parameter ( & self ) { }
198
+
199
+ /// Get the smallest value that is larger than the `this` value
200
+ fn next_value ( this : & Self :: ValueType , _param : & ( ) ) -> Self :: ValueType {
201
+ let mut out = this. clone ( ) ;
202
+ out. idx += 1 ;
203
+ if out. idx >= out. elements . len ( ) as i32 {
204
+ // If we need to wrap around
205
+ out. idx = 0 ;
206
+ }
207
+ out
208
+ }
209
+
210
+ /// Get the largest value that is smaller than `this` value
211
+ fn previous_value ( this : & Self :: ValueType , _param : & ( ) ) -> Self :: ValueType {
212
+ let mut out = this. clone ( ) ;
213
+ out. idx -= 1 ;
214
+ if out. idx < 0 {
215
+ // If we need to wrap around
216
+ out. idx = ( out. elements . len ( ) - 1 ) as i32 ;
217
+ }
218
+ out
219
+ }
220
+ }
221
+
175
222
impl < T : PartialEq > AsRangedCoord for Category < T > {
176
223
type CoordDescType = Self ;
177
224
type Value = Category < T > ;
0 commit comments