Better naming, some inlining.
[SXSI/XMLTree.git] / XMLTree.h
index ea9b938..a3ac2ed 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
@@ -1,4 +1,3 @@
-\r
 /******************************************************************************\r
  *   Copyright (C) 2008 by Diego Arroyuelo                                    *\r
  *   Interface for the in-memory XQuery/XPath engine                          *\r
@@ -21,6 +20,8 @@
 \r
 #ifndef XMLTREE_H_\r
 #define XMLTREE_H_\r
+#include <unordered_set>\r
+#include <unordered_map>\r
 #include "TextCollection/TextCollectionBuilder.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
@@ -32,7 +33,7 @@
 #undef Wminusone\r
 \r
 #include "bp.h"\r
-//#include "basics.h"\r
+#include <libcds/includes/basics.h>\r
 #include <static_bitsequence.h>\r
 #include <alphabet_mapper.h>\r
 #include <static_sequence.h>\r
@@ -57,27 +58,58 @@ typedef struct {
    int max;\r
 } range;\r
 \r
-typedef struct nd {\r
-   uint position;\r
-   struct nd *next;\r
-} ListNode;\r
+// Encoding of the XML Document :\r
+// The following TAGs and IDs are fixed, "" is the tag of the root.\r
+// a TextNode is represented by a leaf <<$>></<$>> The DocId in the TextCollection\r
+// of that leaf is kept in a bit sequence.\r
+// a TextNode below an attribute is likewise represented by a leaf <<@$>><</@$>>\r
+// An element <e a1="v1" a2="v2" ... an="vn" > ...</e> the representation is:\r
+// <e><<@>> <<@>a1> <<$@>>DocID(v1)</<$@>></<@>a1> ... </<@>> .... </e>\r
+// Hence the attributes (if any) are always below the first child of their element,\r
+// as the children of a fake node <@>.\r
+\r
+\r
+#define DOCUMENT_OPEN_TAG ""\r
+#define DOCUMENT_TAG_ID 0\r
+#define ATTRIBUTE_OPEN_TAG "<@>"\r
+#define ATTRIBUTE_TAG_ID 1\r
+#define PCDATA_OPEN_TAG "<$>"\r
+#define PCDATA_TAG_ID 2\r
+#define ATTRIBUTE_DATA_OPEN_TAG "<@$>"\r
+#define ATTRIBUTE_DATA_TAG_ID 3\r
+#define DOCUMENT_CLOSE_TAG "/"\r
+#define ATTRIBUTE_CLOSE_TAG "/<@>"\r
+#define PCDATA_CLOSE_TAG "/<$>"\r
+#define ATTRIBUTE_DATA_CLOSE_TAG "/<@$>"\r
+\r
+\r
+typedef std::unordered_set<int> TagIdSet;\r
+typedef std::unordered_map<string,int> TagIdMap;\r
+typedef TagIdMap::const_iterator TagIdMapIT;\r
+\r
+#define REGISTER_TAG(v,h,t) do { (h)->insert(std::make_pair((t),(v)->size()));\\r
+    (v)->push_back(t); } while (false)\r
+\r
+\r
+// returns NULLT if the test is true\r
+#define NULLT_IF(x)  do { if (x) return NULLT; } while (0)\r
 \r
-typedef struct {\r
-   ListNode *first;\r
-   ListNode *last;\r
-} TagArrayEntry;\r
+\r
+class XMLTreeBuilder;\r
 \r
 class XMLTree {\r
+\r
+  // Only the builder can access the constructor\r
+  friend class XMLTreeBuilder;\r
+\r
+ private:\r
    /** Balanced parentheses representation of the tree */\r
    bp *Par;\r
  \r
    /** Mapping from tag identifer to tag name */  \r
-   unsigned char **TagName;\r
-   uint ntagnames;\r
+   vector<string> *TagName;\r
+   TagIdMap * tIdMap;\r
   \r
