252
252
void StoreNodeSortIterator::open()
258
258
bool StoreNodeSortIterator::next(store::Item_t& result)
260
if (theCurrentNode < 0)
260
if (theCurrentNode == 0)
264
// We are not done yet with gathering all XML nodes.
265
while (theInput->next(item))
266
store::Item_t contextNode;
267
if (!theInput->next(contextNode))
270
ZORBA_ASSERT(contextNode->isNode());
272
theNodes.push_back(reinterpret_cast<XmlNode*>(contextNode.release()));
269
#ifdef ZORBA_WITH_JSON
270
// If no JSON item should be found (like in a path expression), this
271
// is handled by the consumer of this iterator.
272
ZORBA_ASSERT(item->isJSONObject() || item->isJSONArray());
274
json::JSONItem* jsonItem = static_cast<json::JSONItem*>(item.getp());
278
std::set<json::JSONItem*>::const_iterator ite = theJSONItems.find(jsonItem);
279
if (ite == theJSONItems.end())
281
jsonItem = static_cast<json::JSONItem*>(item.release());
282
theJSONItems.insert(jsonItem);
293
ZORBA_ASSERT_WITH_MSG(false, "Non-node found in node sorting iterator.");
297
theNodes.push_back(reinterpret_cast<XmlNode*>(item.release()));
300
// We are out of items. We can now begin to output the nodes. In the next
301
// iteration, this part of the code will be skipped.
275
302
ComparisonFunction cmp(theAscending);
277
304
std::sort(theNodes.begin(), theNodes.end(), cmp);
280
if (theCurrentNode < (long)theNodes.size())
307
if (theCurrentNode < theNodes.size())
284
311
result = theNodes[theCurrentNode++];
286
while (theCurrentNode < (long)theNodes.size() &&
313
while (theCurrentNode < theNodes.size() &&
287
314
theNodes[theCurrentNode] == result)
289
316
theCurrentNode++;
329
367
// which wraps this store iterator.
331
369
csize numNodes = theNodes.size();
332
for (csize i = 0; i < numNodes; i++)
370
for (csize i = 0; i < numNodes; ++i)
334
372
XmlNode* n = theNodes[i];
335
373
n->removeReference();
338
376
theNodes.clear();
379
#ifdef ZORBA_WITH_JSON
380
for (std::set<json::JSONItem*>::iterator ite = theJSONItems.begin();
381
ite != theJSONItems.end();
384
json::JSONItem* n = *ite;
385
n->removeReference();
387
theJSONItems.clear();