C++ Template Metaprogramming
Solutions to the exercises throughout the book
Loading...
Searching...
No Matches
exercise-6-4.hpp
Go to the documentation of this file.
1// ===-- chapter-6/exercise-6-4.hpp ----------------------- -*- C++ -*- --=== //
9#ifndef EXERCISE_6_4
10#define EXERCISE_6_4
11
12#include "chapter-5-tree.hpp"
13#include "exercise-6-3.hpp"
14
15#include <boost/mpl/end.hpp>
16#include <boost/mpl/equal_to.hpp>
17#include <boost/mpl/greater.hpp>
18#include <boost/mpl/if.hpp>
19
20
42namespace exercise_6_4 {
43
47{
48 typedef tree_end type;
49};
50
51} // namespace exercise_6_4
52
53
54namespace boost {
55namespace mpl {
56
59
61template<>
62struct end_impl<chapter5::tree_tag>
63{
64 template <typename S> struct apply : exercise_6_4::tree_end { };
65};
66
68
69} // namespace mpl
70} // namespace boost
71
72
73namespace exercise_6_4 {
74
87template <typename S, typename Elem>
89 : boost::mpl::eval_if<
90 boost::mpl::equal_to<Elem, S>,
91 S,
92 boost::mpl::end< chapter5::tree<> >
93 > { };
94
96template <typename Elem>
97struct binary_tree_search<boost::mpl::void_, Elem>
98 : boost::mpl::end< chapter5::tree<> > { };
99
101template <typename Cur, typename L, typename R, typename Elem>
102struct binary_tree_search<chapter5::tree<Cur, L, R>, Elem>
103 : boost::mpl::eval_if<
104 boost::mpl::equal_to<Elem, Cur>,
105 chapter5::tree<Cur, L, R>,
106 boost::mpl::eval_if<
107 boost::mpl::greater<Elem, Cur>,
108 binary_tree_search<R, Elem>,
109 binary_tree_search<L, Elem> >
110 > { };
111
112} // namespace exercise_6_4
113
114#endif // EXERCISE_6_4
Define a binary tree structure for future exercises.
Solution to Exercise 6-3.
Exists to inject functionality into the Boost MPL namespace.
Exists to inject functionality into the Boost namespace.
Provide utilities availble to all Chapter 5 solutions.
Encapsulate solution for Exercise 6-4.
Perform a search on a binary tree structure.
Quick and dirty (non-iterator, I know) way to mark the end of the tree sequence...