summaryrefslogtreecommitdiff
path: root/src/modules/json/JSONIterators.cpp
blob: 5de4ffb9e063e5bb9efb16ed961a83fff3f0b1b8 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-12 Miranda IM, 2012-13 Miranda NG project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "..\..\core\commonheaders.h"

#include "JSONNode.h"

#ifdef JSON_ITERATORS
	#ifdef JSON_REF_COUNT
		#define JSON_ASSERT_UNIQUE(x) JSON_ASSERT(internal -> refcount == 1, json_string(JSON_TEXT(x)) + JSON_TEXT(" in non single reference"))
	#else
		#define JSON_ASSERT_UNIQUE(x) (void)0
	#endif

	#ifdef JSON_MUTEX_CALLBACKS
		#define JSON_MUTEX_COPY2 ,internal -> mylock
	#else
		#define JSON_MUTEX_COPY2
	#endif

JSONNode::json_iterator JSONNode::find(const json_string & name_t){
	JSON_CHECK_INTERNAL();
	JSON_ASSERT(type() == JSON_NODE, JSON_TEXT("finding a non-iteratable node"));
	makeUniqueInternal();
	if (JSONNode ** res = internal -> at(name_t)) {
		return ptr_to_json_iterator(res);
	}
	return end();
}

#ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
	JSONNode::json_iterator JSONNode::find_nocase(const json_string & name_t){
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE, JSON_TEXT("finding a non-iteratable node"));
		makeUniqueInternal();
		if (JSONNode ** res = internal -> at_nocase(name_t)) {
			return ptr_to_json_iterator(res);
		}
		return end();
	}
#endif

JSONNode::json_iterator JSONNode::erase(json_iterator pos){
	JSON_CHECK_INTERNAL();
	JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
	JSON_ASSERT_UNIQUE("erase 1");
	JSON_ASSERT_SAFE(pos < end(), JSON_TEXT("erase out of range"), return end(););
	JSON_ASSERT_SAFE(pos >= begin(), JSON_TEXT("erase out of range"), return begin(););
	deleteJSONNode(*(json_iterator_ptr(pos)));
	internal -> Children.erase(json_iterator_ptr(pos));
	return (empty()) ? end() : pos;
}

JSONNode::json_iterator JSONNode::erase(json_iterator _start, const json_iterator & _end){
	if (_start == _end) return _start;
	JSON_CHECK_INTERNAL();
	JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
	JSON_ASSERT_UNIQUE("erase 3");
	JSON_ASSERT_SAFE(_start <= end(), JSON_TEXT("erase out of lo range"), return end(););
	JSON_ASSERT_SAFE(_end <= end(), JSON_TEXT("erase out of hi range"), return end(););
	JSON_ASSERT_SAFE(_start >= begin(), JSON_TEXT("erase out of lo range"), return begin(););
	JSON_ASSERT_SAFE(_end >= begin(), JSON_TEXT("erase out of hi range"), return begin(););
	for (JSONNode ** pos = json_iterator_ptr(_start); pos < json_iterator_ptr(_end); ++pos){
		deleteJSONNode(*pos);
	}

	internal -> Children.erase(json_iterator_ptr(_start), json_iterator_ptr(_end) - json_iterator_ptr(_start));
	return (empty()) ? end() : _start;
}

