Revert "Optimize subtree_elements by calling find_close only once."
[SXSI/XMLTree.git] / xml-tree.cpp
index 758c1b4..8c0386e 100644 (file)
@@ -42,7 +42,13 @@ static int bits8 (int t ) {
 }
 
 
-
+static bool array_mem(xml_tree::tag_t *a, xml_tree::tag_t t)
+{
+  for(; *a != xml_tree::NIL_TAG_ID; a++){
+    if (*a >= t) return (*a == t);
+  };
+  return false;
+}
 
 static void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  {
@@ -121,11 +127,7 @@ xml_tree::xml_tree(std::vector<int32_t> *tags,
     delete textbitmap;
 
     this->text_index_type = idx_type;
-    fprintf(stderr, "Before!\n");
-    fflush(stderr);
     text_collection = tc_builder->InitTextCollection();
-    fprintf(stderr, "After!\n");
-    fflush(stderr);
     delete tc_builder;
   };
 
@@ -166,52 +168,48 @@ uint32_t xml_tree::postorder(xml_tree::node_t x) const
 }
 
 xml_tree::node_t
-xml_tree::select_child(xml_tree::node_t x,
-                       std::unordered_set<tag_t> *tags) const
+xml_tree::select_child(xml_tree::node_t x, xml_tree::tag_t *tags) const
 {
   if (is_nil(x) || is_leaf(x)) return xml_tree::NIL;
   xml_tree::node_t child = first_child(x);
-  if (tags->find(tag(child)) != tags->end()) return child;
+  if (array_mem(tags, tag(child))) return child;
   return select_sibling(child, tags);
 }
 
 xml_tree::node_t
-xml_tree::select_descendant(xml_tree::node_t x,
-                            std::unordered_set<tag_t> *tags) const
+xml_tree::select_descendant(xml_tree::node_t x, xml_tree::tag_t *tags) const
 {
   if (is_leaf(x)) return xml_tree::NIL;
   auto min = xml_tree::NIL;
   auto aux = xml_tree::NIL;
-  for(auto tag = tags->begin(); tag != tags->end(); ++ tag){
-    aux = tagged_descendant(x, *tag);
+  for(; *tags != xml_tree::NIL_TAG_ID; ++tags){
+    aux = tagged_descendant(x, *tags);
     if ((unsigned int) aux < (unsigned int) min) min = aux;
   };
   return min;
 }
 
 xml_tree::node_t
-xml_tree::select_sibling(xml_tree::node_t x,
-                         std::unordered_set<tag_t> *tags) const
+xml_tree::select_sibling(xml_tree::node_t x, xml_tree::tag_t *tags) const
 {
   xml_tree::node_t sibling = next_sibling(x);
   xml_tree::tag_t t;
   while(!is_nil(sibling)) {
     t = tag(sibling);
-    if (tags->find(t) != tags->end()) return sibling;
+    if (array_mem(tags, t)) return sibling;
     sibling = next_sibling(sibling);
   };
   return sibling;
 }
 
 xml_tree::node_t
-xml_tree::select_following_before(xml_tree::node_t x,
-                                  std::unordered_set<tag_t> *tags,
+xml_tree::select_following_before(xml_tree::node_t x, xml_tree::tag_t *tags,
                                   xml_tree::node_t limit) const
 {
   auto min = xml_tree::NIL;
   auto aux = xml_tree::NIL;
-  for(auto tag = tags->begin(); tag != tags->end(); ++tag){
-    aux = tagged_following_before(x, *tag, limit);
+  for(; *tags != xml_tree::NIL_TAG_ID; ++tags){
+    aux = tagged_following_before(x, *tags, limit);
     if ((unsigned int) aux < (unsigned int) min) min = aux;
   }
   return min;
@@ -242,8 +240,7 @@ void xml_tree::save(int fd, char* s)
   bool disable_tc = text_collection == 0 || text_positions == 0;
 
   ufwrite(&disable_tc, sizeof(bool),1,fp);
-  fprintf(stderr, "whoot\n");
-  fflush(stderr);
+
   if (!disable_tc) {
     text_positions->save(fp);
     ufwrite(&text_index_type,
@@ -413,6 +410,7 @@ void xml_tree::uflush(int fd)
   if (size < BUFFER_SIZE) return;
   uflush_r(fd, size);
 }
+
 void xml_tree::flush(int fd)
 {
   uflush_r(fd, print_buffer->size());
@@ -425,12 +423,14 @@ void xml_tree::uflush_r(int fd, size_t s)
   size_t written;
   while (1) {
     written = write(fd, print_buffer->data(), s);
-    if ((written < 0) && (errno == EAGAIN || errno == EINTR))
+    if ((written < 0) && (errno == EAGAIN || errno == EINTR)){
       continue;
+    };
     break;
   };
   print_buffer->clear();
 }
+
 void xml_tree::uput_str(std::string s, int fd)
 {
   print_buffer->append(s);
@@ -496,14 +496,16 @@ size_t xml_tree::uprintf(const char*s, int fd)
      return i;
 }
 
-void xml_tree::print(int fd, xml_tree::node_t x, bool no_text)
+void xml_tree::print(xml_tree::node_t x, int fd, bool no_text)
 {
+
   if (print_buffer == 0) {
     print_buffer = new std::string(BUFFER_SIZE, 0);
     print_buffer->clear();
     print_stack = new std::vector<std::string>();
     print_stack->reserve(256);
   };
+
   xml_tree::node_t fin = bp_find_close(par, x);
   xml_tree::node_t n = x;
   xml_tree::tag_t label = tag(n);
@@ -582,7 +584,7 @@ void xml_tree::print(int fd, xml_tree::node_t x, bool no_text)
       uputc('>', fd);
       print_stack->pop_back();
       n++;
-      } while (!bp_inspect(par, n) || print_stack->empty());
+      } while (!bp_inspect(par, n) && !print_stack->empty());
     label = tag(n);
   };
   uputc('\n', fd);