-   /** boolean flag indicating whether we are indexing empty texts or not */\r
-   bool indexing_empty_texts; \r
-   \r
    /** Bit vector indicating with a 1 the positions of the non-empty texts. */\r
    static_bitsequence *EBVector;  \r
                      \r
@@ -89,55 +121,66 @@ class XMLTree {
    /** The texts in the XML document */\r
    TextCollection *Text;\r
 \r
-   /** The texts in the XML document (cached for faster display) */\r
-   vector<string> CachedText;\r
-   \r
-   TagArrayEntry *TagArray;\r
-\r
    // Allows to disable the TextCollection for benchmarkin purposes\r
    bool disable_tc;\r
    \r
-public:\r
+   FILE* stream;\r
+   int   stream_fd;\r
+\r
    /** Data structure constructors */\r
-   XMLTree() {;};\r
+   XMLTree(){;};\r
 \r
-   XMLTree(pb *par, uint npar, unsigned char **TN, uint ntagnames, uint *empty_texts_bmp, TagType *tags,\r
-           TextCollection *TC, vector<string> CT, bool indexing_empty_t, bool dis_tc);\r
\r
+   // non const pointer are freed by this method.\r
+  XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,\r
+          TextCollection * const TC, bool dis_tc);\r
+\r
+public: \r
    /** Data structure destructor */\r
    ~XMLTree();\r
    \r
    /** root(): returns the tree root. */\r
    treeNode Root();\r
-   \r
+\r
+   /** Size() :  Number of parenthesis */\r
+   unsigned int Size(){\r
+     return tags_len/2;\r
+   }\r
+\r
    /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of \r
     * node x. */\r
    int SubtreeSize(treeNode x);\r
-   \r
+  \r
    /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree \r
     * of node x. */\r
    int SubtreeTags(treeNode x, TagType tag);\r
    \r
+   /** SubtreeElements(x) of element nodes in the subtree of x\r
+    */\r
+   int SubtreeElements(treeNode x);\r
+\r
    /** IsLeaf(x): returns whether node x is leaf or not. In the succinct \r
     * representation this is just a bit inspection. */\r
+\r
    bool IsLeaf(treeNode x);\r
-    \r
+\r
    /** IsAncestor(x,y): returns whether node x is ancestor of node y. */\r
+\r
    bool IsAncestor(treeNode x, treeNode y);\r
   \r
    /** IsChild(x,y): returns whether node x is parent of node y. */\r
    bool IsChild(treeNode x, treeNode y);\r
 \r
    /** IsFirstChild(x): returns whether node x is the first child of its parent. */\r
+   /* OCAML */\r
    bool IsFirstChild(treeNode x);\r
-\r
+     \r
    /** NumChildren(x): number of children of node x. Constant time with the \r
     * data structure of Sadakane. */\r
    int NumChildren(treeNode x);\r
-   \r
+\r
    /** ChildNumber(x): returns i if node x is the i-th children of its \r
     * parent. */\r
-   inline int ChildNumber(treeNode x);\r
+   int ChildNumber(treeNode x);\r
 \r
    /** Depth(x): depth of node x, a simple binary rank on the parentheses \r
     * sequence. */\r
@@ -150,33 +193,51 @@ public:
    /** Postorder(x): returns the postorder number of node x, just regarding \r
     * tree nodes (and not texts). */\r
    int Postorder(treeNode x);\r
-   \r
+      \r
    /** Tag(x): returns the tag identifier of node x. */\r
-   TagType Tag(treeNode x);\r
-   \r
+   TagType Tag(treeNode x) {\r
+     if (tags_blen == 8)\r
+       return  (TagType) (((uchar*)tags_fix)[(int) x]);\r
+     else\r
+       return (TagType) get_field(tags_fix,tags_blen, (int) x);\r
+   }\r
+\r
    /** DocIds(x): returns the range (i.e., a pair of integers) of document \r
     * identifiers that descend from node x. */\r
    range DocIds(treeNode x);\r
-   \r
+\r
    /** Parent(x): returns the parent node of node x. */\r
    treeNode Parent(treeNode x);\r
