terrible bug in PenaltyDAG and Penalty2DAG.
[vspell.git] / libvspell / wordnode.h
blobe7460eae49f4546ba5b6604bc597e0e731a41a2f
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 Node;
28 class BranchNode;
29 class LeafNode;
30 typedef boost::shared_ptr<Node> NodeRef;
32 class Node
34 public:
35 virtual bool is_leaf() const = 0;
36 virtual ~Node() {}
39 class BranchNode:public Node
41 public:
42 typedef std::map<strid,NodeRef> 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 ~BranchNode() {}
49 bool is_leaf() const { return false; }
50 LeafNode* get_leaf(strid leaf) const;
51 void get_leaves(std::vector<LeafNode*> &nodes) const;
52 void get_branches(strid,std::vector<BranchNode*> &nodes) const;
53 BranchNode* get_branch(strid) const;
54 const node_map& get_nodes() const { return nodes; }
56 BranchNode* add_path(std::vector<strid> toks);
57 void add(strid,NodeRef);
60 class LeafNode:public Node
62 protected:
63 strid id; // word id
64 std::vector<strid> syllables;
65 uint bitmask;
66 public:
67 LeafNode():bitmask(0) {}
68 virtual ~LeafNode() {}
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 DNode {
84 T* node;
85 int distance;
86 DNode(T* _node = NULL):node(_node),distance(0) {}
87 int operator == (const DNode &dn1) const {
88 return dn1.node == node;
90 int operator < (const DNode &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 NodeRef 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 BranchNode* get_root() { return (BranchNode*)root.get(); }
108 bool load(const char *filename);
109 LeafNode* add_special_entry(strid);
110 void register_leaf(strid leaf);
111 const std::vector<strid>& get_leaf_id() const { return leaf_id; }
113 LeafNode* get_special_node(int);
115 extern WordArchive warch;
117 template<class T>
118 std::ostream& operator << (std::ostream &os,const DNode<T> &node)
120 os << *node.node;
121 return os;
124 std::ostream& operator << (std::ostream &os,const LeafNode &node);
126 #endif