XPath: xpath_node_set now uses custom allocation functions, minor refactoring

git-svn-id: http://pugixml.googlecode.com/svn/trunk@667 99668b35-9821-0410-8761-19e4c4f06640
This commit is contained in:
arseny.kapoulkine 2010-08-29 15:29:45 +00:00
parent f2509e0395
commit f711006e80
2 changed files with 42 additions and 28 deletions

View File

@ -2212,6 +2212,8 @@ namespace pugi
void remove_duplicates(); void remove_duplicates();
void swap(xpath_node_set& ns);
public: public:
/** /**
* Default constructor * Default constructor
@ -2238,7 +2240,7 @@ namespace pugi
* \return self * \return self
*/ */
xpath_node_set& operator=(const xpath_node_set& ns); xpath_node_set& operator=(const xpath_node_set& ns);
/** /**
* Get collection type * Get collection type
* *

View File

@ -194,12 +194,6 @@ namespace
return duplicate_string(string, impl::strlen(string)); return duplicate_string(string, impl::strlen(string));
} }
void _swap(xpath_string& o)
{
pstd::swap(_buffer, o._buffer);
pstd::swap(_uses_heap, o._uses_heap);
}
public: public:
xpath_string(): _buffer(PUGIXML_TEXT("")), _uses_heap(false) xpath_string(): _buffer(PUGIXML_TEXT("")), _uses_heap(false)
{ {
@ -247,11 +241,17 @@ namespace
xpath_string& operator=(const xpath_string& o) xpath_string& operator=(const xpath_string& o)
{ {
xpath_string temp(o); xpath_string temp(o);
_swap(temp); swap(temp);
return *this; return *this;
} }
void swap(xpath_string& o)
{
pstd::swap(_buffer, o._buffer);
pstd::swap(_uses_heap, o._uses_heap);
}
void append(const xpath_string& o) void append(const xpath_string& o)
{ {
// skip empty sources // skip empty sources
@ -280,7 +280,7 @@ namespace
result[length] = 0; result[length] = 0;
// finalize // finalize
xpath_string(result, true)._swap(*this); xpath_string(result, true).swap(*this);
} }
} }
@ -1106,36 +1106,43 @@ namespace pugi
xpath_node_set::~xpath_node_set() xpath_node_set::~xpath_node_set()
{ {
if (_begin != &_storage) delete[] _begin; if (_begin != &_storage) get_memory_deallocation_function()(_begin);
} }
xpath_node_set::xpath_node_set(const xpath_node_set& ns): _type(type_unsorted), _begin(&_storage), _end(&_storage), _eos(&_storage + 1) xpath_node_set::xpath_node_set(const xpath_node_set& ns): _type(ns._type), _begin(&_storage), _end(&_storage), _eos(&_storage + 1)
{ {
*this = ns;
}
xpath_node_set& xpath_node_set::operator=(const xpath_node_set& ns)
{
if (&ns == this) return *this;
if (_begin != &_storage) delete[] _begin;
_begin = _end = _eos = 0;
_type = ns._type;
if (ns.size() == 1) if (ns.size() == 1)
{ {
_storage = *ns._begin; _storage = *ns._begin;
_begin = &_storage; _end = _eos;
_end = _eos = &_storage + 1;
} }
else else
{ {
append(ns.begin(), ns.end()); append(ns.begin(), ns.end());
} }
}
xpath_node_set& xpath_node_set::operator=(const xpath_node_set& ns)
{
if (this == &ns) return *this;
// clear & append
_type = ns._type;
_end = _begin;
append(ns.begin(), ns.end());
return *this; return *this;
} }
void xpath_node_set::swap(xpath_node_set& ns)
{
pstd::swap(_type, ns._type);
pstd::swap(_storage, ns._storage);
pstd::swap(_begin, ns._begin);
pstd::swap(_end, ns._end);
pstd::swap(_eos, ns._eos);
}
xpath_node_set::type_t xpath_node_set::type() const xpath_node_set::type_t xpath_node_set::type() const
{ {
@ -1207,10 +1214,13 @@ namespace pugi
while (capacity < size + count) capacity += capacity / 2; while (capacity < size + count) capacity += capacity / 2;
xpath_node* storage = new xpath_node[capacity]; xpath_node* storage = static_cast<xpath_node*>(get_memory_allocation_function()(capacity * sizeof(xpath_node)));
if (!storage) return; // $$ out of memory
pstd::copy(_begin, _end, storage); pstd::copy(_begin, _end, storage);
// memcpy(storage, _begin, size * sizeof(xpath_node));
if (_begin != &_storage) delete[] _begin; if (_begin != &_storage) get_memory_deallocation_function()(_begin);
_begin = storage; _begin = storage;
_end = storage + size; _end = storage + size;
@ -1218,6 +1228,7 @@ namespace pugi
} }
pstd::copy(begin, end, _end); pstd::copy(begin, end, _end);
// memcpy(_end, begin, count * sizeof(xpath_node));
_end += count; _end += count;
} }
@ -2828,6 +2839,7 @@ namespace pugi
assert(!_right); // root step can't have any predicates assert(!_right); // root step can't have any predicates
xpath_node_set ns; xpath_node_set ns;
ns._type = xpath_node_set::type_sorted;
if (c.n.node()) ns.push_back(c.n.node().root()); if (c.n.node()) ns.push_back(c.n.node().root());
else if (c.n.attribute()) ns.push_back(c.n.parent().root()); else if (c.n.attribute()) ns.push_back(c.n.parent().root());