Moved tests to utils. Added .gitignore
[vspell.git] / libvspell / wordnode.h
blob877fd78cde7e8c3d0cb1227f7d250cc1b40876b0
1 #ifndef __WORDNODE_H__ // -*- tab-width: 2 mode: c++ -*-
2 #define __WORDNODE_H__
4 #ifndef __TYPES_H__
5 #include "types.h"
6 #endif
8 #ifndef __DICTIONARY_H__
9 #include "dictionary.h"
10 #endif
11 #ifndef __VECTOR__
12 #include <vector>
13 #endif
15 #ifndef __MAP__
16 #include <map>
17 #endif
19 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
20 #include <boost/shared_ptr.hpp>
21 #endif
23 #ifndef __MYSTRING_H__
24 #include "mystring.h"
25 #endif
27 class NNode;
28 class BranchNNode;
29 class LeafNNode;
30 typedef boost::shared_ptr<NNode> NNodeRef;
32 class NNode
34 public:
35 virtual bool is_leaf() const = 0;
36 virtual ~NNode() {}
39 class BranchNNode:public NNode
41 public:
42 typedef std::map<strid,NNodeRef> node_map;
43 typedef std::pair<node_map::const_iterator,node_map::const_iterator> const_np_range;
44 typedef std::pair<node_map::iterator,node_map::iterator> np_range;
45 protected:
46 node_map nodes;
47 public:
48 virtual ~BranchNNode() {}
49 bool is_leaf() const { return false; }
50 LeafNNode* get_leaf(strid leaf) const;
51 void get_leaves(std::vector<LeafNNode*> &nodes) const;
52 void get_branches(strid,std::vector<BranchNNode*> &nodes) const;
53 BranchNNode* get_branch(strid) const;
54 const node_map& get_nodes() const { return nodes; }
56 BranchNNode* add_path(std::vector<strid> toks);
57 void add(strid,NNodeRef);
60 class LeafNNode:public NNode
62 protected:
63 strid id; // word id
64 std::vector<strid> syllables;
65 uint bitmask;
66 public:
67 LeafNNode():bitmask(0) {}
68 virtual ~LeafNNode() {}
69 bool is_leaf() const { return true; }
70 strid get_id() const { return id; }
71 void set_id(const std::vector<strid> &sy);
73 uint get_mask() const { return bitmask; }
74 void set_mask(uint maskval,bool mask = true);
75 bool filter(uint mask) const { return (bitmask & mask) == mask; }
77 uint get_syllable_count() const { return syllables.size(); }
78 void get_syllables(std::vector<strid> &_syllables) const { _syllables = syllables; }
82 template<class T>
83 struct DNNode {
84 T* node;
85 int distance;
86 DNNode(T* _node = NULL):node(_node),distance(0) {}
87 int operator == (const DNNode &dn1) const {
88 return dn1.node == node;
90 int operator < (const DNNode &dn1) const {
91 return (int)node+distance < (int)dn1.node+dn1.distance;
93 T& operator* () const { return *node; }
94 T* operator-> () const { return node; }
97 class WordArchive
99 protected:
100 NNodeRef root;
101 void add_entry(std::vector<std::string> toks);
102 void add_case_entry(std::vector<std::string> toks);
103 std::vector<strid> leaf_id;
105 public:
106 WordArchive();
107 BranchNNode* get_root() { return (BranchNNode*)root.get(); }
108 bool load(const char *filename);
109 LeafNNode* add_special_entry(strid);
110 void register_leaf(strid leaf);
111 const std::vector<strid>& get_leaf_id() const { return leaf_id; }
113 LeafNNode* get_special_node(int);
115 extern WordArchive warch;
117 template<class T>
118 std::ostream& operator << (std::ostream &os,const DNNode<T> &node)
120 os << *node.node;
121 return os;
124 std::ostream& operator << (std::ostream &os,const LeafNNode &node);
126 #endif