11use super :: Tree ;
2- use crate :: { Eval , TreeNode , node:: Node } ;
2+ use crate :: { Eval , EvalMut , TreeNode , node:: Node } ;
3+
4+ /// Implements the [Eval] trait for [`Tree<T>`] where `T` is `Eval<[V], V>`. All this really does is
5+ /// call the `eval` method on the root node of the [Tree]. The real work is
6+ /// done in the [TreeNode] implementation below.
7+ impl < T , V > Eval < [ V ] , V > for Tree < T >
8+ where
9+ T : Eval < [ V ] , V > ,
10+ V : Clone ,
11+ {
12+ #[ inline]
13+ fn eval ( & self , input : & [ V ] ) -> V {
14+ self . root ( )
15+ . map ( |root| root. eval ( input) )
16+ . unwrap_or_else ( || panic ! ( "Tree has no root node." ) )
17+ }
18+ }
19+
20+ /// Implements the [EvalMut] trait for [`Tree<T>`] where `T` is `Eval<[V], V>`. This is primarily just a simple
21+ /// implementation to satisfy the [regression](crate::regression) use-case where we want to evaluate
22+ /// a `Tree` and return a `Vec` of results. Since a [`Tree<T>`] only has a single root node, we return
23+ /// a `Vec` with a single element. All it really does is call the `eval` method on the root node of the [Tree].
24+ impl < T , V > EvalMut < [ V ] , Vec < V > > for Tree < T >
25+ where
26+ T : Eval < [ V ] , V > ,
27+ V : Clone ,
28+ {
29+ #[ inline]
30+ fn eval_mut ( & mut self , input : & [ V ] ) -> Vec < V > {
31+ vec ! [
32+ self . root( )
33+ . map( |root| root. eval( input) )
34+ . unwrap_or_else( || panic!( "Tree has no root node." ) ) ,
35+ ]
36+ }
37+ }
338
439/// Implements the [Eval] trait for `Vec<Tree<T>>`. This is a wrapper around a `Vec<Tree<T>>`
540/// and allows for the evaluation of each [Tree] in the `Vec` with a single input.
@@ -17,33 +52,35 @@ where
1752 }
1853}
1954
20- /// Implements the [Eval] trait for `Vec<&TreeNode<T>>`. This is a wrapper around a `Vec<&TreeNode<T>>`
21- /// and allows for the evaluation of each [TreeNode] in the `Vec` with a single input.
22- /// The len of the input slice must equal the number of nodes in the `Vec`.
23- impl < T , V > Eval < [ V ] , Vec < V > > for Vec < & TreeNode < T > >
55+ /// Implements the [EvalMut] trait for `Vec<Tree<T>>`. This is a wrapper around a `Vec<Tree<T>>`
56+ /// and allows for the evaluation of each [Tree] in the `Vec` with a single input.
57+ /// This is useful for things like `Ensemble` models where multiple models are used to make a prediction.
58+ ///
59+ /// This is a simple implementation that just maps over the `Vec` and calls [Eval] on each [Tree]. Just like
60+ /// the implementation of [`EvalMut<[V], Vec<V>>`] for [`Tree<T>`] above, this is primarily to satisfy the
61+ /// [regression](crate::regression) use-case.
62+ impl < T , V > EvalMut < [ V ] , Vec < V > > for Vec < Tree < T > >
2463where
2564 T : Eval < [ V ] , V > ,
2665 V : Clone ,
2766{
2867 #[ inline]
29- fn eval ( & self , inputs : & [ V ] ) -> Vec < V > {
30- self . iter ( ) . map ( |node| node . eval ( inputs) ) . collect ( )
68+ fn eval_mut ( & mut self , inputs : & [ V ] ) -> Vec < V > {
69+ self . iter_mut ( ) . map ( |tree| tree . eval ( inputs) ) . collect ( )
3170 }
3271}
3372
34- /// Implements the [Eval] trait for [`Tree<T>`] where `T` is `Eval<[V], V>`. All this really does is
35- /// call the `eval` method on the root node of the [Tree]. The real work is
36- /// done in the [TreeNode] implementation below .
37- impl < T , V > Eval < [ V ] , V > for Tree < T >
73+ /// Implements the [Eval] trait for `Vec<&TreeNode<T>>`. This is a wrapper around a `Vec<&TreeNode<T>>`
74+ /// and allows for the evaluation of each [TreeNode] in the `Vec` with a single input.
75+ /// The len of the input slice must equal the number of nodes in the `Vec` .
76+ impl < T , V > Eval < [ V ] , Vec < V > > for Vec < & TreeNode < T > >
3877where
3978 T : Eval < [ V ] , V > ,
4079 V : Clone ,
4180{
4281 #[ inline]
43- fn eval ( & self , input : & [ V ] ) -> V {
44- self . root ( )
45- . map ( |root| root. eval ( input) )
46- . unwrap_or_else ( || panic ! ( "Tree has no root node." ) )
82+ fn eval ( & self , inputs : & [ V ] ) -> Vec < V > {
83+ self . iter ( ) . map ( |node| node. eval ( inputs) ) . collect ( )
4784 }
4885}
4986
0 commit comments