libUTL++
Vertex.h
1 #pragma once
2 
4 
5 #include <libutl/slist.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
30 
32 class Vertex : public Object
33 {
35 
36 public:
41  Vertex(const Object* object)
42  {
43  init(object);
44  }
45 
47  virtual void copy(const Object& object);
48 
53  virtual const Object&
54  getKey() const
55  {
56  if (_object == nullptr)
57  return super::getKey();
58  else
59  return *_object;
60  }
61 
63  void
64  addDirectedEdge(const Vertex* vertex)
65  {
66  slist_add(_edgesHead, &_edgesTail, vertex);
67  }
68 
70  void
72  {
73  slist_add(_edgesHead, &_edgesTail, vertex);
74  slist_add(vertex->_edgesHead, &vertex->_edgesTail, this);
75  }
76 
78  void
80  {
81  slist_clear(_edgesHead, &_edgesTail, false);
82  setObject(nullptr);
83  }
84 
86  const SlistNode*
87  edges() const
88  {
89  return _edgesHead;
90  }
91 
97  Vertex*
98  find(const Object& key) const
99  {
100  return utl::cast<Vertex>(slist_find(_edgesHead, key));
101  }
102 
104  Object*
105  getObject() const
106  {
107  return _object;
108  }
109 
111  bool
112  isOwner() const
113  {
114  return ((_flags & flg_owner) != 0);
115  }
116 
118  void
119  setOwner(bool owner)
120  {
121  if (owner)
122  _flags |= flg_owner;
123  else
124  _flags &= ~flg_owner;
125  }
126 
128  bool
129  isVisited() const
130  {
131  return ((_flags & flg_visited) != 0);
132  }
133 
135  void
136  setVisited(bool visited)
137  {
138  if (visited)
139  _flags |= flg_visited;
140  else
141  _flags &= ~flg_visited;
142  }
143 
149  bool
150  removeDirectedEdge(const Vertex* vertex)
151  {
152  return slist_remove(_edgesHead, &_edgesTail, *vertex, false);
153  }
154 
160  bool
162  {
163  return slist_remove(_edgesHead, &_edgesTail, *vertex, false, true)
164  && slist_remove(vertex->_edgesHead, &vertex->_edgesTail, self, false, true);
165  }
166 
168  void
169  setObject(const Object* object)
170  {
171  if (isOwner()) delete _object;
172  _object = const_cast<Object*>(object);
173  }
174 
175 private:
176  enum flg_t
177  {
178  flg_visited = 1,
179  flg_owner = 2
180  };
181 
182 private:
183  void
184  init(const Object* object = nullptr)
185  {
186  _object = const_cast<Object*>(object);
187  _edgesHead = _edgesTail = nullptr;
188  _flags = flg_owner;
189  }
190  void
191  deInit()
192  {
193  clear();
194  }
195 
196 private:
197  Object* _object;
198  SlistNode* _edgesHead;
199  SlistNode* _edgesTail;
200  byte_t _flags;
201 };
202 
204 
205 UTL_NS_END;
void setObject(const Object *object)
Set the associated object.
Definition: Vertex.h:169
void setOwner(bool owner)
Set the owner flag.
Definition: Vertex.h:119
bool isOwner() const
Get the owner flag.
Definition: Vertex.h:112
bool isVisited() const
Get the visited flag.
Definition: Vertex.h:129
bool removeDirectedEdge(const Vertex *vertex)
Remove the directed edge to the given vertex.
Definition: Vertex.h:150
bool removeUndirectedEdge(Vertex *vertex)
Remove the undirected edge to the given vertex.
Definition: Vertex.h:161
void deInit()
De-initialize UTL++.
bool slist_remove(SlistNode *&list, SlistNode **tail, const Object &key, bool owner=true, bool sorted=false, const Ordering *ordering=nullptr)
Remove the object matching the given key from the list.
Object * slist_find(SlistNode *list, const Object &key, bool sorted=false, const Ordering *ordering=nullptr)
Find the object matching the given key in the given list.
void clear()
Clear edges and associated object.
Definition: Vertex.h:79
Vertex * find(const Object &key) const
Find the associated vertex matching the given key.
Definition: Vertex.h:98
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
void addUndirectedEdge(Vertex *vertex)
Add an undirected edge to the given vertex.
Definition: Vertex.h:71
unsigned char byte_t
Unsigned character.
Definition: types.h:31
Graph vertex.
Definition: Vertex.h:32
const SlistNode * edges() const
Return the edges.
Definition: Vertex.h:87
void slist_clear(SlistNode *&list, SlistNode **tail, bool owner=true)
Clear a list.
void addDirectedEdge(const Vertex *vertex)
Add a directed edge to the given vertex.
Definition: Vertex.h:64
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
void slist_add(SlistNode *&list, SlistNode **tail, const Object *object, bool sorted=false, const Ordering *ordering=nullptr)
Add an object to a list.
virtual const Object & getKey() const
Get the key for the vertex.
Definition: Vertex.h:54
Vertex(const Object *object)
Constructor.
Definition: Vertex.h:41
Object * getObject() const
Get the associated object.
Definition: Vertex.h:105
void setVisited(bool visited)
Set the visited flag.
Definition: Vertex.h:136
Singly-linked list node.
Definition: SlistNode.h:26
Root of UTL++ class hierarchy.
Definition: Object.h:52
void init()
Initialize UTL++.