| CXXGraph 0.4.0 CXXGraph is a header only, that manages the Graphs and it's algorithm in C++

CoordinatedRecord.hpp
1 /***********************************************************/
2 /*** ______ ____ ______ _ ***/
3 /*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/
4 /*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/
5 /*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/
6 /*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/
7 /*** |_| ***/
8 /***********************************************************/
9 /*** Header-Only C++ Library for Graph ***/
10 /*** Representation and Algorithms ***/
11 /***********************************************************/
12 /*** Author: ZigRazor ***/
13 /*** E-Mail: zigrazor@gmail.com ***/
14 /***********************************************************/
15 /*** Collaboration: ----------- ***/
16 /***********************************************************/
17 /*** License: MPL v2.0 ***/
18 /***********************************************************/
19
20 #ifndef __CXXGRAPH_PARTITIONING_COORDINATEDRECORD_H__
21 #define __CXXGRAPH_PARTITIONING_COORDINATEDRECORD_H__
22
23 #pragma once
24
25 #include <set>
26 #include "Utility/Typedef.hpp"
27
28 namespace CXXGRAPH
29 {
30 namespace PARTITIONING
31 {
32
33 template <typename T>
34 class CoordinatedRecord : public Record<T>
35 {
36 private:
37 std::set<int> partitions = {};
38 std::mutex *lock = nullptr;
39 int degree = 0;
40
41 public:
42 CoordinatedRecord();
43 ~CoordinatedRecord();
44
45 std::set<int> &getPartitions();
46 void addPartition(int m);
47 bool hasReplicaInPartition(int m);
48 bool getLock();
49 bool releaseLock();
50 int getReplicas() const;
51 int getDegree();
52 void incrementDegree();
53
54 void addAll(std::set<int> &set);
55 std::set<int> intersection(CoordinatedRecord &x, CoordinatedRecord &y);
56 };
57 template <typename T>
58 CoordinatedRecord<T>::CoordinatedRecord() : partitions()
59 {
60 lock = new std::mutex();
61 degree = 0;
62 }
63 template <typename T>
64 CoordinatedRecord<T>::~CoordinatedRecord()
65 {
66 //std::cout << "CoordinatedRecord<T>::~CoordinatedRecord()" << std::endl;
67 //TODOOOOOOOO
68 if(lock != nullptr){
69 delete lock;
70 }
71 }
72 template <typename T>
73 std::set<int> &CoordinatedRecord<T>::getPartitions()
74 {
75 return partitions;
76 }
77 template <typename T>
78 void CoordinatedRecord<T>::addPartition(int m)
79 {
80 if (m == -1)
81 {
82 std::cout << "ERROR! record.addPartition(-1)" << std::endl;
83 exit(-1);
84 }
85 partitions.insert(m);
86 }
87 template <typename T>
88 bool CoordinatedRecord<T>::hasReplicaInPartition(int m)
89 {
90 return partitions.find(m) != partitions.end();
91 }
92 template <typename T>
93 bool CoordinatedRecord<T>::getLock()
94 {
95 return lock->try_lock();
96 }
97 template <typename T>
98 bool CoordinatedRecord<T>::releaseLock()
99 {
100 lock->unlock();
101 return true;
102 }
103 template <typename T>
104 int CoordinatedRecord<T>::getReplicas() const
105 {
106 return partitions.size();
107 }
108 template <typename T>
109 int CoordinatedRecord<T>::getDegree()
110 {
111 return degree;
112 }
113 template <typename T>
114 void CoordinatedRecord<T>::incrementDegree()
115 {
116 degree++;
117 }
118 template <typename T>
119 void CoordinatedRecord<T>::addAll(std::set<int> &set)
120 {
121 partitions.insert(set.begin(), set.end());
122 }
123 template <typename T>
124 std::set<int> CoordinatedRecord<T>::intersection(CoordinatedRecord &x, CoordinatedRecord &y)
125 {
126 std::set<int> result;
127 set_intersection(x.partitions.begin(), x.partitions.end(), y.partitions.begin(), y.partitions.end(),
128 std::inserter(result, result.begin()));
129 return result;
130 }
131 }
132 }
133
134 #endif // __CXXGRAPH_PARTITIONING_COORDINATEDRECORD_H__
CXXGRAPH::PARTITIONING::CoordinatedRecord
Definition: CoordinatedRecord.hpp:35
CXXGRAPH::PARTITIONING::Record
Definition: Record.hpp:31