KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Categorical_event.h
Go to the documentation of this file.
1 #ifndef CATEGORICAL_EVENT_H_
2 #define CATEGORICAL_EVENT_H_
3 
11 #include "l_cpp/l_exception.h"
12 #include <boost/functional/hash.hpp>
13 #include <vector>
14 #include <iostream>
15 
16 namespace semantics
17 {
18 
22 class Categorical_event_base;
23 
24 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
25 
29 template<size_t N> class Categorical_event;
30 
31 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
32 
35 template<size_t N>
37  const Categorical_event<N>& event,
38  const size_t steps = 1);
39 
40 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
41 
44 template<size_t N>
46  const Categorical_event<N>& event,
47  const size_t steps = 1);
48 
49 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
50 
53 std::ostream& operator<<(std::ostream&, const Categorical_event_base&);
54 
55 
56 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
57 
59 {
60 public:
62  typedef size_t Val_type;
63  typedef std::vector<Val_type> Data_type;
64  typedef size_t Hash_type;
65  virtual std::ostream& print_to(std::ostream& os) const {return os;}
66  virtual bool operator==(const Self_type&) const {return false;}
67 };
68 
69 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
70 
71 template<size_t N>
72 class Categorical_event : public Categorical_event_base
73 {
74 public:
75 
76 /*------------------------------------------------------------
77  * TYPEDEFS
78  *------------------------------------------------------------*/
79 
81 
82 /*------------------------------------------------------------
83  * CONSTRUCTORS & DESTRUCTOR
84  *------------------------------------------------------------*/
85 
89  Categorical_event() : size_(0), data_(), hash_val_(0) {}
90 
95  Categorical_event(Data_type data, size_t size);
96 
103  Data_type::const_iterator first,
104  Data_type::const_iterator last,
105  size_t size
106  );
107 
112  size_(other.size_),
113  data_(other.data_),
114  hash_val_(other.hash_val_)
115  {}
116 
117 /*------------------------------------------------------------
118  * ACCESSORS
119  *------------------------------------------------------------*/
120 
123  const size_t& size() const {return size_;}
124 
127  const size_t& front() const {return data_.front();}
128 
131  const size_t& back() const {return data_.back();}
132 
133 /*------------------------------------------------------------
134  * OPERATORS
135  *------------------------------------------------------------*/
136 
139  bool operator==(const Self_type& other) const;
140 
141 /*------------------------------------------------------------
142  * DISPLAY FUNCTIONS
143  *------------------------------------------------------------*/
144 
147  std::ostream& print_to(std::ostream& os) const;
148 
149 /*------------------------------------------------------------
150  * FRIEND FUNCTIONS
151  *------------------------------------------------------------*/
152 
155  friend Categorical_event<N-1> smooth<N>(
156  const Self_type& event,
157  const size_t steps);
158 
161  friend Categorical_event<N> get_context<N>(
162  const Self_type& event,
163  const size_t steps);
164 
167  friend size_t hash_value(Self_type const& e) {return e.hash_val_;}
168 
169 private:
170  size_t size_;
171  Data_type data_;
172  Hash_type hash_val_;
176  void check_size() const
177  {
178  if(data_.size() != size_)
179  {
180  std::cerr << "Data has size: " << data_.size() << ","
181  << "Size required is " << size_ << std::endl;
182  KJB_THROW_2(
184  "Categorical_event constructed with incorrect length.");
185  }
186  }
187 };
188 
189 /*------------------------------------------------------------
190  * TEMPLATE MEMBER FUNCTION DEFINITIONS
191  *------------------------------------------------------------*/
192 
193 template<size_t N> inline
195  : size_(size),
196  data_(data),
197  hash_val_(boost::hash_range(data.begin(), data.end()))
198 {
199  try
200  {
201  check_size();
202  }
203  catch(kjb::Illegal_argument& e)
204  {
205  std::cout << e.get_msg() << std::endl;
206  }
207 }
208 
209 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
210 
211 template<size_t N> inline
213  Data_type::const_iterator first,
214  Data_type::const_iterator last,
215  size_t size
216  ) : size_(size),
217  data_(first, last),
218  hash_val_(boost::hash_range(first, last))
219 {
220  try
221  {
222  check_size();
223  }
224  catch(kjb::Illegal_argument& e)
225  {
226  std::cout << e.get_msg() << std::endl;
227  }
228 }
229 
230 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
231 
232 template<size_t N> inline
234 {
235  return hash_val_ == other.hash_val_;
236 }
237 
238 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
239 
240 template<size_t N>
241 std::ostream& Categorical_event<N>::print_to(std::ostream& os) const
242 {
243  if(data_.size() == 0)
244  {
245  os << "NULL";
246  } else {
247  os << "(";
248  for(Data_type::const_iterator it = data_.begin();
249  it != data_.end(); it++)
250  {
251  os << *it << ",";
252  }
253  os << ")";
254  }
255  return os;
256 }
257 
258 
259 /*------------------------------------------------------------
260  * TEMPLATE FREE FUNCTION DEFINITIONS
261  *------------------------------------------------------------*/
262 
263 template<size_t N>
265  const Categorical_event<N>& event,
266  const size_t steps
267  )
268 {
269  if(steps > event.size())
270  {
271  KJB_THROW_2(
273  "steps argument of smooth() too large for event");
274  }
275  return Categorical_event<N-1>(
276  event.data_.begin(), event.data_.end() - steps,
277  event.size() - steps);
278 }
279 
280 /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
281 
282 template<size_t N>
284  const Categorical_event<N>& event,
285  const size_t steps)
286 {
287  return Categorical_event<N>(
288  event.data_.begin() + steps, event.data_.end(),
289  event.size() - steps);
290 }
291 
292 }; //namespace semantics
293 
294 #endif
Categorical_event< N > Self_type
Definition: Categorical_event.h:80
Categorical_event< N > get_context(const Categorical_event< N > &event, const size_t steps=1)
returns RHS of conditioning expression
Definition: Categorical_event.h:283
std::vector< Val_type > Data_type
Definition: Categorical_event.h:63
std::ostream & operator<<(std::ostream &os, const Categorical_event_base &e)
display key in human readable form
Definition: Categorical_event.cpp:13
Categorical_event< N-1 > smooth(const Categorical_event< N > &event, const size_t steps=1)
marginalizes look-up key by <steps> from back (generalizes context)
Definition: Categorical_event.h:264
friend size_t hash_value(Self_type const &e)
return underlying hash value
Definition: Categorical_event.h:167
size_t Hash_type
Definition: Categorical_event.h:64
const std::string & get_msg() const
Returns the error message for the Exception.
Definition: l_exception.h:152
virtual bool operator==(const Self_type &) const
Definition: Categorical_event.h:66
const size_t & size() const
get length of data key
Definition: Categorical_event.h:123
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
look-up key class for contingency table
Definition: Categorical_event.h:29
bool operator==(const Self_type &other) const
two keys are equal iff their hash values are equal
Definition: Categorical_event.h:233
virtual std::ostream & print_to(std::ostream &os) const
Definition: Categorical_event.h:65
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
const size_t & front() const
get first data entry
Definition: Categorical_event.h:127
Categorical_event()
default ctor
Definition: Categorical_event.h:89
abstract base class for look-up keys
Definition: Categorical_event.h:58
Support for error handling exception classes in libKJB.
size_t Val_type
Definition: Categorical_event.h:62
Categorical_event_base Self_type
Definition: Categorical_event.h:61
const size_t & back() const
get last data entry
Definition: Categorical_event.h:131
std::ostream & print_to(std::ostream &os) const
print key data to ostream os
Definition: Categorical_event.h:241
Categorical_event(const Self_type &other)
copy constructor
Definition: Categorical_event.h:110