• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-----------------------------------------------------------------------------+
2 Copyright (c) 2008-2009: Joachim Faulhaber
3 +------------------------------------------------------------------------------+
4    Distributed under the Boost Software License, Version 1.0.
5       (See accompanying file LICENCE.txt or copy at
6            http://www.boost.org/LICENSE_1_0.txt)
7 +-----------------------------------------------------------------------------*/
8 #ifndef BOOST_ICL_TEST_VALUE_MAKER_HPP_JOFA_080916
9 #define BOOST_ICL_TEST_VALUE_MAKER_HPP_JOFA_080916
10 
11 #include <boost/icl/type_traits/identity_element.hpp>
12 #include <boost/icl/interval_bounds.hpp>
13 
14 namespace boost{ namespace icl
15 {
16 
17 struct mono
18 {
monoboost::icl::mono19     mono(){};
operator ++boost::icl::mono20     mono& operator ++ (){ return *this; }
operator --boost::icl::mono21     mono& operator -- (){ return *this; }
operator +=boost::icl::mono22     mono& operator += (const mono&){ return *this; }
23 };
24 
operator ==(const mono &,const mono &)25 bool operator == (const mono&, const mono&){ return true; }
operator <(const mono &,const mono &)26 bool operator < (const mono&, const mono&){ return false; }
27 
28 template<class CharType, class CharTraits>
29 std::basic_ostream<CharType, CharTraits>&
operator <<(std::basic_ostream<CharType,CharTraits> & stream,const mono & object)30 operator << (std::basic_ostream<CharType, CharTraits>& stream, const mono& object)
31 {
32     return stream << "*";
33 }
34 
35 
36 
37 template <class BicrementableT>
make(int n)38 BicrementableT make(int n)
39 {
40     BicrementableT value = identity_element<BicrementableT>::value();
41     if(n>=0)
42         for(int i=0; i<n; i++)
43             ++value;
44     else
45         for(int i=0; i>n; i--)
46             --value;
47 
48     return value;
49 }
50 
51 
52 template <class Type>
53 struct test_value;
54 
55 template<>
56 struct test_value<std::string>
57 {
makeboost::icl::test_value58     static std::string make(int n)
59     {
60         std::string value = identity_element<std::string>::value();
61         int abs_n = n<0 ? -n : n;
62         for(int i=1; i<abs_n; i++)
63             value += (i%2==1 ? "hello " : "world ");
64 
65         return value;
66     }
67 };
68 
69 
70 template <class Type>
71 struct test_value<Type*>
72 {
73 
map_integersboost::icl::test_value74     static bool map_integers(Type values[], int size)
75     {
76         static const int offset = size/2;
77         for(int idx = 0; idx < size; idx++)
78             values[idx] = test_value<Type>::make(idx - offset);
79 
80         return true;
81     }
82 
makeboost::icl::test_value83     static Type* make(int n)
84     {
85         static bool initialized;
86         static const int size   = 100;
87         static const int offset = size/2;
88         static Type values[size];
89 
90         if(!initialized)
91             initialized = map_integers(values, size);
92 
93         Type* value = values + offset;
94         if(n>=0)
95             for(int i=0; i<n; i++)
96                 ++value;
97         else
98             for(int i=0; i>n; i--)
99                 --value;
100 
101         return value;
102     }
103 };
104 
105 
106 
107 template <class Type>
108 struct test_value
109 {
makeboost::icl::test_value110     static Type make(int n)
111     {
112         Type value = identity_element<Type>::value();
113         if(n>=0)
114             for(int i=0; i<n; i++)
115                 ++value;
116         else
117             for(int i=0; i>n; i--)
118                 --value;
119 
120         return value;
121     }
122 };
123 
124 
125 template <class ItvMapT>
126 struct map_val
127 {
128     typedef typename ItvMapT::domain_type       domain_type;
129     typedef typename ItvMapT::codomain_type     codomain_type;
130     typedef typename ItvMapT::interval_type     interval_type;
131     typedef typename ItvMapT::value_type        value_type;
132     typedef typename ItvMapT::segment_type      segment_type;
133     typedef typename ItvMapT::domain_mapping_type domain_mapping_type;
134     typedef std::pair<domain_type, codomain_type> std_pair_type;
135 
mk_segmentboost::icl::map_val136     static segment_type mk_segment(const interval_type& inter_val, int val)
137     {
138         return segment_type(inter_val, test_value<codomain_type>::make(val));
139     }
140 
141     /*CL?
142     static interval_type interval_(int lower, int upper, int bounds = 2)
143     {
144         return segment_type(inter_val, test_value<codomain_type>::make(val));
145     }
146 
147     static segment_type val_pair(int lower, int upper, int val, int bounds = 2)
148     {
149         return segment_type( interval_(lower, upper, static_cast<bound_type>(bounds)),
150                              test_value<codomain_type>::make(val) );
151     }
152     */
153 
map_pairboost::icl::map_val154     static domain_mapping_type map_pair(int key, int val)
155     {
156         return domain_mapping_type(test_value<  domain_type>::make(key),
157                                    test_value<codomain_type>::make(val));
158     }
159 
std_pairboost::icl::map_val160     static std_pair_type std_pair(int key, int val)
161     {
162         return std_pair_type(test_value<  domain_type>::make(key),
163                              test_value<codomain_type>::make(val));
164     }
165 };
166 
167 
168 // Very short value denotation for intervals
169 // Assumption typename T and IntervalT exists in scope
170 //I_I : [a,b]
171 #define I_I(low,up) icl::interval<T>::closed    (test_value<T>::make(low), test_value<T>::make(up))
172 //I_D : [a,b)
173 #define I_D(low,up) icl::interval<T>::right_open(test_value<T>::make(low), test_value<T>::make(up))
174 //C_I : (a,b]
175 #define C_I(low,up) icl::interval<T>::left_open (test_value<T>::make(low), test_value<T>::make(up))
176 //C_D : (a,b)
177 #define C_D(low,up) icl::interval<T>::open      (test_value<T>::make(low), test_value<T>::make(up))
178 
179 #define MK_I(ItvT,low,up) ItvT(test_value<T>::make(low), test_value<T>::make(up))
180 
181 #define MK_v(key)  test_value<T>::make(key)
182 #define MK_u(key)  test_value<U>::make(key)
183 
184 // Very short value denotation for interval value pairs
185 // Assumption typename IntervalMapT existes in scope
186 #define IIv(low,up,val) map_val<IntervalMapT>::mk_segment(I_I(low,up), val)
187 #define IDv(low,up,val) map_val<IntervalMapT>::mk_segment(I_D(low,up), val)
188 #define CIv(low,up,val) map_val<IntervalMapT>::mk_segment(C_I(low,up), val)
189 #define CDv(low,up,val) map_val<IntervalMapT>::mk_segment(C_D(low,up), val)
190 #define K_v(key,val)    map_val<IntervalMapT>::map_pair(key,val)
191 #define sK_v(key,val)   map_val<IntervalMapT>::std_pair(key,val)
192 
193 #define MK_seg(itv,val) map_val<IntervalMapT>::mk_segment(itv, val)
194 
195 
196 }} // namespace boost icl
197 
198 #endif
199 
200