-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsets.cpp
123 lines (115 loc) · 2.97 KB
/
sets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef UNIQUE_SETS_CPP
#define UNIQUE_SETS_CPP
#include "sets.h"
using namespace std;
UniqueSets::UniqueSets( )
{
root = new Node(0);
n_duplicated = n_unique = most_frequent_n = 0;
}
UniqueSets::~UniqueSets()
{
delete root;
}
bool UniqueSets::add_set(string line)
{
vector<int> s;
if(line.back() == '\n') // discard \n
line.pop_back();
if(line.back() == '\r') // damn windows
line.pop_back();
populate_vector(line, s);
if(s.empty())
{
invalid_str.push_back(line);
return false;
}
radix::radixsort(&s[0], s.size());
int n_added = root->add_path(s);
if(n_added > 1) // First time adding this string?
{
n_duplicated++;
if( n_added > most_frequent_n )
{
most_frequent = line;
most_frequent_n = n_added;
}
}
else
n_unique++;
return (n_added == 1);
}
vector<string>& UniqueSets::get_invalids()
{
return invalid_str;
}
string UniqueSets::get_most_repeated( int& number_repeated ) const
{
if(most_frequent_n > 1)
number_repeated = most_frequent_n-1;
else
number_repeated = 0;
return most_frequent;
}
vector<int> UniqueSets::get_most_repeated_vec( int& number_repeated )
{
vector<int> vec;
number_repeated = most_frequent_n;
if(most_frequent_n==0)
return vec;
number_repeated--; // 5 equal sets means 1 unique and 4 duplicate, so we decrement 1 here.
populate_vector(most_frequent, vec);
if(vec.size() > 1)
radix::radixsort(&vec[0], vec.size());
return vec;
}
void UniqueSets::get_n_duplicates(unsigned int& duplicates, unsigned int& unique) const
{
duplicates = n_duplicated;
unique = n_unique;
}
inline bool UniqueSets::is_integer(const std::string &s)
{
// I'm not sure if I should handle signs -/+
if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+')))
return false;
char* p;
strtol(s.c_str(), &p, 10);
return (*p == 0);
}
/* Why am I using the ol' strtok here?
* Well, long story short, I was running into problems using ifstringstream, so at one point
* I got so upset that I just switched to strtok, but only after I did this, I realized that
* the problem was the lame window's line-endings like \r\n instead of just \n.
* I could have moved back to ifstringstream, but there is no point if this is already working.
* */
void UniqueSets::populate_vector(string line, vector<int>& s)
{
if(line.empty())
return;
const char c[2] = ",";
char *token;
char* str = new char[line.size() + 1];
int i;
for(i=0; i<line.size(); ++i)
str[i] = line[i];
str[i] = '\0';
token = strtok(str, c);
while( token != NULL )
{
if(token[0]==' ')
++token;
if(is_integer(token))
{
s.push_back(atoi(token));
}
else
{
s.clear();
break;
}
token = strtok(NULL, c);
}
free(str);
}
#endif