+   /* Assumes x is neither 0 nor -1 */\r
    \r
    /** Child(x,i): returns the i-th child of node x, assuming it exists. */   \r
    treeNode Child(treeNode x, int i);\r
-   \r
-   /** FirstChild(x): returns the first child of node x, assuming it exists. \r
-    * Very fast in BP. */\r
+\r
+   /** FirstChild(x): returns the first child of node x, or NULLT if the node is a leaf\r
+    */\r
    treeNode FirstChild(treeNode x);\r
-   \r
+\r
+   /** FirstElement(x): returns the first non text, non attribute child of node x, or NULLT\r
+    *    if none.\r
+    */\r
+   treeNode FirstElement(treeNode x);\r
+\r
    /** LastChild(x): returns the last child of node x.  */\r
    treeNode LastChild(treeNode x);\r
    \r
-   /** NextSibling(x): returns the next sibling of node x, assuming it \r
+   /** NextSibling(x): returns the next sibling of node x, or NULLT if none \r
     * exists. */\r
+\r
    treeNode NextSibling(treeNode x);\r
+\r
+   /** NextElement(x): returns the first non text, non attribute sibling of node x, or NULLT\r
+    *    if none.\r
+    */\r
+   treeNode NextElement(treeNode x);\r
    \r
    /** PrevSibling(x): returns the previous sibling of node x, assuming it \r
     * exists. */\r
+\r
    treeNode PrevSibling(treeNode x);\r
    \r
    /** TaggedChild(x,tag): returns the first child of node x tagged tag, or \r
@@ -185,52 +246,38 @@ public:
     * among the children of node x until finding the desired child. */\r
    treeNode TaggedChild(treeNode x, TagType tag);\r
    \r
-   treeNode SelectChild(treeNode x, TagType *tags, int ntags);\r
+   treeNode SelectChild(treeNode x, TagIdSet * tags);\r
 \r
-   /** TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or \r
+   /** TaggedFollowingSibling(x,tag): returns the first sibling of node x tagged tag, or \r
     *  NULLT if there is none. */\r
-   treeNode TaggedSibling(treeNode x, TagType tag);\r
+   treeNode TaggedFollowingSibling(treeNode x, TagType tag);\r
    \r
-   treeNode SelectSibling(treeNode x, TagType *tags, int ntags);\r
+   treeNode SelectFollowingSibling(treeNode x, TagIdSet * tags);\r
 \r
    /** TaggedDesc(x,tag): returns the first node tagged tag with larger \r
     * preorder than x and within the subtree of x. Returns NULT if there \r
     * is none. */\r
-   treeNode TaggedDesc(treeNode x, TagType tag);\r
-\r
-   treeNode SelectDesc(treeNode x, TagType *tags, int ntags);\r
-\r
-   treeNode TaggedBelow(treeNode x, TagType *childtags, unsigned int ctlen,\r
-                                TagType *desctags, unsigned int dtlen);\r
-   \r
-   treeNode TaggedNext(treeNode x, TagType *childtags, unsigned int ctlen,\r
-                               TagType *folltags, unsigned int flen,treeNode root);\r
+   treeNode TaggedDescendant(treeNode x, TagType tag);\r
 \r
-   treeNode TaggedDescOnly(treeNode x, TagType *desctags, unsigned int dtlen);\r
-   \r
-   treeNode TaggedDescOrFollOnly(treeNode x, TagType *folltags, unsigned int flen,\r
-                          treeNode root);\r
-\r
-   treeNode TaggedFollOnly(treeNode x, TagType *folltags, unsigned int flen,\r
-                          treeNode root);\r
-   \r
+   treeNode SelectDescendant(treeNode x, TagIdSet * tags);\r
 \r
    /** TaggedPrec(x,tag): returns the first node tagged tag with smaller \r
     * preorder than x and not an ancestor of x. Returns NULLT if there \r
     * is none. */\r
