From: <vac...@us...> - 2009-02-06 22:42:27
|
Revision: 131 http://xmlwrapp.svn.sourceforge.net/xmlwrapp/?rev=131&view=rev Author: vaclavslavik Date: 2009-02-06 22:42:15 +0000 (Fri, 06 Feb 2009) Log Message: ----------- improved performance of invalid iterators: don't allocate pimpl for them Modified Paths: -------------- trunk/include/xmlwrapp/node.h trunk/include/xmlwrapp/nodes_view.h trunk/src/libxml/node.cxx trunk/src/libxml/node_iterator.cxx trunk/src/libxml/node_iterator.h Modified: trunk/include/xmlwrapp/node.h =================================================================== --- trunk/include/xmlwrapp/node.h 2009-01-26 17:00:08 UTC (rev 130) +++ trunk/include/xmlwrapp/node.h 2009-02-06 22:42:15 UTC (rev 131) @@ -1,5 +1,6 @@ /* * Copyright (C) 2001-2003 Peter J Jones (pj...@pm...) + * 2009 Vaclav Slavik <vs...@fa...> * All Rights Reserved * * Redistribution and use in source and binary forms, with or without @@ -431,7 +432,7 @@ typedef value_type& reference; typedef std::forward_iterator_tag iterator_category; - iterator (void); + iterator (void) : pimpl_(0) {} iterator (const iterator &other); iterator& operator= (const iterator& other); ~iterator (void); @@ -445,12 +446,15 @@ /// postfix increment (avoid if possible for better performance) iterator operator++ (int); - friend bool operator== (const iterator &lhs, const iterator &rhs); - friend bool operator!= (const iterator &lhs, const iterator &rhs); + bool operator==(const iterator& other) const + { return get_raw_node() == other.get_raw_node(); } + bool operator!=(const iterator& other) const + { return !(*this == other); } + private: impl::nipimpl *pimpl_; explicit iterator (void *data); - void* get_raw_node (void); + void* get_raw_node (void) const; void swap (iterator &other); friend class node; friend class document; @@ -470,7 +474,7 @@ typedef value_type& reference; typedef std::forward_iterator_tag iterator_category; - const_iterator (void); + const_iterator (void) : pimpl_(0) {} const_iterator (const const_iterator &other); const_iterator (const iterator &other); const_iterator& operator= (const const_iterator& other); @@ -485,12 +489,14 @@ /// postfix increment (avoid if possible for better performance) const_iterator operator++ (int); - friend bool operator== (const const_iterator &lhs, const const_iterator &rhs); - friend bool operator!= (const const_iterator &lhs, const const_iterator &rhs); + bool operator==(const const_iterator& other) const + { return get_raw_node() == other.get_raw_node(); } + bool operator!=(const const_iterator& other) const + { return !(*this == other); } private: impl::nipimpl *pimpl_; explicit const_iterator (void *data); - void* get_raw_node (void); + void* get_raw_node (void) const; void swap (const_iterator &other); friend class document; friend class node; Modified: trunk/include/xmlwrapp/nodes_view.h =================================================================== --- trunk/include/xmlwrapp/nodes_view.h 2009-01-26 17:00:08 UTC (rev 130) +++ trunk/include/xmlwrapp/nodes_view.h 2009-02-06 22:42:15 UTC (rev 131) @@ -94,7 +94,7 @@ typedef value_type& reference; typedef std::forward_iterator_tag iterator_category; - iterator(); + iterator() : pimpl_(0), advance_func_(0) {} iterator(const iterator& other); iterator& operator=(const iterator& other); ~iterator(); @@ -105,11 +105,14 @@ iterator& operator++(); iterator operator++(int); - bool operator==(const iterator& other) const; - bool operator!=(const iterator& other) const { return !(*this == other); } + bool operator==(const iterator& other) const + { return get_raw_node() == other.get_raw_node(); } + bool operator!=(const iterator& other) const + { return !(*this == other); } private: explicit iterator(void *data, impl::iter_advance_functor *advance_func); + void* get_raw_node() const; void swap(iterator& other); impl::nipimpl *pimpl_; @@ -137,7 +140,7 @@ typedef value_type& reference; typedef std::forward_iterator_tag iterator_category; - const_iterator(); + const_iterator() : pimpl_(0), advance_func_(0) {} const_iterator(const const_iterator& other); const_iterator(const iterator& other); const_iterator& operator=(const const_iterator& other); @@ -150,11 +153,14 @@ const_iterator& operator++(); const_iterator operator++(int); - bool operator==(const const_iterator& other) const; - bool operator!=(const const_iterator& other) const { return !(*this == other); } + bool operator==(const const_iterator& other) const + { return get_raw_node() == other.get_raw_node(); } + bool operator!=(const const_iterator& other) const + { return !(*this == other); } private: explicit const_iterator(void *data, impl::iter_advance_functor *advance_func); + void* get_raw_node() const; void swap(const_iterator& other); impl::nipimpl *pimpl_; Modified: trunk/src/libxml/node.cxx =================================================================== --- trunk/src/libxml/node.cxx 2009-01-26 17:00:08 UTC (rev 130) +++ trunk/src/libxml/node.cxx 2009-02-06 22:42:15 UTC (rev 131) @@ -1,5 +1,6 @@ /* * Copyright (C) 2001-2003 Peter J Jones (pj...@pm...) + * 2009 Vaclav Slavik <vs...@fa...> * All Rights Reserved * * Redistribution and use in source and binary forms, with or without Modified: trunk/src/libxml/node_iterator.cxx =================================================================== --- trunk/src/libxml/node_iterator.cxx 2009-01-26 17:00:08 UTC (rev 130) +++ trunk/src/libxml/node_iterator.cxx 2009-02-06 22:42:15 UTC (rev 131) @@ -1,5 +1,6 @@ /* * Copyright (C) 2001-2003 Peter J Jones (pj...@pm...) + * 2009 Vaclav Slavik <vs...@fa...> * All Rights Reserved * * Redistribution and use in source and binary forms, with or without @@ -76,16 +77,12 @@ */ //#################################################################### -xml::node::iterator::iterator (void) { - pimpl_ = new nipimpl; -} -//#################################################################### xml::node::iterator::iterator (void *data) { pimpl_ = new nipimpl(static_cast<xmlNodePtr>(data)); } //#################################################################### xml::node::iterator::iterator (const iterator &other) { - pimpl_ = new nipimpl(*(other.pimpl_)); + pimpl_ = other.pimpl_ ? new nipimpl(*(other.pimpl_)) : 0; } //#################################################################### xml::node::iterator& xml::node::iterator::operator= (const iterator &other) { @@ -121,8 +118,8 @@ return tmp; } //#################################################################### -void* xml::node::iterator::get_raw_node (void) { - return pimpl_->it.get_raw_node(); +void* xml::node::iterator::get_raw_node (void) const { + return pimpl_ ? pimpl_->it.get_raw_node() : 0; } //#################################################################### @@ -131,20 +128,16 @@ */ //#################################################################### -xml::node::const_iterator::const_iterator (void) { - pimpl_ = new nipimpl; -} -//#################################################################### xml::node::const_iterator::const_iterator (void *data) { pimpl_ = new nipimpl(static_cast<xmlNodePtr>(data)); } //#################################################################### xml::node::const_iterator::const_iterator (const const_iterator &other) { - pimpl_ = new nipimpl(*(other.pimpl_)); + pimpl_ = other.pimpl_ ? new nipimpl(*(other.pimpl_)) : 0; } //#################################################################### xml::node::const_iterator::const_iterator (const iterator &other) { - pimpl_ = new nipimpl(*(other.pimpl_)); + pimpl_ = other.pimpl_ ? new nipimpl(*(other.pimpl_)) : 0; } //#################################################################### xml::node::const_iterator& xml::node::const_iterator::operator= (const const_iterator &other) { @@ -180,41 +173,12 @@ return tmp; } //#################################################################### -void* xml::node::const_iterator::get_raw_node (void) { - return pimpl_->it.get_raw_node(); +void* xml::node::const_iterator::get_raw_node (void) const { + return pimpl_ ? pimpl_->it.get_raw_node() : 0; } //#################################################################### -namespace xml { -namespace impl { - bool operator== (const node_iterator &lhs, const node_iterator &rhs) { - return lhs.node_ == rhs.node_; - } - bool operator!= (const node_iterator &lhs, const node_iterator &rhs) { - return !(lhs == rhs); - } -} - - bool operator== (const node::iterator &lhs, const node::iterator &rhs) { - return lhs.pimpl_->it == rhs.pimpl_->it; - } - - bool operator!= (const node::iterator &lhs, const node::iterator &rhs) { - return !(lhs == rhs); - } - - bool operator== (const node::const_iterator &lhs, const node::const_iterator &rhs) { - return lhs.pimpl_->it == rhs.pimpl_->it; - } - - bool operator!= (const node::const_iterator &lhs, const node::const_iterator &rhs) { - return !(lhs == rhs); - } -} -//#################################################################### - - namespace xml { @@ -222,15 +186,9 @@ // xml::nodes_view::iterator // ------------------------------------------------------------------------ -nodes_view::iterator::iterator() -{ - pimpl_ = new nipimpl; - advance_func_ = 0; -} - nodes_view::iterator::iterator(const iterator& other) { - pimpl_ = new nipimpl(*(other.pimpl_)); + pimpl_ = other.pimpl_ ? new nipimpl(*(other.pimpl_)) : 0; advance_func_ = other.advance_func_; } @@ -273,9 +231,9 @@ return tmp; } -bool nodes_view::iterator::operator==(const iterator& other) const +void* nodes_view::iterator::get_raw_node() const { - return pimpl_->it == other.pimpl_->it; + return pimpl_ ? pimpl_->it.get_raw_node() : 0; } nodes_view::iterator::iterator(void *data, impl::iter_advance_functor *advance_func) @@ -295,21 +253,15 @@ // xml::nodes_view::const_iterator // ------------------------------------------------------------------------ -nodes_view::const_iterator::const_iterator() -{ - pimpl_ = new nipimpl; - advance_func_ = 0; -} - nodes_view::const_iterator::const_iterator(const const_iterator& other) { - pimpl_ = new nipimpl(*(other.pimpl_)); + pimpl_ = other.pimpl_ ? new nipimpl(*(other.pimpl_)) : 0; advance_func_ = other.advance_func_; } nodes_view::const_iterator::const_iterator(const iterator& other) { - pimpl_ = new nipimpl(*(other.pimpl_)); + pimpl_ = other.pimpl_ ? new nipimpl(*(other.pimpl_)) : 0; advance_func_ = other.advance_func_; } @@ -360,9 +312,9 @@ return tmp; } -bool nodes_view::const_iterator::operator==(const const_iterator& other) const +void* nodes_view::const_iterator::get_raw_node() const { - return pimpl_->it == other.pimpl_->it; + return pimpl_ ? pimpl_->it.get_raw_node() : 0; } nodes_view::const_iterator::const_iterator(void *data, impl::iter_advance_functor *advance_func) Modified: trunk/src/libxml/node_iterator.h =================================================================== --- trunk/src/libxml/node_iterator.h 2009-01-26 17:00:08 UTC (rev 130) +++ trunk/src/libxml/node_iterator.h 2009-02-06 22:42:15 UTC (rev 131) @@ -1,5 +1,6 @@ /* * Copyright (C) 2001-2003 Peter J Jones (pj...@pm...) + * 2009 Vaclav Slavik <vs...@fa...> * All Rights Reserved * * Redistribution and use in source and binary forms, with or without @@ -94,9 +95,7 @@ void advance() { node_ = node_->next; } void advance(iter_advance_functor& next) { node_ = next(node_); } - friend bool operator== (const node_iterator &lhs, const node_iterator &rhs); - private: mutable node fake_node_; xmlNodePtr node_; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |