summaryrefslogtreecommitdiff
path: root/inputmethods
authorerik <erik>2007-01-19 01:12:38 (UTC)
committer erik <erik>2007-01-19 01:12:38 (UTC)
commit1ab92f1d2b346de7da8ca5c3aaa6bc75b43981e7 (patch) (unidiff)
treeaf4a12bc46e25853386dc53868b869e1bf05d863 /inputmethods
parent2b45dc71e79a3eb7d4e8553273c9bc4f4282d50a (diff)
downloadopie-1ab92f1d2b346de7da8ca5c3aaa6bc75b43981e7.zip
opie-1ab92f1d2b346de7da8ca5c3aaa6bc75b43981e7.tar.gz
opie-1ab92f1d2b346de7da8ca5c3aaa6bc75b43981e7.tar.bz2
Every single file in this commit had a memory leak where a resource is
allocated in the constructor but not de-allocated in the destructor. This commit fixes that.
Diffstat (limited to 'inputmethods') (more/less context) (ignore whitespace changes)
-rw-r--r--inputmethods/dasher/PPMLanguageModel.cpp3
1 files changed, 2 insertions, 1 deletions
diff --git a/inputmethods/dasher/PPMLanguageModel.cpp b/inputmethods/dasher/PPMLanguageModel.cpp
index 137b07f..d767d16 100644
--- a/inputmethods/dasher/PPMLanguageModel.cpp
+++ b/inputmethods/dasher/PPMLanguageModel.cpp
@@ -1,166 +1,167 @@
1// PPMLanguageModel.h 1// PPMLanguageModel.h
2// 2//
3///////////////////////////////////////////////////////////////////////////// 3/////////////////////////////////////////////////////////////////////////////
4// 4//
5// Copyright (c) 1999-2002 David Ward 5// Copyright (c) 1999-2002 David Ward
6// 6//
7///////////////////////////////////////////////////////////////////////////// 7/////////////////////////////////////////////////////////////////////////////
8 8
9#include <math.h> 9#include <math.h>
10#include "PPMLanguageModel.h" 10#include "PPMLanguageModel.h"
11 11
12using namespace Dasher; 12using namespace Dasher;
13using namespace std; 13using namespace std;
14 14
15// static TCHAR debug[256]; 15// static TCHAR debug[256];
16typedef unsigned long ulong; 16typedef unsigned long ulong;
17 17
18//////////////////////////////////////////////////////////////////////// 18////////////////////////////////////////////////////////////////////////
19/// PPMnode definitions 19/// PPMnode definitions
20//////////////////////////////////////////////////////////////////////// 20////////////////////////////////////////////////////////////////////////
21 21
22CPPMLanguageModel::CPPMnode *CPPMLanguageModel::CPPMnode::find_symbol(int sym) 22CPPMLanguageModel::CPPMnode *CPPMLanguageModel::CPPMnode::find_symbol(int sym)
23// see if symbol is a child of node 23// see if symbol is a child of node
24{ 24{
25 // printf("finding symbol %d at node %d\n",sym,node->id); 25 // printf("finding symbol %d at node %d\n",sym,node->id);
26 CPPMnode *found=child; 26 CPPMnode *found=child;
27 while (found) { 27 while (found) {
28 if (found->symbol==sym) 28 if (found->symbol==sym)
29 return found; 29 return found;
30 found=found->next; 30 found=found->next;
31 } 31 }
32 return 0; 32 return 0;
33} 33}
34 34
35 35
36CPPMLanguageModel::CPPMnode * CPPMLanguageModel::CPPMnode::add_symbol_to_node(int sym,int *update) 36CPPMLanguageModel::CPPMnode * CPPMLanguageModel::CPPMnode::add_symbol_to_node(int sym,int *update)
37{ 37{
38 CPPMnode *born,*search; 38 CPPMnode *born,*search;
39 search=find_symbol(sym); 39 search=find_symbol(sym);
40 if (!search) { 40 if (!search) {
41 born = new CPPMnode(sym); 41 born = new CPPMnode(sym);
42 born->next=child; 42 born->next=child;
43 child=born; 43 child=born;
44 // node->count=1; 44 // node->count=1;
45 return born; 45 return born;
46 } else { 46 } else {
47 if (*update) { // perform update exclusions 47 if (*update) { // perform update exclusions
48 search->count++; 48 search->count++;
49 *update=0; 49 *update=0;
50 } 50 }
51 return search; 51 return search;
52 } 52 }
53 53
54} 54}
55 55
56 56
57///////////////////////////////////////////////////////////////////// 57/////////////////////////////////////////////////////////////////////
58// CPPMLanguageModel defs 58// CPPMLanguageModel defs
59///////////////////////////////////////////////////////////////////// 59/////////////////////////////////////////////////////////////////////
60 60
61CPPMLanguageModel::CPPMLanguageModel(CAlphabet *_alphabet,int _normalization) 61CPPMLanguageModel::CPPMLanguageModel(CAlphabet *_alphabet,int _normalization)
62 : CLanguageModel(_alphabet,_normalization) 62 : CLanguageModel(_alphabet,_normalization), root(0), m_rootcontext(0)
63{ 63{
64 root=new CPPMnode(-1); 64 root=new CPPMnode(-1);
65 m_rootcontext=new CPPMContext(root,0); 65 m_rootcontext=new CPPMContext(root,0);
66} 66}
67 67
68 68
69CPPMLanguageModel::~CPPMLanguageModel() 69CPPMLanguageModel::~CPPMLanguageModel()
70{ 70{
71 delete m_rootcontext;
71 delete root; 72 delete root;
72} 73}
73 74
74 75
75bool CPPMLanguageModel::GetProbs(CContext *context,vector<unsigned int> &probs,double ) 76bool CPPMLanguageModel::GetProbs(CContext *context,vector<unsigned int> &probs,double )
76 // get the probability distribution at the context 77 // get the probability distribution at the context
77{ 78{
78 // seems like we have to have this hack for VC++ 79 // seems like we have to have this hack for VC++
79 CPPMContext *ppmcontext=static_cast<CPPMContext *> (context); 80 CPPMContext *ppmcontext=static_cast<CPPMContext *> (context);
80 81
81 82
82 int modelchars=GetNumberModelChars(); 83 int modelchars=GetNumberModelChars();
83 int norm=CLanguageModel::normalization(); 84 int norm=CLanguageModel::normalization();
84 probs.resize(modelchars); 85 probs.resize(modelchars);
85 CPPMnode *temp,*s; 86 CPPMnode *temp,*s;
86 int loop,total; 87 int loop,total;
87 int sym; 88 int sym;
88 // ulong spent=0; 89 // ulong spent=0;
89 ulong size_of_slice; 90 ulong size_of_slice;
90 bool *exclusions=new bool [modelchars]; 91 bool *exclusions=new bool [modelchars];
91 ulong uniform=modelchars; 92 ulong uniform=modelchars;
92 ulong tospend=norm-uniform; 93 ulong tospend=norm-uniform;
93 temp=ppmcontext->head; 94 temp=ppmcontext->head;
94 for (loop=0; loop <modelchars; loop++) { /* set up the exclusions array */ 95 for (loop=0; loop <modelchars; loop++) { /* set up the exclusions array */
95 probs[loop]=0; 96 probs[loop]=0;
96 exclusions[loop]=0; 97 exclusions[loop]=0;
97 } 98 }
98 while (temp!=0) { 99 while (temp!=0) {
99 //Usprintf(debug,TEXT("tospend %u\n"),tospend); 100 //Usprintf(debug,TEXT("tospend %u\n"),tospend);
100 //DebugOutput(TEXT("round\n")); 101 //DebugOutput(TEXT("round\n"));
101 total=0; 102 total=0;
102 s=temp->child; 103 s=temp->child;
103 while (s) { 104 while (s) {
104 sym=s->symbol; 105 sym=s->symbol;
105 if (!exclusions[s->symbol]) 106 if (!exclusions[s->symbol])
106 total=total+s->count; 107 total=total+s->count;
107 s=s->next; 108 s=s->next;
108 } 109 }
109 if (total) { 110 if (total) {
110 //Usprintf(debug,TEXT"escape %u\n"),tospend* 111 //Usprintf(debug,TEXT"escape %u\n"),tospend*
111 size_of_slice=tospend; 112 size_of_slice=tospend;
112 s=temp->child; 113 s=temp->child;
113 while (s) { 114 while (s) {
114 if (!exclusions[s->symbol]) { 115 if (!exclusions[s->symbol]) {
115 exclusions[s->symbol]=1; 116 exclusions[s->symbol]=1;
116 ulong p=size_of_slice*(2*s->count-1)/2/ulong(total); 117 ulong p=size_of_slice*(2*s->count-1)/2/ulong(total);
117 probs[s->symbol]+=p; 118 probs[s->symbol]+=p;
118 tospend-=p; 119 tospend-=p;
119 } 120 }
120 // Usprintf(debug,TEXT("sym %u counts %d p %u tospend %u \n"),sym,s->count,p,tospend); 121 // Usprintf(debug,TEXT("sym %u counts %d p %u tospend %u \n"),sym,s->count,p,tospend);
121 // DebugOutput(debug); 122 // DebugOutput(debug);
122 s=s->next; 123 s=s->next;
123 } 124 }
124 } 125 }
125 temp = temp->vine; 126 temp = temp->vine;
126 } 127 }
127 //Usprintf(debug,TEXT("Norm %u tospend %u\n"),Norm,tospend); 128 //Usprintf(debug,TEXT("Norm %u tospend %u\n"),Norm,tospend);
128 //DebugOutput(debug); 129 //DebugOutput(debug);
129 130
130 size_of_slice=tospend; 131 size_of_slice=tospend;
131 int symbolsleft=0; 132 int symbolsleft=0;
132 for (sym=1;sym<modelchars;sym++) 133 for (sym=1;sym<modelchars;sym++)
133 if (!probs[sym]) 134 if (!probs[sym])
134 symbolsleft++; 135 symbolsleft++;
135 for (sym=1;sym<modelchars;sym++) 136 for (sym=1;sym<modelchars;sym++)
136 if (!probs[sym]) { 137 if (!probs[sym]) {
137 ulong p=size_of_slice/symbolsleft; 138 ulong p=size_of_slice/symbolsleft;
138 probs[sym]+=p; 139 probs[sym]+=p;
139 tospend-=p; 140 tospend-=p;
140 } 141 }
141 142
142 // distribute what's left evenly 143 // distribute what's left evenly
143 tospend+=uniform; 144 tospend+=uniform;
144 for (sym=1;sym<modelchars;sym++) { 145 for (sym=1;sym<modelchars;sym++) {
145 ulong p=tospend/(modelchars-sym); 146 ulong p=tospend/(modelchars-sym);
146 probs[sym]+=p; 147 probs[sym]+=p;
147 tospend-=p; 148 tospend-=p;
148 } 149 }
149 //Usprintf(debug,TEXT("finaltospend %u\n"),tospend); 150 //Usprintf(debug,TEXT("finaltospend %u\n"),tospend);
150 //DebugOutput(debug); 151 //DebugOutput(debug);
151 152
152 // free(exclusions); // !!! 153 // free(exclusions); // !!!
153 // !!! NB by IAM: p577 Stroustrup 3rd Edition: "Allocating an object using new and deleting it using free() is asking for trouble" 154 // !!! NB by IAM: p577 Stroustrup 3rd Edition: "Allocating an object using new and deleting it using free() is asking for trouble"
154 delete[] exclusions; 155 delete[] exclusions;
155 return true; 156 return true;
156} 157}
157 158
158 159
159void CPPMLanguageModel::AddSymbol(CPPMLanguageModel::CPPMContext &context,int symbol) 160void CPPMLanguageModel::AddSymbol(CPPMLanguageModel::CPPMContext &context,int symbol)
160 // add symbol to the context 161 // add symbol to the context
161 // creates new nodes, updates counts 162 // creates new nodes, updates counts
162 // and leaves 'context' at the new context 163 // and leaves 'context' at the new context
163{ 164{
164 // sanity check 165 // sanity check
165 if (symbol==0 || symbol>=GetNumberModelChars()) 166 if (symbol==0 || symbol>=GetNumberModelChars())
166 return; 167 return;