Few more things
[apertium.git] / lttoolbox / lttoolbox / Node.H
blob0a1a4608020db07000720bf61495187e136660bb
1 /*
2  * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
20 #ifndef _NODE_
21 #define _NODE_
23 #include <cstdlib>
24 #include <list>
25 #include <map>
27 class State;
28 class Node;
30 using namespace std;
32 class Dest
34 private:
35   int size;
36   ushort *out_tag;
37   Node **dest;
38   
39   friend class State;
40   friend class Node;
41   
42   void copy(Dest const &d)
43   {
44     if(size != 0)
45     {
46       if(!out_tag)
47       {
48         delete out_tag;
49       }
50       if(!dest)
51       {
52         delete dest;
53       }
54     }
55     size = d.size;
56     out_tag = new ushort[size];
57     dest = new Node*[size];
58   }
59   
60   void destroy()
61   {
62     if(size != 0)
63     {
64       size = 0;
65       if(!out_tag)
66       {
67         delete out_tag;
68       }
69       if(!dest)
70       {
71         delete dest;
72       }
73     }
74   }
75   
76 public:
77   Dest()
78   {
79     size = 0;
80     out_tag = NULL;
81     dest = NULL;
82   }
83   
84   ~Dest()
85   {
86     destroy();
87   }
88   
89   Dest(Dest const &d)
90   {
91     copy(d);
92   }
93   
94   Dest & operator=(Dest const &d)
95   {
96     if(this != &d)
97     {
98       destroy();
99       copy(d);
100     }
101     return *this; 
102   }
108  * Node class of TransExe.  State is a friend class since the
109  * algorithms are implemented in State
110  */
111 class Node
113 private:
114   friend class State;
116   /**
117    * The outgoing transitions of this node. 
118    * Schema: (input symbol, (output symbol, destination))
119    */
120   map<unsigned short, Dest> transitions;
122   /**
123    * Copy method
124    * @param n the node to be copied
125    */
126   void copy(Node const &n);
128   /**
129    * Destroy method
130    */
131   void destroy();
133 public:
135   /**
136    * Constructor
137    */
138   Node();
140   /**
141    * Destructor
142    */
143   ~Node();
145   /**
146    * Copy constructor 
147    * @param n the node to be copied
148    */
149   Node(Node const &n);
151   /**
152    * Assignment operator
153    * @param n the node to be assigned
154    * @return the assigned object
155    */
156   Node & operator=(Node const &n);
158   /**
159    * Making a link between this node and another
160    * @param i input symbol
161    * @param o output symbol
162    * @param d destination
163    */
164   void addTransition(unsigned short const i, unsigned short const o, Node * const d);
167 #endif