aGrUM  0.16.0
nodeGraphPart.cpp
Go to the documentation of this file.
1 
30 
31 #ifdef GUM_NO_INLINE
33 #endif // GUM_NOINLINE
34 
35 namespace gum {
36 
38  NodeGraphPart::NodeGraphPart(Size holes_size, bool holes_resize_policy) :
39  __holes_size(holes_size), __holes_resize_policy(holes_resize_policy),
40  __endIteratorSafe(*this), __boundVal(0) {
41  __holes = nullptr;
42  GUM_CONSTRUCTOR(NodeGraphPart);
44  }
45 
49  __holes = nullptr;
50 
51  if (s.__holes) __holes = new NodeSet(*s.__holes);
52 
54 
55  GUM_CONS_CPY(NodeGraphPart);
56  }
57 
59  if (__holes) delete __holes;
60 
61  GUM_DESTRUCTOR(NodeGraphPart);
62  }
63 
65  clear(); // "virtual" flush of the nodes set
68 
69  if (s.__holes) __holes = new NodeSet(*s.__holes);
70 
72 
74  }
75 
76  // id is assumed to belong to NodeGraphPart
78  // we assume that the node exists
79  if (node + 1 == __boundVal) {
80  // we remove the max : no new hole and maybe a bunch of holes to remove
81  --__boundVal;
82 
83  if (__holes) {
84  while (__holes->contains(__boundVal - 1)) {
85  // a bunch of holes to remove. We do not use inHoles for optimisation
86  // :
87  // not to repeat the test if (__holes) each time
89  }
90 
91  if (__holes->empty()) {
92  delete __holes;
93  __holes = nullptr;
94  }
95  }
96 
98  } else {
100 
101  __holes->insert(node);
102  }
103  }
104 
105  std::string NodeGraphPart::toString() const {
106  std::stringstream s;
107  bool first = true;
108  s << "{";
109 
110  for (NodeId id = 0; id < __boundVal; ++id) {
111  if (__inHoles(id)) continue;
112 
113  if (first) {
114  first = false;
115  } else {
116  s << ",";
117  }
118 
119  s << id;
120  }
121 
122  s << "}";
123 
124  return s.str();
125  }
126 
127  std::ostream& operator<<(std::ostream& stream, const NodeGraphPart& set) {
128  stream << set.toString();
129  return stream;
130  }
131 
133  if (id >= __boundVal) {
134  if (id > __boundVal) { // we have to add holes
136 
137  for (NodeId i = __boundVal; i < id; ++i)
138  __holes->insert(i);
139  }
140 
141  __boundVal = id + 1;
142 
144  } else {
145  if (__inHoles(id)) { // we fill a hole
146  __eraseHole(id);
147  } else {
148  GUM_ERROR(DuplicateElement, "Id " << id << " is already used");
149  }
150  }
151 
152  GUM_EMIT1(onNodeAdded, id);
153  }
154 
157  __boundVal = 0;
158 
159  if (onNodeDeleted.hasListener()) {
160  for (NodeId n = 0; n < bound; ++n) {
161  if (!__inHoles(n)) GUM_EMIT1(onNodeDeleted, n);
162  }
163  }
164 
166 
167  delete (__holes);
168  __holes = nullptr;
169  }
170 
172  if (id == _pos) { // we just deleted the _pos in NodeGraphPart
173  _valid = false;
174  }
175 
176  if (_pos >= _nodes->bound()) { // moreover, it was the last position
177  _pos = _nodes->bound();
178  _valid = false;
179  }
180  }
181 
182 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:581
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
virtual void addNodeWithId(const NodeId id)
try to insert a node with the given id
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:707
NodeSet * __holes
the set of nodes not contained in the NodeGraphPart in the interval 1..__max
NodeGraphPartIteratorSafe __endIteratorSafe
the end iterator (used to speed-up parsings of the NodeGraphPart)
#define GUM_EMIT1(signal, arg1)
Definition: signaler1.h:42
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
bool __holes_resize_policy
value for __holes configuration
void __addHole(NodeId id)
to add a hole.
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:656
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
bool __inHoles(NodeId id) const
virtual ~NodeGraphPart()
destructor
NodeId __boundVal
the id below which NodeIds may belong to the NodeGraphPart
Size __holes_size
value for __holes configuration
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:605
void __updateEndIteratorSafe()
updating endIterator (always at __max+1)
NodeGraphPart(Size holes_size=HashTableConst::default_size, bool holes_resize_policy=true)
default constructor
Signaler1< NodeId > onNodeAdded
std::string toString() const
a function to display the set of nodes
Class for node sets in graph.
virtual void clear()
alias for clearNodes
NodeId bound() const
returns a number n such that all node ids are strictly lower than n
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
void __eraseHole(NodeId id)
to delete hole.
void __clearNodes()
code for clearing nodes (called twice)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Signaler1< NodeId > onNodeDeleted
void whenNodeDeleted(const void *src, NodeId id)
called when a node is deleted in the iterated NodeGraphPart
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:613
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55