1
1
use std:: collections:: { HashMap , HashSet } ;
2
+ use std:: fmt;
2
3
3
4
use crate :: common:: { ClassifierTarget , Observation } ;
4
5
use num:: Float ;
@@ -47,6 +48,18 @@ pub enum Data<F: Float + std::str::FromStr> {
47
48
Bool ( bool ) ,
48
49
String ( String ) ,
49
50
}
51
+
52
+ impl < F : Float + fmt:: Display + std:: str:: FromStr > fmt:: Display for Data < F > {
53
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
54
+ match self {
55
+ Data :: Scalar ( v) => write ! ( f, "{}" , v) ,
56
+ Data :: Int ( v) => write ! ( f, "{}" , v) ,
57
+ Data :: Bool ( v) => write ! ( f, "{}" , v) ,
58
+ Data :: String ( v) => write ! ( f, "{}" , v) ,
59
+ }
60
+ }
61
+ }
62
+
50
63
impl < F : Float + std:: fmt:: Display + std:: str:: FromStr > Data < F > {
51
64
pub fn to_float ( & self ) -> Result < F , & str > {
52
65
match self {
@@ -72,6 +85,30 @@ pub enum DataStream<F: Float + std::str::FromStr> {
72
85
XY ( HashMap < String , Data < F > > , HashMap < String , Data < F > > ) ,
73
86
}
74
87
88
+ impl < F : Float + fmt:: Display + std:: str:: FromStr > fmt:: Display for DataStream < F > {
89
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
90
+ fn fmt_hashmap < F : Float + fmt:: Display + std:: str:: FromStr > (
91
+ f : & mut fmt:: Formatter < ' _ > ,
92
+ hm : & HashMap < String , Data < F > > ,
93
+ hm_name : & str ,
94
+ ) -> fmt:: Result {
95
+ write ! ( f, "{hm_name}: [" ) ?;
96
+ for ( key, value) in hm {
97
+ write ! ( f, " {}: {}," , key, value) ?;
98
+ }
99
+ write ! ( f, " ] " )
100
+ }
101
+
102
+ match self {
103
+ DataStream :: X ( x) => fmt_hashmap ( f, x, "X" ) ,
104
+ DataStream :: XY ( x, y) => {
105
+ fmt_hashmap ( f, x, "X" ) ?;
106
+ fmt_hashmap ( f, y, "Y" )
107
+ }
108
+ }
109
+ }
110
+ }
111
+
75
112
impl < F : Float + std:: str:: FromStr + std:: fmt:: Display > DataStream < F > {
76
113
pub fn get_x ( & self ) -> & HashMap < String , Data < F > > {
77
114
match self {
0 commit comments