@@ -2,7 +2,7 @@ use std::fmt;
22use std:: ops:: Range ;
33use std:: rc:: Rc ;
44
5- use super :: { AsRangedCoord , Ranged } ;
5+ use super :: { AsRangedCoord , DiscreteRanged , Ranged } ;
66
77/// The category coordinate
88pub struct Category < T : PartialEq > {
@@ -22,6 +22,25 @@ impl<T: PartialEq> Clone for Category<T> {
2222 }
2323}
2424
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+
2544impl < T : PartialEq + fmt:: Display > fmt:: Debug for Category < T > {
2645 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2746 let element = & self . elements [ self . idx as usize ] ;
@@ -172,6 +191,34 @@ impl<T: PartialEq> Ranged for Category<T> {
172191 }
173192}
174193
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+
175222impl < T : PartialEq > AsRangedCoord for Category < T > {
176223 type CoordDescType = Self ;
177224 type Value = Category < T > ;
0 commit comments