FreeLing  4.0
prefTree.h
Go to the documentation of this file.
00001 
00002 //
00003 //    FreeLing - Open Source Language Analyzers
00004 //
00005 //    Copyright (C) 2014   TALP Research Center
00006 //                         Universitat Politecnica de Catalunya
00007 //
00008 //    This library is free software; you can redistribute it and/or
00009 //    modify it under the terms of the GNU Affero General Public
00010 //    License as published by the Free Software Foundation; either
00011 //    version 3 of the License, or (at your option) any later version.
00012 //
00013 //    This library is distributed in the hope that it will be useful,
00014 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //    Affero General Public License for more details.
00017 //
00018 //    You should have received a copy of the GNU Affero General Public
00019 //    License along with this library; if not, write to the Free Software
00020 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021 //
00022 //    contact: Lluis Padro (padro@lsi.upc.es)
00023 //             TALP Research Center
00024 //             despatx C6.212 - Campus Nord UPC
00025 //             08034 Barcelona.  SPAIN
00026 //
00028 
00030 //
00031 //   Author: Stanilovsky Evgeny 
00032 //
00034 
00035 #ifndef _PREF_TREE_H_
00036 #define _PREF_TREE_H_
00037 
00038 #include <string>
00039 #include <vector>
00040 
00041 namespace freeling {
00042 
00044   // Class List is auxiliary to PrefTree
00046 
00047   class List {
00048   public:
00049     struct ListRecBase  {
00050     ListRecBase(): next(NULL), nextList(NULL) {}
00051     ListRecBase(wchar_t c): symb(c), next(NULL), nextList(NULL) {}
00052       virtual ~ListRecBase() {}
00053       wchar_t symb;
00054       ListRecBase *next;
00055       List *nextList;
00056     };
00057   
00058     struct ListRec: public ListRecBase {
00059     ListRec(): ListRecBase() {}
00060     ListRec(wchar_t c): ListRecBase(c) {}
00061       virtual ~ListRec() {};
00062     };
00063     
00064     struct ListRecEnd: public ListRecBase {
00065     ListRecEnd(): ListRecBase() {}
00066     ListRecEnd(wchar_t c): ListRecBase(c) {}
00067     };
00068     
00069     struct ListRecData: public ListRecBase {
00070     ListRecData(): ListRecBase(), value(NULL) {}
00071     ListRecData(wchar_t c): ListRecBase(c), value(NULL) {}
00072       ~ListRecData() {delete[] value;}
00073       const wchar_t *value;
00074       void setValue(const wchar_t *data);
00075       void delValue() {delete[] value; value = NULL;};      
00076       const wchar_t *getValue()
00077       {
00078         return value;
00079       }
00080     };
00081     
00082   List(): begin(0), end(0) {}
00083     inline ListRecBase* find(const wchar_t c);
00084     void* push(const wchar_t c, const bool wordEnd = false);
00085     virtual ~List();
00086 
00087   private:
00088     ListRecBase *begin, *end;
00089   };
00090 
00092   // Class PrefTree implements a prefix Tree index
00094 
00095   class PrefTree {
00096   private:
00097     List *root;
00098     size_t DELIM_LEN;
00099 
00100   public:
00101     PrefTree() { root = new List(); DELIM_LEN = wcslen(L" ");}
00102     ~PrefTree() { delete root; }
00103     void addWord(const wchar_t *word, List::ListRecBase *lr, const wchar_t *data);
00104     List::ListRecBase *findWord(const wchar_t *) const;
00105     List::ListRecData *find(const wchar_t *word) const;
00106     void deleteWord(const wchar_t *);
00107   };
00108 
00109 } // namespace
00110 
00111 #endif