seg fault in word_check()
[vspell.git] / libvspell / wordnode.h
blob4c6443b206864c300df0e63ed1a79d3bf45fdec4
1 #ifndef __WORDNODE_H__ // -*- tab-width: 2 mode: c++ -*-
2 #define __WORDNODE_H__
4 #ifndef __DICTIONARY_H__
5 #include "dictionary.h"
6 #endif
7 #ifndef __VECTOR__
8 #include <vector>
9 #endif
11 #ifndef __MAP__
12 #include <map>
13 #endif
15 #ifndef BOOST_SHARED_PTR_HPP_INCLUDED
16 #include <boost/shared_ptr.hpp>
17 #endif
19 #ifndef __MYSTRING_H__
20 #include "mystring.h"
21 #endif
23 class Node;
24 class BranchNode;
25 class LeafNode;
26 typedef boost::shared_ptr<Node> NodeRef;
28 class Node
30 public:
31 virtual bool is_leaf() const = 0;
32 virtual ~Node() {}
35 class BranchNode:public Node
37 public:
38 typedef std::map<strid,NodeRef> node_map;
39 typedef std::pair<node_map::const_iterator,node_map::const_iterator> const_np_range;
40 typedef std::pair<node_map::iterator,node_map::iterator> np_range;
41 protected:
42 node_map nodes;
43 public:
44 virtual ~BranchNode() {}
45 bool is_leaf() const { return false; }
46 LeafNode* get_leaf(strid leaf) const;
47 void get_leaves(std::vector<LeafNode*> &nodes) const;
48 void get_branches(strid,std::vector<BranchNode*> &nodes) const;
49 BranchNode* get_branch(strid) const;
50 const node_map& get_nodes() const { return nodes; }
52 BranchNode* add_path(std::vector<strid> toks);
53 void add(strid,NodeRef);
56 class LeafNode:public Node
58 protected:
59 strid id; // word id
60 std::vector<strid> syllables;
61 uint bitmask;
62 public:
63 LeafNode():bitmask(0) {}
64 virtual ~LeafNode() {}
65 bool is_leaf() const { return true; }
66 strid get_id() const { return id; }
67 void set_id(const std::vector<strid> &sy);
69 uint get_mask() const { return bitmask; }
70 void set_mask(uint maskval,bool mask = true);
71 bool filter(uint mask) const { return (bitmask & mask) == mask; }
73 uint get_syllable_count() const { return syllables.size(); }
74 void get_syllables(std::vector<strid> &_syllables) const { _syllables = syllables; }
78 template<class T>
79 struct DNode {
80 T* node;
81 int distance;
82 DNode(T* _node = NULL):node(_node),distance(0) {}
83 int operator == (const DNode &dn1) const {
84 return dn1.node == node;
86 int operator < (const DNode &dn1) const {
87 return (int)node+distance < (int)dn1.node+dn1.distance;
89 T& operator* () const { return *node; }
90 T* operator-> () const { return node; }
93 class WordArchive
95 protected:
96 NodeRef root;
97 void add_entry(std::vector<std::string> toks);
98 void add_case_entry(std::vector<std::string> toks);
99 std::vector<strid> leaf_id;
101 public:
102 WordArchive();
103 BranchNode* get_root() { return (BranchNode*)root.get(); }
104 bool load(const char *filename);
105 LeafNode* add_special_entry(strid);
106 void register_leaf(strid leaf);
107 const std::vector<strid>& get_leaf_id() const { return leaf_id; }
109 LeafNode* get_special_node(int);
111 extern WordArchive warch;
113 template<class T>
114 std::ostream& operator << (std::ostream &os,const DNode<T> &node)
116 os << *node.node;
117 return os;
120 std::ostream& operator << (std::ostream &os,const LeafNode &node);
122 #endif