Fix a bug in TaggedFoll
[SXSI/XMLTree.git] / XMLTree.h
index 8fc6596..7c08fcc 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *\r
  ******************************************************************************/ \r
 \r
+#ifndef XMLTREE_H_\r
+#define XMLTREE_H_\r
+#include "TextCollection/TextCollection.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
+#include <cstring>\r
+\r
+//KIM : OJO\r
+//clash between TextCollection/Tools.h and libcds/includes/basics.h\r
+#undef W\r
+#undef WW\r
+#undef Wminusone\r
+\r
 #include "bp.h"\r
-#include "libcds/includes/static_bitsequence.h"\r
-#include "libcds/includes/alphabet_mapper.h"\r
-#include "libcds/includes/static_sequence.h"\r
+#include <static_bitsequence.h>\r
+#include <alphabet_mapper.h>\r
+#include <static_sequence.h>\r
+using SXSI::TextCollection;\r
 \r
-//#include "TextCollection/TextCollection.h"\r
-//using SXSI::TextCollection;\r
 \r
 // this constant is used to efficiently compute the child operation in the tree\r
 #define OPTD 10\r
 \r
 #define NULLT -1\r
 \r
+#define PERM_SAMPLE 10\r
+\r
         // sets bit p in e\r
 #define bitset(e,p) ((e)[(p)/W] |= (1<<((p)%W)))\r
         // cleans bit p in e\r
@@ -50,6 +62,13 @@ typedef struct {
 } range;\r
 \r
 \r
