COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
cc.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <string>
3#include <fstream>
4#include <sstream>
5#include <vector>
6#include <algorithm>
7#include <chrono>
8#include <functional>
9#include <cmath>
10#include <map>
11#include <tuple>
12#include <cstdlib>
13#include <cstdio>
14#include <limits>
15#include "MMConverter.h"
16#include "DisjSets.h"
17
18using namespace std;
19
20
21// typedef void tommy_foreach_arg_func(void* arg, void* obj);
22
23void* printfunc(void* arg, void* obj)
24{
25 pair<DisjSets*, ofstream*> * mypair = (pair<DisjSets*, ofstream*> *) arg; // cast argument
26 DisjSets * ds = mypair->first;
27 ofstream * out = mypair->second;
28 (*out) << ((tommy_object *) obj)->vname << "\t" << ds->find( (int) ((tommy_object *) obj)->vid) << "\n";
29}
30
31int main(int argc, char* argv[])
32{
33 if(argc < 3)
34 {
35 cout << "Usage: ./cc <vertices_file> <edges_file>" << endl;
36 return 0;
37 }
38
39 ifstream inputvert(argv[1]);
40 char vname[150];
41 uint32_t vertexid = 0;
42
43 tommy_hashdyn hashdyn;
44 tommy_hashdyn_init(&hashdyn);
45 while(inputvert >> vname)
46 {
47 string s_vname(vname); // string version
48 tommy_object* obj = new tommy_object(vertexid, s_vname); // (vertexid,s_vname) pair is the payload (data)
49 tommy_hashdyn_insert(&hashdyn, &(obj->node), obj, tommy_hash_u32(0, vname, strlen(vname))); // hashed string is key
50 vertexid++;
51 }
52 cout << "vertex list read, there are " << vertexid << endl;
53 DisjSets ds(vertexid);
54
55 FILE *f;
56 if ((f = fopen(argv[2], "r")) == NULL)
57 {
58 printf("file %s can not be found\n", argv[2]);
59 exit(1);
60 }
61
62 // Use fseek again to go backwards two bytes and check that byte with fgetc
63 struct stat st; // get file size
64 if (stat(argv[2], &st) == -1)
65 {
66 exit(1);
67 }
68 int64_t file_size = st.st_size;
69 cout << "Edge file is " << file_size << " bytes" << endl;
70 long int ffirst = ftell(f); // doesn't change
71 long int fpos = ffirst;
72 long int end_fpos = file_size;
73
74 vector<string> lines;
75 bool finished = FetchBatch(f, fpos, end_fpos, true, lines); // fpos will move
76 int64_t entriesread = lines.size();
77
78
79 char from[128];
80 char to[128];
81 double vv;
82 for (vector<string>::iterator itr=lines.begin(); itr != lines.end(); ++itr)
83 {
84 // string::c_str() -> Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string)
85 sscanf(itr->c_str(), "%s %s %lg", from, to, &vv);
86 string s_from = string(from);
87 string s_to = string(to);
88
89 tommy_object* obj1 = (tommy_object*) tommy_hashdyn_search(&hashdyn, compare, &s_from, tommy_hash_u32(0, from, strlen(from)));
90 if(!obj1)
91 {
92 cout << "This doesn't make sense! " << s_from << " should exist" << endl;
93 }
94
95 tommy_object* obj2 = (tommy_object*) tommy_hashdyn_search(&hashdyn, compare, &s_to, tommy_hash_u32(0, to, strlen(to)));
96 if(!obj2)
97 {
98 cout << "This doesn't make sense! " << s_to << " should exist" << endl;
99 }
100 int set1 = ds.find((int) obj1->vid);
101 int set2 = ds.find((int) obj2->vid);
102 if(set1 != set2)
103 {
104 ds.unionSets(set1, set2);
105 }
106 }
107 vector<string>().swap(lines);
108
109 while(!finished)
110 {
111 finished = FetchBatch(f, fpos, end_fpos, false, lines);
112 entriesread += lines.size();
113 cout << "entriesread: " << entriesread << ", current vertex id: " << vertexid << endl;
114
115 // Process files
116 char from[128];
117 char to[128];
118 double vv;
119 for (vector<string>::iterator itr=lines.begin(); itr != lines.end(); ++itr)
120 {
121 // string::c_str() -> Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string)
122 sscanf(itr->c_str(), "%s %s %lg", from, to, &vv);
123
124 string s_from = string(from);
125 string s_to = string(to);
126
127 tommy_object* obj1 = (tommy_object*) tommy_hashdyn_search(&hashdyn, compare, &s_from, tommy_hash_u32(0, from, strlen(from)));
128 if(!obj1)
129 {
130 cout << "This doesn't make sense! " << s_from << " should exist" << endl;
131 }
132
133 tommy_object* obj2 = (tommy_object*) tommy_hashdyn_search(&hashdyn, compare, &s_to, tommy_hash_u32(0, to, strlen(to)));
134 if(!obj2)
135 {
136 cout << "This doesn't make sense! " << s_to << " should exist" << endl;
137 }
138 int set1 = ds.find((int) obj1->vid);
139 int set2 = ds.find((int) obj2->vid);
140 if(set1 != set2)
141 {
142 ds.unionSets(set1, set2);
143 }
144 }
145 vector<string>().swap(lines);
146 }
147 cout << "There are " << vertexid << " vertices and " << entriesread << " edges" << endl;
148
149 string s_out(argv[1]);
150 s_out += ".components";
151 ofstream output(s_out);
152
153 pair<DisjSets*, ofstream*> mypair(&ds, &output);
155
156}
int main()
Definition Driver.cpp:12
bool FetchBatch(FILE *f_local, long int &curpos, long int end_fpos, bool firstcall, vector< string > &lines)
int compare(const void *arg, const void *obj)
Definition TommyObj.h:33
void * printfunc(void *arg, void *obj)
Definition cc.cpp:23
int find(int x) const
Definition DisjSets.cpp:36
void unionSets(int root1, int root2)
Definition DisjSets.cpp:18
bool output
Definition comms.cpp:56
tommy_uint32_t tommy_hash_u32(tommy_uint32_t init_val, const void *void_key, tommy_size_t key_len)
tommy_inline void * tommy_hashdyn_search(tommy_hashdyn *hashdyn, tommy_search_func *cmp, const void *cmp_arg, tommy_hash_t hash)
void tommy_hashdyn_init(tommy_hashdyn *hashdyn)
void tommy_hashdyn_insert(tommy_hashdyn *hashdyn, tommy_hashdyn_node *node, void *data, tommy_hash_t hash)
void tommy_hashdyn_foreach_arg(tommy_hashdyn *hashdyn, tommy_foreach_arg_func *func, void *arg)
void tommy_foreach_arg_func(void *arg, void *obj)
Definition tommytypes.h:291
unsigned int uint32_t
Definition stdint.h:80
uint32_t vid
Definition TommyObj.h:10
tommy_node node
Definition TommyObj.h:9