DiaDes  0.1
DIAgnosis of Discrete-Event System
EventSet.hh
Go to the documentation of this file.
1 
8 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__EVENTSET__HH__
9 #define __DIADES__AUTOMATA__EXPERIMENTAL__EVENTSET__HH__
10 
11 #include <unordered_map>
12 #include <vector>
13 #include <algorithm>
14 #include <iterator>
15 #include <boost/functional/hash.hpp>
16 #include <boost/dynamic_bitset.hpp>
18 #include <diades/utils/Verbose.hh>
21 
22 namespace Diades
23 {
24  namespace Automata
25  {
26  namespace Experimental
27  {
28 
29 
30 // /**
31 // * boost hash_value function to run with hash_combine, hash_range
32 // *
33 // */
34 // template <typename Block, typename Alloc>
35 // inline std::size_t hash_value(boost::dynamic_bitset<Block, Alloc> const& bs)
36 // {
37 // size_t seed = boost::hash_value(bs.size());
38 // std::vector<Block> blocks(bs.num_blocks());
39 // boost::to_block_range(bs, std::inserter(blocks, blocks.begin()));
40 // boost::hash_range(seed, blocks.begin(), blocks.end());
41 // return seed;
42 // }
43 
44  }
45  }
46 }
47 
48 
49 namespace std
50 {
51 
56  template <typename Block, typename Alloc> struct hash<boost::dynamic_bitset<Block, Alloc> >
57  {
58 
59  size_t operator()(boost::dynamic_bitset<Block, Alloc> const& bs) const
60  {
61  size_t seed = boost::hash_value(bs.size());
62  std::vector<Block> blocks(bs.num_blocks());
63  boost::to_block_range(bs, std::inserter(blocks, blocks.begin()));
64  boost::hash_range(seed, blocks.begin(), blocks.end());
65  return seed;
66  }
67  };
68 }
69 
70 
71 namespace Diades
72 {
73  namespace Automata
74  {
75  namespace Experimental
76  {
77  using Diades::Utils::Msg;
78 
89  template<typename EventLabel, typename EventId>
91  {
92  public:
93 
95  typedef size_t EventCode;
97  typedef typename std::unordered_map<EventId, size_t> Map;
98 
100  typedef boost::dynamic_bitset<> EventSet;
101 
102  private:
104  std::unordered_map<size_t, EventId> _eventOfId;
105 
106  public:
107 
108  static string typeName()
109  {
110  return "Automata::Experimental::EventSetManager";
111  }
113 
114  public:
115 
119  EventSetManager() : _idOfEvent(), _eventOfId()
120  {
121  }
122 
127  _idOfEvent(mgr._idOfEvent), _eventOfId(mgr._eventOfId)
128  {
129  }
130 
135  _idOfEvent(mgr._idOfEvent), _eventOfId(mgr._eventOfId)
136  {
137  }
138 
143  {
144  if (this != &mgr)
145  {
146  _idOfEvent = mgr._idOfEvent;
147  _eventOfId = mgr._eventOfId;
148  }
149  return *this;
150  }
151 
156  {
157  }
158 
162  const Map & map() const
163  {
164  return _idOfEvent;
165  }
166 
171  bool hasEvent(EventId e) const
172  {
173  return _idOfEvent.find(e) != _idOfEvent.end();
174  }
175 
180  bool hasCode(EventCode c) const
181  {
182  return _eventOfId.find(c) != _eventOfId.end();
183  }
184 
193  template<typename EventIterator>
194  EventSetManager & initialise(EventIterator first,
195  EventIterator last)
196  {
197  _idOfEvent.clear();
198  _eventOfId.clear();
199  return encodeRange(first, last);
200  }
201 
211  template<typename EventIterator>
212  EventSetManager & encodeRange(EventIterator first,
213  EventIterator last)
214  {
215  while (first != last)
216  {
217  if (!hasEvent(*first))
218  {
219  encodeNoCheck(*first);
220  }
221  ++first;
222  }
223  return *this;
224  }
225 
232  EventCode encode(EventId e)
233  {
234  auto it = _idOfEvent.find(e);
235  if ( it != _idOfEvent.end())
236  {
237  return it->second;
238  }
239  return encodeNoCheck(e);
240  }
241 
247  EventCode encodeNoCheck(EventId e)
248  {
249  size_t code = _idOfEvent.size();
250  _idOfEvent[e] = code;
251  _eventOfId[code] = e;
252  return code;
253  }
254 
260  EventCode code(EventId e) const
261  {
262  require(Exception, hasEvent(e),
263  Msg("code: the event %1% has no code in this manager")
264  % e);
265  return (_idOfEvent.find(e)->second);
266  }
267 
273  EventId event(EventCode c) const
274  {
275  require(Exception, hasCode(c),
276  Msg("event: the code %1% does not in this manager")
277  % c);
278  return (_eventOfId.find(c)->second);
279  }
280 
284  size_t numberOfEncodedEvents() const
285  {
286  return _idOfEvent.size();
287  }
288 
298  template<typename EventIterator>
299  EventSet eventSet(EventIterator first, EventIterator last) const
300  {
301  EventSet result(numberOfEncodedEvents(), false);
302  while (first != last)
303  {
304  assertion(Exception, hasEvent(*first),
305  Msg("eventSet: event %1% has no code in this manager")
306  % *first);
307  result[code(*first)] = true;
308  ++first;
309  }
310  return result;
311  }
312 
319  std::vector<EventId> decodeEventSet(const EventSet & eventSet) const
320  {
321  std::vector<EventId> result;
322  for(unsigned index = 0; index < eventSet.size(); ++index)
323  {
324  if(eventSet[index])
325  {
326  if(hasCode(index))
327  {
328  result.push_back(event(index));
329  }
330  }
331 
332 
333  }
334  return result;
335  }
336 
337 
346  EventSet projectEventSet(const EventSetManager & mgr,
347  const EventSet & es)
348  {
349  EventSet result(numberOfEncodedEvents(), false);
350  size_t code = es.find_first();
351  while (code < es.npos)
352  {
353  auto it = _idOfEvent.find(mgr.event(code));
354  if (it != _idOfEvent.end())
355  {
356  result[it->second] = true;
357  }
358  code = es.find_next(code);
359  }
360  return result;
361  }
362 
373  EventSet eventSet(EventId e) const
374  {
375  require(Exception, hasEvent(e),
376  Msg("eventSet: event %1% has no code in this manager")
377  % e);
378  EventSet result(numberOfEncodedEvents(), false);
379 
380  result[code(e)] = true;
381  return result;
382  }
383 
384 
389  EventSet emptySet() const
390  {
391  EventSet result(numberOfEncodedEvents(), false);
392  return result;
393  }
394 
395 
396 
403  bool contains(const EventSet & eSet, EventId e) const
404  {
405  require(Exception, hasEvent(e),
406  Msg("contains: event %1% has no code in this manager")
407  % e);
408  return eSet[code(e)];
409  }
410 
423  {
424  std::vector<EventId> events;
425  if (numberOfEncodedEvents() > 0)
426  {
427  events.reserve(numberOfEncodedEvents());
428  std::transform(_idOfEvent.begin(),
429  _idOfEvent.end(),
430  std::back_inserter(events),
431  [](auto & pair)
432  {
433  return pair.first;
434  });
435  std::sort(events.begin(), events.end());
436  _idOfEvent.clear();
437  _eventOfId.clear();
438  for (unsigned i = 0; i < events.size(); ++i)
439  {
440  _idOfEvent[events[i]] = i;
441  _eventOfId[i] = events[i];
442  }
443  }
444  return *this;
445  }
446 
447 
448  };
449  }
450  }
451 }
452 
453 #endif
454 
bool contains(const EventSet &eSet, EventId e) const
Definition: EventSet.hh:403
std::unordered_map< EventId, size_t > Map
Map data structure that contains the encoding of the involved Events.
Definition: EventSet.hh:97
EventSetManager(const EventSetManager &mgr)
Definition: EventSet.hh:126
boost::dynamic_bitset EventSet
EventSet a simple dynamic bitset represents a set of Events.
Definition: EventSet.hh:100
EventSetManager & initialise(EventIterator first, EventIterator last)
Definition: EventSet.hh:194
STL namespace.
std::vector< EventId > decodeEventSet(const EventSet &eventSet) const
Definition: EventSet.hh:319
std::size_t hash_value(Diades::Graph::Edge const &e)
Definition: Edge.hh:324
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
size_t EventCode
EventCode the code of an Event in the EventSetManager.
Definition: EventSet.hh:95
std::unordered_map< size_t, EventId > _eventOfId
mapping Event->Id
Definition: EventSet.hh:104
EventSetManager & operator=(const EventSetManager &mgr)
Definition: EventSet.hh:142
EventSetManager & encodeRange(EventIterator first, EventIterator last)
Definition: EventSet.hh:212
EventSet projectEventSet(const EventSetManager &mgr, const EventSet &es)
Definition: EventSet.hh:346
Diades::Utils::Exception< EventSetManager > Exception
Definition: EventSet.hh:112
static string typeName()
mapping Id->Event
Definition: EventSet.hh:108
size_t operator()(boost::dynamic_bitset< Block, Alloc > const &bs) const
Definition: EventSet.hh:59
boost::format Msg
Definition: Verbose.hh:42
EventSet eventSet(EventIterator first, EventIterator last) const
Definition: EventSet.hh:299
#define assertion(Exception, expr, message)
Definition: Assertion.hh:106