-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsset.h
160 lines (147 loc) · 2.31 KB
/
jsset.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
struct Rc {
var *ref;
int count;
};
struct arrset {
Rc *rc;
var *vr;
arrset () {
rc = new Rc;
rc->ref = new var;
rc->count = 1;
}
arrset (var *a, int dummy) {
//dummy is to remove "ambiguous" conflict
rc = 0;
vr = a;
}
arrset(arrset &a) {
if (a.rc) {
rc = a.rc;
rc->count++;
}
else vr = a.vr, rc = 0;
}
~arrset() {
if (rc && rc->ref && --rc->count == 0) {
delete rc->ref;
delete rc;
}
}
var *get() {
if (rc) return rc->ref;
return vr;
}
operator var& (){
return *get();
}
arrset &operator , (var b) {
if (get()->type == varArr) get()->push(b);
return self;
}
arrset &operator = (var b) {
(*get()) = Array;
get()->push(b);
return self;
}
};
struct objset {
Rc *rc;
var *vr;
objset () {
rc = new Rc;
rc->ref = new var;
rc->count = 1;
}
objset (var *a) {
rc = 0;
vr = a;
}
objset(objset &a) {
if (a.rc) {
rc = a.rc;
rc->count++;
}
else vr = a.vr, rc = 0;
}
~objset() {
if (rc && rc->ref && --rc->count == 0) {
delete rc->ref;
delete rc;
}
}
var *get() {
if (rc) return rc->ref;
return vr;
}
void set(var *a) {
if (rc) rc->ref = a;
vr = a;
}
operator var& (){
return *get();
}
chr *key;
stk stack;
var& __value() {
var t;
t.makeStringToSet();
t._chr().set(key->s, key->size);
return (*get())[t];
}
objset &operator , (varSyntax b) {
if (b == Object) {
__value() = Object;
stack.push(get());
set(& __value());
delete key;
key = 0;
} else if (b == end) {
set((var*) stack.pop());
}
return self;
}
objset &operator , (var b) {
if (get()->type == varObj) {
if (key) {
__value() = b;
delete key;
key = 0;
} else {
key = new chr;
b = b.toString();
key->set(b._chr().s, b._chr().size);
}
}
return self;
}
objset &operator = (var b) {
(*get()) = Object;
key = new chr;
b = b.toString();
key->set(b._chr().s, b._chr().size);
return self;
}
};
objset var::obj() {
objset R;
return R;
}
arrset var::arr() {
arrset R;
return R;
}
//arrset var::Arr() {
// arrset R(this, 1);
// return R;
//}
//
//objset var::Obj() {
// objset R(this);
// return R;
//}
//objset (varSyntax) {
//}
//maybe that would be better than var::initObj() = 1,2,3,4
// var a = lit(arr) = 1,2,3,4;
//var a = lit(obj) = "a",1,"b",2,"c",3,"d",4;