aGrUM  0.14.2
nodeGraphPart.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 
28 #ifdef GUM_NO_INLINE
30 #endif // GUM_NOINLINE
31 
32 namespace gum {
33 
35  NodeGraphPart::NodeGraphPart(Size holes_size, bool holes_resize_policy) :
36  __holes_size(holes_size), __holes_resize_policy(holes_resize_policy),
37  __endIteratorSafe(*this), __boundVal(0) {
38  __holes = nullptr;
39  GUM_CONSTRUCTOR(NodeGraphPart);
41  }
42 
46  __holes = nullptr;
47 
48  if (s.__holes) __holes = new NodeSet(*s.__holes);
49 
51 
52  GUM_CONS_CPY(NodeGraphPart);
53  }
54 
56  if (__holes) delete __holes;
57 
58  GUM_DESTRUCTOR(NodeGraphPart);
59  }
60 
62  clear(); // "virtual" flush of the nodes set
65 
66  if (s.__holes) __holes = new NodeSet(*s.__holes);
67 
69 
71  }
72 
73  // id is assumed to belong to NodeGraphPart
75  // we assume that the node exists
76  if (node + 1 == __boundVal) {
77  // we remove the max : no new hole and maybe a bunch of holes to remove
78  --__boundVal;
79 
80  if (__holes) {
81  while (__holes->contains(__boundVal - 1)) {
82  // a bunch of holes to remove. We do not use inHoles for optimisation
83  // :
84  // not to repeat the test if (__holes) each time
86  }
87 
88  if (__holes->empty()) {
89  delete __holes;
90  __holes = nullptr;
91  }
92  }
93 
95  } else {
97 
98  __holes->insert(node);
99  }
100  }
101 
102  std::string NodeGraphPart::toString() const {
103  std::stringstream s;
104  bool first = true;
105  s << "{";
106 
107  for (NodeId id = 0; id < __boundVal; ++id) {
108  if (__inHoles(id)) continue;
109 
110  if (first) {
111  first = false;
112  } else {
113  s << ",";
114  }
115 
116  s << id;
117  }
118 
119  s << "}";
120 
121  return s.str();
122  }
123 
124  std::ostream& operator<<(std::ostream& stream, const NodeGraphPart& set) {
125  stream << set.toString();
126  return stream;
127  }
128 
130  if (id >= __boundVal) {
131  if (id > __boundVal) { // we have to add holes
133 
134  for (NodeId i = __boundVal; i < id; ++i)
135  __holes->insert(i);
136  }
137 
138  __boundVal = id + 1;
139 
141  } else {
142  if (__inHoles(id)) { // we fill a hole
143  __eraseHole(id);
144  } else {
145  GUM_ERROR(DuplicateElement, "Id " << id << " is already used");
146  }
147  }
148 
149  GUM_EMIT1(onNodeAdded, id);
150  }
151 
154  __boundVal = 0;
155 
156  if (onNodeDeleted.hasListener()) {
157  for (NodeId n = 0; n < bound; ++n) {
158  if (!__inHoles(n)) GUM_EMIT1(onNodeDeleted, n);
159  }
160  }
161 
163 
164  delete (__holes);
165  __holes = nullptr;
166  }
167 
169  if (id == _pos) { // we just deleted the _pos in NodeGraphPart
170  _valid = false;
171  }
172 
173  if (_pos >= _nodes->bound()) { // moreover, it was the last position
174  _pos = _nodes->bound();
175  _valid = false;
176  }
177  }
178 
179 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
Inline implementation of the base node set class for graphs.
Base node set class for graphs.
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:704
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:40
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:653
gum is the global namespace for all aGrUM entities
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:583
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:45
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:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52