All Distance Sketch  0.1
All distance sketch based algorithms
utils.h
1 #ifndef ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_UTILS_UTILS_H_
2 #define ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_UTILS_UTILS_H_
3 
4 #define FILE_SEPERATOR "/"
5 #define __DEBUG 0
6 
7 
8 namespace all_distance_sketch {
9 namespace utils{
10 
11 typedef std::vector<int> SingleCommunity;
12 typedef std::vector< SingleCommunity > Communities;
13 
14 
15 class FileUtils {
16 
17 public:
18 
19  typedef std::list< std::pair< int, int> > NodePairList;
20 
21  static bool hasEnding (std::string const &fullString, std::string const &ending)
22  {
23  if (fullString.length() >= ending.length()) {
24  return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
25  } else {
26  return false;
27  }
28  }
29 
30  static void getCommunityFromLine(std::string line, SingleCommunity * aComm, std::string delimiter){
31  int i = 0;
32  char * pch;
33  char * p = new char[line.size() + 1];
34  strcpy(p, line.c_str());
35  pch = strtok(p, delimiter.c_str());
36  while (pch != NULL) {
37  int member = std::atoi(pch);
38  pch = strtok(NULL, delimiter.c_str());
39  ++i;
40  aComm->push_back(member);
41  }
42  delete [] p;
43  }
44 
45  static void getNodeIdsFromLine(std::string line, int * u, int * v, std::string delimiter){
46  int values[2] = {0, 0};
47  int i = 0;
48  char * pch;
49  char p[20];
50  strcpy(p, line.c_str());
51  pch = strtok(p, delimiter.c_str());
52  while (pch != NULL) {
53  values[i] = std::atoi(pch);
54  pch = strtok(NULL, delimiter.c_str());
55  ++i;
56  }
57  *u = values[0];
58  *v = values[1];
59  if (__DEBUG == 1){
60  std::cout << "Node src=" << *u << " Node dest=" << *v << std::endl;
61  }
62  }
63 
64  static void GetCommunityFromFile(std::string aFilePath, Communities * aCommunities, std::string delimiter){
65  std::string line;
66  std::ifstream graphFile (aFilePath.c_str());
67  if (__DEBUG){
68  std::cout << "Extacting values from file=" << aFilePath << std::endl;
69  }
70  if (graphFile.is_open())
71  {
72  while ( getline (graphFile,line) )
73  {
74  SingleCommunity com;
75  getCommunityFromLine(line, &com, delimiter);
76  aCommunities->push_back(com);
77  }
78  graphFile.close();
79  }
80  else {
81  std::cout << "Unable to open file" << std::endl;
82  }
83  }
84 
85  static void GetNodePairListFromFile(std::string aFilePath, NodePairList * aNodePairList, std::string delimiter){
86  std::string line;
87  std::ifstream graphFile (aFilePath.c_str());
88  int u, v;
89  if (__DEBUG){
90  std::cout << "Extacting values from file=" << aFilePath << std::endl;
91  }
92  if (graphFile.is_open())
93  {
94  while ( getline (graphFile,line) )
95  {
96  getNodeIdsFromLine(line, &u, &v, delimiter);
97  std::pair<int, int> nodePair = std::pair<int, int>(u, v);
98  aNodePairList->push_back(nodePair);
99  }
100  graphFile.close();
101  }
102  else {
103  std::cout << "Unable to open file" << std::endl;
104  }
105  }
106 
107  static void GetNodePairListFromDir(std::string aPath, NodePairList * aNodePairList) {
108  DIR *dir;
109  struct dirent *ent;
110  if ((dir = opendir (aPath.c_str())) != NULL) {
111  /* print all the files and directories within directory */
112  while ((ent = readdir (dir)) != NULL) {
113  if (utils::FileUtils::hasEnding(ent->d_name, "edges")){
114  std::string fullPath = aPath + FILE_SEPERATOR + ent->d_name;
115  std::string delimiter = " ";
116  utils::FileUtils::GetNodePairListFromFile(fullPath, aNodePairList, delimiter);
117  }
118  if (utils::FileUtils::hasEnding(ent->d_name, "txt")){
119  std::string fullPath = aPath + FILE_SEPERATOR + ent->d_name;
120  std::string delimiter = "\t";
121  utils::FileUtils::GetNodePairListFromFile(fullPath, aNodePairList, delimiter);
122  }
123  if (utils::FileUtils::hasEnding(ent->d_name, "edgelist")){
124  std::string fullPath = aPath + FILE_SEPERATOR + ent->d_name;
125  std::string delimiter = " ";
126  utils::FileUtils::GetNodePairListFromFile(fullPath, aNodePairList, delimiter);
127  }
128  }
129  closedir (dir);
130  } else {
131  /* could not open directory */
132  perror ("could not open directory");
133  }
134  return;
135  }
136 
137  static void GetCommunityFromDir(std::string aPath, Communities * aCommunities){
138  DIR *dir;
139  struct dirent *ent;
140  std::cout << aPath << std::endl;
141  if ((dir = opendir (aPath.c_str())) != NULL) {
142  /* print all the files and directories within directory */
143  while ((ent = readdir (dir)) != NULL) {
144  if (utils::FileUtils::hasEnding(ent->d_name, "txt")){
145  std::string fullPath = aPath + FILE_SEPERATOR + ent->d_name;
146  std::string delimiter = "\t";
147  utils::FileUtils::GetCommunityFromFile(fullPath, aCommunities, delimiter);
148  }
149  }
150  closedir (dir);
151  } else {
152  /* could not open directory */
153  perror ("could not open directory ");
154  }
155  return;
156  }
157 };
158 
159 class Resources {
160 public:
161  static int parseLine(char* line){
162  int i = strlen(line);
163  while (*line < '0' || *line > '9') line++;
164  line[i-3] = '\0';
165  i = atoi(line);
166  return i;
167  }
168 
169 
170  static int getVMValue(){ //Note: this value is in KB!
171  FILE* file = fopen("/proc/self/status", "r");
172  int result = -1;
173  char line[128];
174 
175 
176  while (fgets(line, 128, file) != NULL){
177  if (strncmp(line, "VmSize:", 7) == 0){
178  result = parseLine(line);
179  break;
180  }
181  }
182  fclose(file);
183  return result;
184  }
185 
186  static int getRSSValue(){ //Note: this value is in KB!
187  FILE* file = fopen("/proc/self/status", "r");
188  int result = -1;
189  char line[128];
190 
191 
192  while (fgets(line, 128, file) != NULL){
193  if (strncmp(line, "VmRSS:", 6) == 0){
194  result = parseLine(line);
195  break;
196  }
197  }
198  fclose(file);
199  return result;
200  }
201 };
202 
203 class Community {
204 public:
205  void LoadCommunity(std::string aDirPath) {
206  utils::FileUtils::GetCommunityFromDir(aDirPath, &myCommun);
207  }
208  SingleCommunity * GetCommunity(int i) {
209  return &(myCommun[i]);
210  }
211 
212  int CalcInterection(SingleCommunity * a, SingleCommunity * b) {
213  int intersection = 0;
214  for(unsigned int i=0; i < a->size(); i++) {
215  for(unsigned int j=0; j < b->size(); j++) {
216  if ((*a)[i] == (*b)[j]) {
217  intersection += 1;
218  }
219  }
220  }
221  return intersection;
222  }
223 
224  void GetCommunitiesWithIntersection(long long * aOneId,
225  long long * aTwoId,
226  long long aThreshold ) {
227  for(unsigned int i=0; i < myCommun.size(); i++) {
228  for (unsigned int j=i+1; j < myCommun.size(); j++) {
229  if (CalcInterection(&(myCommun[i]), &(myCommun[j]) ) > aThreshold) {
230  (*aOneId) = i;
231  (*aTwoId) = j;
232  return;
233  }
234  }
235  }
236  (*aOneId) = -1;
237  (*aTwoId) = -1;
238  }
239 
240  void GetNodesDist(std::vector<int> * dist) {
241  for(unsigned int i=0; i < myCommun.size(); i++) {
242  for( unsigned int j=0; j < myCommun[i].size(); j++) {
243  int nId = myCommun[i][j];
244  (*dist)[nId] += 1;
245  }
246  }
247  }
248  Communities myCommun;
249 };
250 
251 } // namespace utils
252 } // namespace all_distance_sketch
253 
254 #endif
Definition: common.h:53