-
Notifications
You must be signed in to change notification settings - Fork 0
/
amap.cpp
executable file
·148 lines (113 loc) · 2.42 KB
/
amap.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
139
140
141
142
143
144
145
146
#include "amap.h"
#include "awk_like.h"
namespace awk {
map::map(awk_like &a): parent(a) {}
map::Proxy map::operator[](int key)
{
return Proxy(*this, key);
}
const map::Proxy map::operator[](int key) const
{
return Proxy(const_cast<map &>(*this), key);
}
void map::clear()
{
_field.clear();
}
size_t map::size() const
{
return _field.size() - 1;
}
map::Proxy::Proxy(map &m, int k): pm(m), key(k) {}
map::Proxy::operator afield::mapped_type() const
{
return map_get_ro(pm._field, key);
}
afield::mapped_type map::Proxy::get_content() const
{
return static_cast<afield::mapped_type>(*this);
}
const char* map::Proxy::c_str() const
{
return map_get_ro(pm._field, key).c_str();
}
//Proxy of string& operator=(const string& str);
map::Proxy&
map::Proxy::operator=(const Proxy &rhs)
{
pm._field[key] = map_get_ro(rhs.pm._field, rhs.key);
if (key == 0) {
resplit();
} else if (pm.parent.smart_build) {
recompute();
}
return *this;
}
map::Proxy&
map::Proxy::operator=(afield::mapped_type str)
{
pm._field[key] = str;
if (key == 0) {
resplit();
} else if (pm.parent.smart_build) {
recompute();
}
return *this;
}
//Proxy of string& operator=(char c);
map::Proxy&
map::Proxy::operator=(afield::mapped_type::value_type c)
{
pm._field[key] = c;
if (key == 0) {
resplit();
} else if (pm.parent.smart_build) {
recompute();
}
return *this;
}
map::Proxy&
map::Proxy::operator+=(const Proxy &rhs)
{
pm._field[key] += map_get_ro(rhs.pm._field, rhs.key);
if (pm.parent.smart_build)
recompute();
return *this;
}
map::Proxy&
map::Proxy::operator+=(afield::mapped_type str)
{
pm._field[key] += str;
if (pm.parent.smart_build)
recompute();
return *this;
}
//Proxy of char& operator[](size_t pos);
char &map::Proxy::operator[](size_t pos)
{
return map_get_ro(pm._field, key)[pos];
}
//Proxy of const char& operator[](size_t pos) const;
const char &map::Proxy::operator[](size_t pos) const
{
return map_get_ro(pm._field, key)[pos];
}
void map::Proxy::recompute()
{
int i;
auto tail = pm._field.crbegin();
// The last element of ordered map is always the largest one.
// Maybe fill the *holes* inside the map with string("")
pm._field[0] = "";
for (i = 1; i < tail->first; ++i) {
pm._field[0] += pm._field[i];
pm._field[0] += pm.parent.OFS;
}
pm._field[0] += pm._field[i];
pm.parent.NF = pm._field.size() - 1;
}
void map::Proxy::resplit()
{
pm.parent.NF = pm.parent.split(pm._field[0], pm._field);
}
};