+//KIM : OJO\r
+// I know this class implements the working draft that we have but the logics seem flawed here...\r
+// We should have two classes. One XMLTreeBuilder and one XMLTree.\r
+// XMLTreeBuilder would have OpenDocument, NewOpenTag,... and CloseDocument would return an XMLTree\r
+// XMLTree would have only an initialized structure. If find it really ugly to check (!finished) or (!initialized)\r
+// in every function (FirstChild....).\r
+\r
 class XMLTree {\r
    /** Balanced parentheses representation of the tree */\r
    bp *Par;\r
@@ -64,10 +83,10 @@ class XMLTree {
    static_bitsequence_rrr02 *EBVector;  \r
                      \r
    /** Tag sequence represented with a data structure for rank and select */\r
-   static_sequence_wvtree *Tags;\r
+   static_sequence *Tags;\r
 \r
    /** The texts in the XML document */\r
-   //TextCollection *Text;\r
+   TextCollection *Text;\r
    \r
    /** Flag indicating whether the whole data structure has been constructed or \r
     * not. If the value is true, you cannot add more texts, nodes, etc. */\r
@@ -82,8 +101,21 @@ class XMLTree {
    pb *par_aux;\r
    TagType *tags_aux;\r
    int npar;\r
+   int parArraySize;\r
    int ntagnames;\r
    unsigned int *empty_texts_aux;\r
+\r
+   // KIM : OJO\r
+   // I added those two. The TagName array should always contains two special tags\r
+   // <@> for attributes and <$> for PCDATA.\r
+   // <$> can never be in a document (since we handle the text differently)\r
+   // but <@> can be returned by the parser. This boolean is needed for the construction\r
+   // of the Tag bitmap to know if <@> must be taken into account or not\r
+   bool found_attributes;\r
+\r
+   // KIM : OJO\r
+   // Allows to disable the TextCollection for benchmarkin purposes\r
+   bool disable_tc;\r
    \r
 public:\r
 \r
@@ -203,14 +235,17 @@ public:
    /** ParentNode(d): returns the parent node of document identifier d. */\r
    treeNode ParentNode(DocID d);\r
 \r
-   /** OpenDocument(empty_texts,sample_rate_text): initilizes the construction\r
+   /** OpenDocument(empty_texts,sample_rate_text,dtc): initilizes the construction\r
     * of the data structure for the XML document. Parameter empty_texts \r
     * indicates whether we index empty texts in document or not. Parameter \r
     * sample_rate_text indicates the sampling rate for the text searching data\r
     * structures (small values get faster searching but a bigger space \r
-    * requirement). Returns a non-zero value upon success, NULLT in case of \r
+    * requirement). dtc disable the use of the TextCollection\r
+    * (i.e. everything is considered an empty text *)\r
+    * Returns a non-zero value upon success, NULLT in case of \r
     * error. */\r
-   int OpenDocument(bool empty_texts, int sample_rate_text);\r
+\r
+   int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc);\r
 \r
    /** CloseDocument(): finishes the construction of the data structure for \r
     * the XML document. Tree and tags are represented in the final form, \r
@@ -248,13 +283,118 @@ public:
     * Returns NULL in case that the tag identifier is not valid.*/\r
    unsigned char *GetTagName(TagType tagid);\r
 \r
-   /** saveXMLTree: saves XML tree data structure to file. Every component of \r
-    * the collection is stored in different files (same name, different file \r
-    * extensions). */\r
+\r
+   // OJO\r
+   /** GetTagName(tagid): returns the tag name of a given tag identifier.     \r
+    *  The result is just a reference and should not be freed by the caller.\r
+    */\r
+   const unsigned char *GetTagNameByRef(TagType tagid);\r
+\r
+   //OJO\r
+   /** RegisterTag adds a new tag to the tag collection this is needed\r
+    * if the query contains a tag which is not in the document, we need\r
+    * to give this new tag a fresh id and store it somewhere. A logical\r
+    * choice is here.\r
+    * We might want to use a hashtable instead of an array though.\r
+    */\r
+   TagType RegisterTag(unsigned char *tagname);\r
+\r
+   bool EmptyText(DocID i) {\r
+      return Text->EmptyText(i);\r
+   }\r
+   /** Prefix(s): search for texts prefixed by string s. */\r
+   TextCollection::document_result Prefix(uchar const *s) {\r
+      return Text->Prefix(s);\r
+   }\r
+\r
+   /** Suffix(s): search for texts having string s as a suffix. */\r
+   TextCollection::document_result Suffix(uchar const *s) {\r
+      return Text->Suffix(s);\r
+   }\r
+\r
+   /** Equal(s): search for texts equal to string s. */\r
+   TextCollection::document_result Equal(uchar const *s) {\r
+      return Text->Equal(s);\r
+   }\r
+\r
+   /** Contains(s): search for texts containing string s.  */\r
+   TextCollection::document_result Contains(uchar const *s) {\r
+      return Text->Contains(s);\r
+   }\r
+\r
+   /** LessThan(s): returns document identifiers for the texts that\r
+    * are lexicographically smaller than string s. */\r
+   TextCollection::document_result LessThan(uchar const *s) {\r
+      return Text->LessThan(s);\r
+   }\r
+   \r
+   /** IsPrefix(x): returns true if there is a text prefixed by string s. */\r
+   bool IsPrefix(uchar const *s) {\r
+      return Text->IsPrefix(s);\r
+   }          \r
+   \r
+   /** IsSuffix(s): returns true if there is a text having string s as a \r
+    * suffix.*/\r
+   bool IsSuffix(uchar const *s) {\r
+      return Text->IsSuffix(s);\r
+   }\r
+   \r
+   /** IsEqual(s): returns true if there is a text that equals given \r
+    * string s. */\r
+   bool IsEqual(uchar const *s) {\r
+      return Text->IsEqual(s);\r
+   }\r
+   \r
+   /** IsContains(s): returns true if there is a text containing string s. */\r
+   bool IsContains(uchar const *s) {\r
+      return Text->IsContains(s);\r
+   }\r
+   \r
+   /** IsLessThan(s): returns true if there is at least a text that is \r
+    * lexicographically smaller than string s. */\r
+   bool IsLessThan(uchar const *s) {\r
+      return Text->IsLessThan(s);\r
+   }\r
+\r
+   /** CountPrefix(s): counting version of Prefix(s). */\r
+   unsigned CountPrefix(uchar const *s) {\r
+      return Text->CountPrefix(s);\r
+   }\r
+   \r
+   /** CountSuffix(s): counting version of Suffix(s). */\r
+   unsigned CountSuffix(uchar const *s) {\r
+      return Text->CountSuffix(s);\r
+   }\r
+   \r
+   /** CountEqual(s): counting version of Equal(s). */\r
+   unsigned CountEqual(uchar const *s) {\r
+      return Text->CountEqual(s);\r
+   }\r
+   \r
+   /** CountContains(s): counting version of Contains(s). */\r
+   unsigned CountContains(uchar const *s) {\r
+      return Text->CountContains(s);\r
+   }\r
+   \r
+   /** CountLessThan(s): counting version of LessThan(s). */\r
+   unsigned CountLessThan(uchar const *s) {\r
+      return CountLessThan(s);\r
+   }\r
+   \r
+   /** GetText(d): returns the text corresponding to document with\r
+    * id d. */\r
+   uchar* GetText(DocID d) {\r
+      return Text->GetText(d);\r
+   }\r
+   \r
+   TextCollection *getTextCollection() {\r
+      return Text;\r
+   }\r
+   /** Save: saves XML tree data structure to file. */\r
    void Save(unsigned char *filename);\r
       \r
-   /** load: loads XML tree data structure from file. */\r
+   /** Load: loads XML tree data structure from file. sample_rate_text \r
+    * indicates the sample rate for the text search data structure. */\r
    static XMLTree *Load(unsigned char *filename, int sample_rate_text);   \r
 };\r
-\r
-\r
+#endif\r