Evoplex  0.2.1
node_p.h
1 /* Evoplex <https://evoplex.org>
2  * Copyright (C) 2016-present - Marcos Cardinot <marcos@cardinot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NODE_P_H
18 #define NODE_P_H
19 
20 #include <memory>
21 
22 #include "attributes.h"
23 #include "edges.h"
24 #include "prg.h"
25 
26 namespace evoplex {
27 
28 class BaseNode;
29 using NodePtr = std::shared_ptr<BaseNode>;
30 
36 {
37  friend class AbstractGraph;
38  friend class Nodes;
39  friend class TestNode;
40  friend class TestEdge;
41 
42 public:
44  virtual ~NodeInterface() = default;
45 
50  virtual NodePtr clone() const = 0;
51 
55  virtual const Edges& inEdges() const = 0;
56 
60  virtual const Edges& outEdges() const = 0;
61 
65  virtual int degree() const = 0;
66 
71  virtual int inDegree() const = 0;
72 
77  virtual int outDegree() const = 0;
78 
79 private:
80  virtual void addInEdge(const Edge& inEdge) = 0;
81  virtual void addOutEdge(const Edge& outEdge) = 0;
82  virtual void removeInEdge(const int edgeId) = 0;
83  virtual void removeOutEdge(const int edgeId) = 0;
84  virtual void clearInEdges() = 0;
85  virtual void clearOutEdges() = 0;
86 };
87 
96 class BaseNode : public NodeInterface
97 {
98  friend class AbstractGraph;
99  friend class NodesPrivate;
100  friend class TestNode;
101  friend class TestEdge;
102 
103 public:
107  inline const Attributes& attrs() const;
109  inline const Value& attr(int id) const;
111  inline Value attr(const QString& name, Value defaultValue=Value()) const;
113  inline void setAttr(int id, const Value& value);
114 
118  inline int id() const;
122  inline float x() const;
126  inline float y() const;
127 
131  inline void setX(float x);
135  inline void setY(float y);
139  inline void setCoords(float x, float y);
140 
145  Node randNeighbour(PRG* prg) const;
146 
147 protected:
148  Edges m_outEdges;
149 
153  struct constructor_key { };
154 
155  explicit BaseNode(const constructor_key&, int id, const Attributes& attrs, float x, float y);
156  explicit BaseNode(const constructor_key& k, int id, const Attributes& attr);
157  ~BaseNode() override;
158 
159 private:
160  const int m_id;
161  Attributes m_attrs;
162  float m_x;
163  float m_y;
164 };
165 
170 class UNode : public BaseNode
171 {
172 public:
173  explicit UNode(const constructor_key& k, int id, const Attributes& attrs, float x, float y);
174  explicit UNode(const constructor_key& k, int id, const Attributes& attrs);
175  ~UNode() override = default;
176 
177  inline NodePtr clone() const override;
178  inline const Edges& inEdges() const override;
179  inline const Edges& outEdges() const override;
180  inline int degree() const override;
181  inline int inDegree() const override;
182  inline int outDegree() const override;
183 
184 private:
185  inline void addInEdge(const Edge& inEdge) override;
186  inline void addOutEdge(const Edge& outEdge) override;
187  inline void removeInEdge(const int edgeId) override;
188  inline void removeOutEdge(const int edgeId) override;
189  inline void clearInEdges() override;
190  inline void clearOutEdges() override;
191 };
192 
197 class DNode : public BaseNode
198 {
199 public:
200  explicit DNode(const constructor_key& k, int id, const Attributes& attrs, float x, float y);
201  explicit DNode(const constructor_key& k, int id, const Attributes& attrs);
202  ~DNode() override = default;
203 
204  inline NodePtr clone() const override;
205  inline const Edges& inEdges() const override;
206  inline const Edges& outEdges() const override;
207  inline int degree() const override;
208  inline int inDegree() const override;
209  inline int outDegree() const override;
210 
211 private:
212  Edges m_inEdges;
213 
214  inline void addInEdge(const Edge& inEdge) override;
215  inline void addOutEdge(const Edge& outEdge) override;
216  inline void removeInEdge(const int edgeId) override;
217  inline void removeOutEdge(const int edgeId) override;
218  inline void clearInEdges() override;
219  inline void clearOutEdges() override;
220 };
221 
222 /************************************************************************
223  BaseNode: Inline member functions
224  ************************************************************************/
225 
226 inline const Attributes& BaseNode::attrs() const
227 { return m_attrs; }
228 
229 inline const Value& BaseNode::attr(int id) const
230 { return m_attrs.value(id); }
231 
232 inline Value BaseNode::attr(const QString& name, Value defaultValue) const
233 { return m_attrs.value(name, defaultValue); }
234 
235 inline void BaseNode::setAttr(int id, const Value& value)
236 { m_attrs.setValue(id, value); }
237 
238 inline int BaseNode::id() const
239 { return m_id; }
240 
241 inline float BaseNode::x() const
242 { return m_x; }
243 
244 inline void BaseNode::setX(float x)
245 { m_x = x; }
246 
247 inline float BaseNode::y() const
248 { return m_y; }
249 
250 inline void BaseNode::setY(float y)
251 { m_y = y; }
252 
253 inline void BaseNode::setCoords(float x, float y)
254 { setX(x); setY(y); }
255 
256 /************************************************************************
257  UNode: Inline member functions
258  ************************************************************************/
259 
260 inline NodePtr UNode::clone() const
261 { return std::make_shared<UNode>(constructor_key(), id(), attrs(), x(), y()); }
262 
263 inline const Edges& UNode::inEdges() const
264 { return m_outEdges; }
265 
266 inline const Edges& UNode::outEdges() const
267 { return m_outEdges; }
268 
269 inline int UNode::degree() const
270 { return static_cast<int>(m_outEdges.size()); }
271 
272 inline int UNode::inDegree() const
273 { return degree(); }
274 
275 inline int UNode::outDegree() const
276 { return degree(); }
277 
278 inline void UNode::addInEdge(const Edge& inEdge)
279 { addOutEdge(inEdge); }
280 
281 inline void UNode::addOutEdge(const Edge& outEdge)
282 { m_outEdges.insert({outEdge.id(), outEdge}); }
283 
284 inline void UNode::removeInEdge(const int edgeId)
285 { removeOutEdge(edgeId); }
286 
287 inline void UNode::removeOutEdge(const int edgeId)
288 { m_outEdges.erase(edgeId); }
289 
290 inline void UNode::clearInEdges()
291 { clearOutEdges(); }
292 
293 inline void UNode::clearOutEdges()
294 { m_outEdges.clear(); }
295 
296 /************************************************************************
297  DNode: Inline member functions
298  ************************************************************************/
299 
300 NodePtr DNode::clone() const
301 { return std::make_shared<DNode>(constructor_key(), id(), attrs(), x(), y()); }
302 
303 inline const Edges& DNode::inEdges() const
304 { return m_inEdges; }
305 
306 inline const Edges& DNode::outEdges() const
307 { return m_outEdges; }
308 
309 inline int DNode::degree() const
310 { return inDegree() + outDegree(); }
311 
312 inline int DNode::inDegree() const
313 { return static_cast<int>(m_inEdges.size()); }
314 
315 inline int DNode::outDegree() const
316 { return static_cast<int>(m_outEdges.size()); }
317 
318 inline void DNode::addInEdge(const Edge& inEdge)
319 { m_inEdges.insert({inEdge.id(), inEdge}); }
320 
321 inline void DNode::addOutEdge(const Edge& outEdge)
322 { m_outEdges.insert({outEdge.id(), outEdge}); }
323 
324 inline void DNode::removeInEdge(const int edgeId)
325 { m_inEdges.erase(edgeId); }
326 
327 inline void DNode::removeOutEdge(const int edgeId)
328 { m_outEdges.erase(edgeId); }
329 
330 inline void DNode::clearInEdges()
331 { m_inEdges.clear(); }
332 
333 inline void DNode::clearOutEdges()
334 { m_outEdges.clear(); }
335 
336 } // evoplex
337 #endif // NODE_P_H
virtual NodePtr clone() const =0
Creates a new std::shared_ptr<BaseNode> with the same data of the current Node.
A class for variant data types (tagged union).
Definition: value.h:56
virtual int outDegree() const =0
Gets the node&#39;s out-degree, i.e., the number of edges leaving the node.
A node belongs to either a directed or undirected graph.
Definition: node_p.h:96
It wraps a std::shared_ptr<BaseNode>.
Definition: node.h:35
A collection of utility functions for creating and saving nodes.
Definition: nodes_p.h:32
const Attributes & attrs() const
Gets all the node&#39;s Attributes.
Definition: node_p.h:226
const Value & attr(int id) const
Gets the value of the attribute at id.
Definition: node_p.h:229
void setCoords(float x, float y)
Sets the node&#39;s coordinates.
Definition: node_p.h:253
BaseNode implementation for directed nodes.
Definition: node_p.h:197
A common interface for the node&#39;s classes.
Definition: node_p.h:35
BaseNode implementation for undirected nodes.
Definition: node_p.h:170
NodePtr clone() const override
Creates a new std::shared_ptr<BaseNode> with the same data of the current Node.
Definition: node_p.h:300
float y() const
Gets the node&#39;s y coordinate.
Definition: node_p.h:247
const Edges & outEdges() const override
Gets the edges leaving the node.
Definition: node_p.h:306
void setValue(int id, const Value &value)
Sets the value at id.
Definition: attributes.h:221
const Value & value(int id) const
Gets the value of the attribute at id.
Definition: attributes.h:213
int outDegree() const override
Gets the node&#39;s out-degree, i.e., the number of edges leaving the node.
Definition: node_p.h:275
Node randNeighbour(PRG *prg) const
Gets a random neighbour.
virtual int degree() const =0
Gets the node&#39;s degree.
A Node container.
Definition: nodes.h:32
int inDegree() const override
Gets the node&#39;s in-degree, i.e., the number of edges entering the node.
Definition: node_p.h:272
int inDegree() const override
Gets the node&#39;s in-degree, i.e., the number of edges entering the node.
Definition: node_p.h:312
virtual const Edges & outEdges() const =0
Gets the edges leaving the node.
int outDegree() const override
Gets the node&#39;s out-degree, i.e., the number of edges leaving the node.
Definition: node_p.h:315
An Edge connects a Node to itself or to another Node.
Definition: edge.h:37
const Edges & inEdges() const override
Gets the edges entering the node.
Definition: node_p.h:303
This is a private key accessible only to friend classes.
Definition: node_p.h:153
int id() const
Gets the node&#39;s id.
Definition: node_p.h:238
void setY(float y)
Sets the node&#39;s y coordinate.
Definition: node_p.h:250
virtual ~NodeInterface()=default
Destructor.
int id() const
Gets the edge&#39;s id.
int degree() const override
Gets the node&#39;s degree.
Definition: node_p.h:269
void setX(float x)
Sets the node&#39;s x coordinate.
Definition: node_p.h:244
const Edges & inEdges() const override
Gets the edges entering the node.
Definition: node_p.h:263
float x() const
Gets the node&#39;s x coordinate.
Definition: node_p.h:241
const Edges & outEdges() const override
Gets the edges leaving the node.
Definition: node_p.h:266
Pseudo-random number generator.
Definition: prg.h:29
virtual int inDegree() const =0
Gets the node&#39;s in-degree, i.e., the number of edges entering the node.
void setAttr(int id, const Value &value)
Sets the value at id.
Definition: node_p.h:235
NodePtr clone() const override
Creates a new std::shared_ptr<BaseNode> with the same data of the current Node.
Definition: node_p.h:260
int degree() const override
Gets the node&#39;s degree.
Definition: node_p.h:309
Definition: abstractgraph.h:29
A container of labeled values.
Definition: attributes.h:39
Abstract base class for graph plugins.
Definition: abstractgraph.h:54
virtual const Edges & inEdges() const =0
Gets the edges entering the node.
An Edge container.
Definition: edges.h:32