Fix handling of subtree_elements. Make the function constant time better-doc smaneth-devel
authorKim Nguyễn <kn@lri.fr>
Fri, 19 Oct 2012 13:09:11 +0000 (15:09 +0200)
committerKim Nguyễn <kn@lri.fr>
Fri, 19 Oct 2012 13:09:29 +0000 (15:09 +0200)
by making use of the internal representation (no need to remove
all the attribute tags one by one but only remove twice the number
of attribute-data tags).

xml-tree-inc.hpp
xml-tree.hpp

index faf4538..18c91e3 100644 (file)
@@ -41,15 +41,12 @@ inline uint32_t xml_tree::subtree_elements(xml_tree::node_t x) const
 
   int32_t size = bp_subtree_size(par, x) - 1;
   if (size <= 0) return 0;
-  size -= subtree_tags(x, xml_tree::PCDATA_OPEN_TAG_ID);
-  size -= subtree_tags(x, xml_tree::ATTRIBUTE_OPEN_TAG_ID);
-  size -= subtree_tags(x, xml_tree::ATTRIBUTE_DATA_OPEN_TAG_ID);
-  if (size < 3) return (uint32_t) size;
-  std::unordered_set<xml_tree::tag_t>::iterator it;
-  for(it = this->attribute_ids->begin();
-      it != this->attribute_ids->end();
-      ++it)
-    size -= subtree_tags(x, *it);
+  uint32_t num_texts = subtree_tags(x, xml_tree::PCDATA_OPEN_TAG_ID);
+  uint32_t num_atts = subtree_tags(x, xml_tree::ATTRIBUTE_OPEN_TAG_ID);
+  uint32_t num_att_data = subtree_tags(x, xml_tree::ATTRIBUTE_DATA_OPEN_TAG_ID);
+  size -= num_texts;
+  size -= num_atts;
+  size -= 2*num_att_data;
   return (uint32_t) size;
 
 }
index 1591285..0795ca0 100644 (file)
@@ -75,7 +75,7 @@ public:
 
   /**
    * [subtree_elements(n)] returns the number of element nodes below [n]
-   * Runs in O(attribute_ids->size()+3)
+   * Runs in O(1)
    */
   inline uint32_t subtree_elements(node_t) const;