Graph API & internal structures redesign
[hypercube:hypercube.git] / CORE / adjlist.h
1 #ifndef ADJLIST_H_
2 #define ADJLIST_H_
3
4 #include <vector>
5 #include "vertex.h"
6 #include "edge.h"
7
8 class AdjacencyList
9 {
10 public:
11
12         class edge_iterator {
13         public:
14                 edge_iterator() {}
15                 edge_iterator(std::vector<size_t>::iterator it) : _it(it) {}
16
17                 edge_iterator& operator=(const edge_iterator& other)
18                         {_it = other._it; return(*this);}
19                 bool operator==(const edge_iterator& other)
20                         {return(_it == other._it);}
21                 bool operator!=(const edge_iterator& other)
22                         {return(_it != other._it);}
23                 size_t operator*()
24                         {return *_it;}
25                 edge_iterator operator++(int)
26                         {edge_iterator tmp(*this); _it++; return tmp;}
27         private:
28                 std::vector<size_t>::iterator _it;
29         };
30
31         void addVertex()
32                 {_data.push_back(std::vector<size_t>());}
33         void addEdge(Edge *edge)
34         {
35                 _data[edge->src()->id()].push_back(edge->id());
36                 _data[edge->dst()->id()].push_back(edge->id());
37         }
38
39         void clear()
40                 {_data.clear();}
41
42         edge_iterator begin(size_t vertex)
43                 {return edge_iterator(_data[vertex].begin());}
44         edge_iterator end(size_t vertex)
45                 {return edge_iterator(_data[vertex].end());}
46
47 private:
48         std::vector<std::vector<size_t> > _data;
49 };
50
51 #endif // ADJLIST_H_