1 #ifndef ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_UTILS_UTILS_H_
2 #define ALL_DISTANCE_SKETCH_ALL_DISTANCE_SKETCH_UTILS_UTILS_H_
4 #define FILE_SEPERATOR "/"
11 typedef std::vector<int> SingleCommunity;
12 typedef std::vector< SingleCommunity > Communities;
19 typedef std::list< std::pair< int, int> > NodePairList;
21 static bool hasEnding (std::string
const &fullString, std::string
const &ending)
23 if (fullString.length() >= ending.length()) {
24 return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
30 static void getCommunityFromLine(std::string line, SingleCommunity * aComm, std::string delimiter){
33 char * p =
new char[line.size() + 1];
34 strcpy(p, line.c_str());
35 pch = strtok(p, delimiter.c_str());
37 int member = std::atoi(pch);
38 pch = strtok(NULL, delimiter.c_str());
40 aComm->push_back(member);
45 static void getNodeIdsFromLine(std::string line,
int * u,
int * v, std::string delimiter){
46 int values[2] = {0, 0};
50 strcpy(p, line.c_str());
51 pch = strtok(p, delimiter.c_str());
53 values[i] = std::atoi(pch);
54 pch = strtok(NULL, delimiter.c_str());
60 std::cout <<
"Node src=" << *u <<
" Node dest=" << *v << std::endl;
64 static void GetCommunityFromFile(std::string aFilePath, Communities * aCommunities, std::string delimiter){
66 std::ifstream graphFile (aFilePath.c_str());
68 std::cout <<
"Extacting values from file=" << aFilePath << std::endl;
70 if (graphFile.is_open())
72 while ( getline (graphFile,line) )
75 getCommunityFromLine(line, &com, delimiter);
76 aCommunities->push_back(com);
81 std::cout <<
"Unable to open file" << std::endl;
85 static void GetNodePairListFromFile(std::string aFilePath, NodePairList * aNodePairList, std::string delimiter){
87 std::ifstream graphFile (aFilePath.c_str());
90 std::cout <<
"Extacting values from file=" << aFilePath << std::endl;
92 if (graphFile.is_open())
94 while ( getline (graphFile,line) )
96 getNodeIdsFromLine(line, &u, &v, delimiter);
97 std::pair<int, int> nodePair = std::pair<int, int>(u, v);
98 aNodePairList->push_back(nodePair);
103 std::cout <<
"Unable to open file" << std::endl;
107 static void GetNodePairListFromDir(std::string aPath, NodePairList * aNodePairList) {
110 if ((dir = opendir (aPath.c_str())) != NULL) {
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);
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);
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);
132 perror (
"could not open directory");
137 static void GetCommunityFromDir(std::string aPath, Communities * aCommunities){
140 std::cout << aPath << std::endl;
141 if ((dir = opendir (aPath.c_str())) != NULL) {
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);
153 perror (
"could not open directory ");
161 static int parseLine(
char* line){
162 int i = strlen(line);
163 while (*line < '0' || *line >
'9') line++;
170 static int getVMValue(){
171 FILE* file = fopen(
"/proc/self/status",
"r");
176 while (fgets(line, 128, file) != NULL){
177 if (strncmp(line,
"VmSize:", 7) == 0){
178 result = parseLine(line);
186 static int getRSSValue(){
187 FILE* file = fopen(
"/proc/self/status",
"r");
192 while (fgets(line, 128, file) != NULL){
193 if (strncmp(line,
"VmRSS:", 6) == 0){
194 result = parseLine(line);
205 void LoadCommunity(std::string aDirPath) {
206 utils::FileUtils::GetCommunityFromDir(aDirPath, &myCommun);
208 SingleCommunity * GetCommunity(
int i) {
209 return &(myCommun[i]);
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]) {
224 void GetCommunitiesWithIntersection(
long long * aOneId,
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) {
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];
248 Communities myCommun;