#ifdef JSON_LIBRARY
JSONNode::json_iterator JSONNode::insert(json_iterator pos, JSONNode * x){
#else
JSONNode::json_iterator JSONNode::insert(json_iterator pos, const JSONNode & x){
#endif
	JSON_CHECK_INTERNAL();
	JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
	JSON_ASSERT_UNIQUE("insert 1");
	if (json_iterator_ptr(pos) >= internal -> Children.end()) {
		internal -> push_back(x);
		return end() - 1;
	}
	JSON_ASSERT_SAFE(pos >= begin(), JSON_TEXT("insert out of lo range"), return begin(););
	#ifdef JSON_LIBRARY
		internal -> Children.insert(json_iterator_ptr(pos), x);
	#else
		internal -> Children.insert(json_iterator_ptr(pos), newJSONNode(x));
	#endif
	return pos;
}

JSONNode::json_iterator JSONNode::insertFFF(json_iterator pos, JSONNode ** const _start, JSONNode ** const _end){
	JSON_CHECK_INTERNAL();
	JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
	JSON_ASSERT_UNIQUE("insertFFF");
	JSON_ASSERT_SAFE(pos <= end(), JSON_TEXT("insert out of high range"), return end(););
	JSON_ASSERT_SAFE(pos >= begin(), JSON_TEXT("insert out of low range"), return begin(););
	const size_t num = _end - _start;
	json_auto<JSONNode *> mem(num);
	JSONNode ** runner = mem.ptr;
	for (JSONNode ** po = _start; po < _end; ++po){
		*runner++=newJSONNode(*(*po)  JSON_MUTEX_COPY2);
	}
	internal -> Children.insert(json_iterator_ptr(pos), mem.ptr, num);
	return pos;
}

#ifndef JSON_LIBRARY
	JSONNode::const_iterator JSONNode::find(const json_string & name_t) const {
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE, JSON_TEXT("finding a non-iteratable node"));
		if (JSONNode ** res = internal -> at(name_t)) {
			return JSONNode::const_iterator(res);
		}
		return JSONNode::const_iterator(internal -> end());
	}

	#ifdef JSON_CASE_INSENSITIVE_FUNCTIONS
		JSONNode::const_iterator JSONNode::find_nocase(const json_string & name_t) const {
			JSON_CHECK_INTERNAL();
			JSON_ASSERT(type() == JSON_NODE, JSON_TEXT("finding a non-iteratable node"));
			if (JSONNode ** res = internal -> at_nocase(name_t)) {
				return JSONNode::const_iterator(res);
			}
			return JSONNode::const_iterator(internal -> end());
		}
	#endif

	JSONNode::reverse_iterator JSONNode::erase(reverse_iterator pos){
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
		JSON_ASSERT_UNIQUE("erase 2");
		JSON_ASSERT_SAFE(pos < rend(), JSON_TEXT("erase out of range"), return rend(););
		JSON_ASSERT_SAFE(pos >= rbegin(), JSON_TEXT("erase out of range"), return rbegin(););
		deleteJSONNode(*(pos.it));
		internal -> Children.erase(pos.it);
		return (empty()) ? rend() : pos + 1;
	}

	JSONNode::reverse_iterator JSONNode::erase(reverse_iterator _start, const reverse_iterator & _end){
		if (_start == _end) return _start;
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
		JSON_ASSERT_UNIQUE("erase 4");
		JSON_ASSERT_SAFE(_start <= rend(), JSON_TEXT("erase out of lo range"), return rend(););
		JSON_ASSERT_SAFE(_end <= rend(), JSON_TEXT("erase out of hi range"), return rend(););
		JSON_ASSERT_SAFE(_start >= rbegin(), JSON_TEXT("erase out of lo range"), return rbegin(););
		JSON_ASSERT_SAFE(_end >= rbegin(), JSON_TEXT("erase out of hi range"), return rbegin(););
		for (JSONNode ** pos = _start.it; pos > _end.it; --pos){
			deleteJSONNode(*pos);
		}
		const size_t num = _start.it - _end.it;
		internal -> Children.erase(_end.it + 1, num, _start.it);
		return (empty()) ? rend() : _start + num;
	}

	JSONNode::reverse_iterator JSONNode::insert(reverse_iterator pos, const JSONNode & x){
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
		JSON_ASSERT_UNIQUE("insert 1");
		if (pos.it < internal -> Children.begin()) {
			internal -> push_front(x);
			return rend() - 1;
		}
		JSON_ASSERT_SAFE(pos >= rbegin(), JSON_TEXT("insert out of range"), return rbegin(););
		internal -> Children.insert(++pos.it, newJSONNode(x), true);
		return pos;
	}

	JSONNode::reverse_iterator JSONNode::insertRFF(reverse_iterator pos, JSONNode ** const _start, JSONNode ** const _end){
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
		JSON_ASSERT_UNIQUE("insert RFF");
		JSON_ASSERT_SAFE(pos <= rend(), JSON_TEXT("insert out of range"), return rend(););
		JSON_ASSERT_SAFE(pos >= rbegin(), JSON_TEXT("insert out of range"), return rbegin(););
		const size_t num = _end - _start;
		json_auto<JSONNode *> mem(num);
		JSONNode ** runner = mem.ptr + num;
		for (JSONNode ** po = _start; po < _end; ++po){  //fill it backwards
			*(--runner) = newJSONNode(*(*po)    JSON_MUTEX_COPY2);
		}
		internal -> Children.insert(++pos.it, mem.ptr, num);
		return pos - num + 1;
	}

	JSONNode::iterator JSONNode::insertFRR(json_iterator pos, JSONNode ** const _start, JSONNode ** const _end){
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
		JSON_ASSERT_UNIQUE("insert FRR");
		JSON_ASSERT_SAFE(pos <= end(), JSON_TEXT("insert out of range"), return end(););
		JSON_ASSERT_SAFE(pos >= begin(), JSON_TEXT("insert out of range"), return begin(););
		const size_t num = _start - _end;
		json_auto<JSONNode *> mem(num);
		JSONNode ** runner = mem.ptr;
		for (JSONNode ** po = _start; po > _end; --po){
			*runner++=newJSONNode(*(*po)    JSON_MUTEX_COPY2);
		}
		internal -> Children.insert(pos.it, mem.ptr, num);
		return pos;
	}

	JSONNode::reverse_iterator JSONNode::insertRRR(reverse_iterator pos, JSONNode ** const _start, JSONNode ** const _end){
		JSON_CHECK_INTERNAL();
		JSON_ASSERT(type() == JSON_NODE || type() == JSON_ARRAY, JSON_TEXT("erasing a non-iteratable node"));
		JSON_ASSERT_UNIQUE("insert RRR");
		JSON_ASSERT_SAFE(pos <= rend(), JSON_TEXT("insert out of range"), return rend(););
		JSON_ASSERT_SAFE(pos >= rbegin(), JSON_TEXT("insert out of range"), return rbegin(););
		const size_t num = _start - _end;
		json_auto<JSONNode *> mem(num);
		JSONNode ** runner = mem.ptr;
		for (JSONNode ** po = _start; po > _end; --po){
			*runner++=newJSONNode(*(*po)    JSON_MUTEX_COPY2);
		}
		internal -> Children.insert(++pos.it, mem.ptr, num);
		return pos - num + 1;
	}
#endif

#endif