-   treeNode TaggedPrec(treeNode x, TagType tag);\r
+   treeNode TaggedPreceding(treeNode x, TagType tag);\r
   \r
    /** TaggedFoll(x,tag): returns the first node tagged tag with larger \r
     * preorder than x and not in the subtree of x. Returns NULLT if there \r
     * is none. */\r
-   treeNode TaggedFoll(treeNode x, TagType tag);\r
+   treeNode TaggedFollowing(treeNode x, TagType tag);\r
 \r
-   treeNode TaggedFollBelow(treeNode x, TagType tag,treeNode root);     \r
-   \r
-   treeNode SelectFollBelow(treeNode x, TagType *tags, int ntags, treeNode ctx);\r
+   treeNode TaggedFollowingBelow(treeNode x, TagType tag,treeNode ancestor);     \r
 \r
-   /** TaggedFollowingSibling(x,tag) */\r
-   treeNode TaggedFollowingSibling(treeNode x, TagType tag);\r
+   treeNode SelectFollowingBelow(treeNode x, TagIdSet * tags, treeNode ancestor);\r
+\r
+   treeNode TaggedFollowingBefore(treeNode x, TagType tag,treeNode closing);\r
+\r
+   treeNode SelectFollowingBefore(treeNode x, TagIdSet * tags, treeNode closing);\r
 \r
    /** TaggedAncestor(x, tag): returns the closest ancestor of x tagged \r
      * tag. Return NULLT is there is none. */\r
@@ -247,7 +294,8 @@ public:
    /** MyText(x): returns the document identifier of the text below node x, or \r
     * NULLT if x is not a leaf node. */\r
    DocID MyText(treeNode x);\r
-   \r
+   DocID MyTextUnsafe(treeNode x);\r
+\r
    /** TextXMLId(d): returns the preorder of document with identifier d in the \r
     * tree consisting of all tree nodes and all text nodes. */\r
    int TextXMLId(DocID d);\r
@@ -297,7 +345,7 @@ public:
    }\r
 \r
    /** Equal(s): search for texts equal to string s. */\r
-   TextCollection::document_result Equal(uchar const *s) {\r
+   TextCollection::document_result Equals(uchar const *s) {\r
       return Text->Equal(s);\r
    }\r
 \r
@@ -373,29 +421,43 @@ public:
    /** GetText(d): returns the text corresponding to document with\r
     * id d. */\r
    uchar* GetText(DocID d) {\r
-       return Text->GetText(d);\r
+     \r
+       uchar * s = Text->GetText(d);\r
+       return (s[0] == 1 ? (uchar*)"" : s);\r
    }\r
 \r
-   uchar* GetCachedText(DocID d) {\r
-     uchar * str = (uchar*) calloc(sizeof(char),(CachedText.at(d).size() + 1));\r
-     strcpy((char*) str,(const char*) CachedText.at(d).c_str());\r
-     return (uchar*) (str);\r
-   }\r
-   \r
+   /** GetText(i, j): returns the texts corresponding to documents with\r
+    * ids i, i+1, ..., j. Texts are separated by '\0' character.  */\r
+   //   uchar* GetText(DocID i, DocID j) {\r
+   //  uchar * s = Text->GetText(i, j);\r
+   // return (s[0] == 1 ? (uchar*)"" : s);\r
+   //}\r
+\r
    TextCollection *getTextCollection() {\r
       return Text;\r
    }\r
    \r
    /** Save: saves XML tree data structure to file. */\r
-   void Save(unsigned char *filename);\r
+   void Save(int fd);\r
       \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
+   static XMLTree *Load(int fd,bool load_tc, int sample_factor);   \r
 \r
    void insertTag(TagType tag, uint position);\r
    \r
    void print_stats();\r
+\r
+   \r
+   /** Parenthesis functions */\r
+   treeNode Closing(treeNode x);\r
+\r
+   bool IsOpen(treeNode x);\r
+\r
+\r
+   /** Print procedure */\r
+   void Print(int fd,treeNode x);\r
+\r
 };\r
 #endif\r
 \r