-
Notifications
You must be signed in to change notification settings - Fork 4
/
bagofwords.cc
57 lines (44 loc) · 1.23 KB
/
bagofwords.cc
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
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <iostream>
#include <unistd.h>
#include "heap.h"
#include "table.h"
struct node {
unsigned count;
const char * string;
node(const char * s) : count(0), string(s) { }
lite::heap_link<node> heap_link;
lite::table_link<node> table_link;
bool
bound() const {
return false
|| table_link.bound()
;;
}
void kill() { if (!bound()) delete this; }
typedef lite::heap<node, &node::heap_link, typeof(node::count), &node::count, lace::reverse_compare<typeof(node::count)> > heap_t;
typedef lite::table<node, &node::table_link, typeof(node::string), &node::string> table_t;
};
int
main(int argc, char* argv[]) {
node::table_t::bucket_t buckets[unsigned(sqrt(argc))];
node::table_t table(buckets, sizeof(buckets)/sizeof(*buckets));
for (int i = 1 ; i < argc ; ++i) {
node* x = table.get(argv[i]);
if (!x)
table.set(x = new node(argv[i]));
++x->count;
}
node::heap_t heap;
for (node* x = table.iterator() ; x ; x = table.next(x))
heap.inhume(x);
while (!heap.empty()) {
node* x = table.bus(heap.exhume());
std::cout << x->count << " " << x->string << std::endl;
x->kill();
}
return EXIT_SUCCESS;
}
//