-
-
Save jgc128/c19a2385b9a15250d641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS 1 | |
#include <stdio.h> | |
#include <string.h> | |
#include <map> | |
#include <vector> | |
#include <set> | |
#include <algorithm> | |
#include <iterator> | |
using namespace std; | |
map<unsigned long, vector<unsigned long>> load_group_users(char* user_groups_filename) | |
{ | |
map<unsigned long, vector<unsigned long>> result; | |
// Open file | |
auto user_group_file = fopen(user_groups_filename, "r"); | |
char line_buff[128], user_str[64], group_str[64]; | |
char * comma_pos; | |
size_t line_len, comma_len; | |
long user, group; | |
// Read file line-by-line | |
while (fgets(line_buff, sizeof line_buff, user_group_file) != NULL) | |
{ | |
memset(user_str, 0, sizeof user_str); | |
memset(group_str, 0, sizeof group_str); | |
line_len = strlen(line_buff); | |
comma_pos = strchr(line_buff, ','); | |
comma_len = comma_pos - line_buff; | |
strncpy(user_str, line_buff, comma_len); | |
strncpy(group_str, comma_pos + 1, line_len - comma_len - 2); | |
user = atol(user_str); | |
group = atol(group_str); | |
if (result.find(group) == result.end()) | |
{ | |
result[group].reserve(100); | |
} | |
result[group].push_back(user); | |
printf("Load %lu - %lu\n", group, user); | |
} | |
fclose(user_group_file); | |
return result; | |
}; | |
void calc_and_write_group_shr(map<unsigned long, vector<unsigned long>> group_users_data, char * output_filename) | |
{ | |
auto file = fopen(output_filename, "w"); | |
for (auto &g1 : group_users_data) | |
{ | |
auto ud = g1.second; | |
sort(ud.begin(), ud.end()); | |
} | |
for (auto &g1 : group_users_data) | |
{ | |
for (auto &g2 : group_users_data) | |
{ | |
if (g1 <= g2) | |
{ | |
auto u1 = g1.second; | |
auto u2 = g2.second; | |
set<unsigned long> inter_set; | |
set_intersection( | |
u1.begin(), u1.end(), | |
u2.begin(), u2.end(), | |
inserter(inter_set, inter_set.begin())); | |
auto inter_len = inter_set.size(); | |
if (inter_len > 0) | |
fprintf(file, "%lu,%lu,%zu", g1.first, g2.first, inter_len); | |
printf("Handled %lu - %lu\n", g1.first, g2.first); | |
} | |
} | |
} | |
fclose(file); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc != 3) | |
{ | |
printf("Specify i/o files!"); | |
return 1; | |
} | |
auto input_filename = argv[1]; | |
auto output_filename = argv[2]; | |
auto group_users_data = load_group_users(input_filename); | |
calc_and_write_group_shr(group_users_data, output_filename); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment