-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstatistics.cpp
138 lines (124 loc) · 4.12 KB
/
statistics.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifdef TRACE_TIGHT
#include <vector>
#include <string>
#include <iostream>
std::vector<std::string> constructionTrace;
void constructedWith(const char *how, const std::string *s) {
constructionTrace.push_back(how);
constructionTrace.push_back(*s);
}
void constructedWith(const char *how, const char *buffer, std::size_t length) {
std::string arg{buffer, buffer + length};
constructedWith(how, &arg);
}
template<typename T>
void makeRefAnn(const T *driver, const void *space, const void *vptr) {
std::cout << "Referential made for " << typeid(*driver).name() <<
'@' << driver << " Handling space at " << space << " pointing to " << vptr << std::endl;
}
#define ANNOTATE_MAKE_REFERENTIAL(...) makeRefAnn(__VA_ARGS__);
#define TIGHT_CONSTRUCTOR_ANNOTATION(...) constructedWith(__VA_ARGS__);
#endif
#include "TightPolicy.h"
int stringInternalBufferSize() {
std::string s{"1"};
for(;;) {
auto data = s.data();
std::string other{std::move(s)};
if(data == other.data()) {
// this size first moved the buffer
return other.size();
}
s = other + '/';
}
}
template<typename Iterator>
long occupied(Iterator begin, Iterator end) {
static auto sibs = stringInternalBufferSize();
constexpr auto cut = 1l << 32;
long rv = 0;
for(auto cursor = begin; cursor != end; ++cursor) {
if(!cursor->container()->isPointer()) {
++rv;
continue;
}
if(typeid(std::string) != cursor->type()) { throw; }
rv += cut + 3; // allocates an AnyContainer<ConverterPolicy>
auto str = tightCast<std::string>(*cursor);
auto strS = str.size();
if(sibs <= strS) {
// the string requires allocating a buffer
rv += cut + (strS + 7)/8;
}
}
return rv;
}
#include <vector>
#include <istream>
#include <cctype>
#include <iostream>
void deconstruct(const TightAny &ta) {
auto cont = ta.container();
if(cont->isPointer()) {
void *ptr = cont->code.pointer;
std::cout << "Is pointer: " << ptr << std::endl;
auto f = fallback(cont->code.pointer);
auto innerC = f->container();
std::cout << typeid(*innerC).name() << " at " << innerC << std::endl;
auto driver = innerC->driver();
std::cout << typeid(*driver).name() << " at " << driver << std::endl;
auto &tid = driver->type();
if(typeid(std::string) == tid) {
void *reference = innerC->m_space;
std::string *strPtr = *reinterpret_cast<std::string **>(reference);
std::cout << "Contains std::string ptr at " << reference << std::endl;
std::cout << *strPtr << std::endl;
//auto valPtr = zoo::anyContainerCast<std::string>(f);
//if(valPtr != str)
}
}
}
template<typename Container>
void parse(Container &container, std::istream &input) {
using namespace std;
char buffer[256];
int count = 0;
while(input >> noskipws >> buffer[count]) {
auto c = buffer[count];
if(isspace(c)) {
if(0 == count) { continue; }
std::string orig{buffer, buffer + count};
TightAny a{orig};
container.push_back(a);
count = 0;
continue;
}
if(isalpha(c)) {
if(255 == count) { throw; }
++count;
continue;
}
if(count) {
std::string orig{buffer, buffer + count};
TightAny a{orig};
container.push_back(a);
}
count = 0;
}
}
#include <fstream>
int main(int argc, const char *argv[]) {
std::vector<TightAny> anies;
std::ifstream in{"input"};
parse(anies, in);
std::cout << "Parsed " << anies.size() << std::endl;
auto hum = occupied(anies.begin(), anies.end());
constexpr auto cut = 1l << 32;
std::cout << "Allocations: " << hum / cut << " @" << 8*(hum % cut) << std::endl;
std::cout << "String internal buffer size: " << stringInternalBufferSize()
<< std::endl;
/*for(auto &v: anies) {
std::cout << tightCast<std::string>(v) << std::endl;
}*/
return 0;
}