Context algorithm
Semi-predictive context algorithm implementation
 All Data Structures Files Functions Variables Typedefs Macros Pages
see.c
Go to the documentation of this file.
1 /* Copyright 2013 Jorge Merlino
2 
3  This file is part of Context.
4 
5  Context is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Context is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Context. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <assert.h>
20 #include <math.h> /* for log */
21 #include "see.h"
22 #include "debug.h"
23 
24 static int get3ByteRepresentation (const Uint num, const Uint alphasize) {
25  if (num <= 0) return 0;
26  else if (num <= 1) return 1;
27  else if (num <= 2) return 2;
28  else if (num <= 4) return 3;
29  else if (num <= (alphasize >= 150 ? 8 : 6)) return 4;
30  else if (num <= 9) return 5;
31  else if (num <= (alphasize >= 150 ? 25 : 15)) return 6;
32  else return 7;
33 }
34 
44 int getSeeStateEncoder (fsmTree_t tree, Uint allCount, Uint pos, Uint numMasked, const Uchar * text, Uint alphasize) {
45  Uint state, syms;
46 
47  if (allCount >= (alphasize >= 150 ? 128 : 30)) {
48  return -1;
49  }
50 
51  state = get3ByteRepresentation(tree->totalCount - allCount, alphasize); /* escapes */
52 
53  state <<= 3;
54  state |= get3ByteRepresentation(allCount, alphasize);
55 
56  state <<= 3;
57  state |= get3ByteRepresentation(numMasked, alphasize);
58 
59  syms = tree->totalSyms;
60 
61  if (alphasize < 150) {
62  while (!isRootFsmTree(tree) && tree->totalSyms == syms) {
63  tree = tree->parent->origin;
64  }
65  state <<=2;
66  if (tree->totalSyms > 3) {
67  state |= 3;
68  }
69  else {
70  state |= tree->totalSyms;
71  }
72  }
73 
74  state <<=3;
75  if ((pos > 0) && (text[pos-1] > 0)) {
76  state |= (int)(log(text[pos-1])+0.5);
77  }
78 
79  return state;
80 }
81 
82 int getSeeStateDecoder (decoderTree_t tree, Uint allCount, Uint pos, Uint numMasked, const Uchar * text, Uint alphasize) {
83  Uint state, syms;
84 
85  if (allCount >= (alphasize >= 150 ? 128 : 30)) {
86  return -1;
87  }
88 
89  state = get3ByteRepresentation(tree->totalCount - allCount, alphasize);
90 
91  state <<= 3;
92  state |= get3ByteRepresentation(allCount, alphasize);
93 
94  state <<=3;
95  state |= get3ByteRepresentation(numMasked, alphasize);
96 
97  syms = tree->totalSyms;
98 
99  if (alphasize < 150) {
100  while (!isRootDecoderTree(tree) && tree->totalSyms == syms) {
101  tree = tree->parent->origin;
102  }
103  state <<=2;
104  if (tree->totalSyms > 3) {
105  state |= 3;
106  }
107  else {
108  state |= tree->totalSyms;
109  }
110  }
111 
112  state <<=3;
113  if ((pos > 0) && (text[pos-1] > 0)) {
114  state |= (int)(log(text[pos-1])+0.5);
115  }
116 
117  return state;
118 }
119 
120 void updateSee (Uint state, BOOL escape, Uint alphasize) {
121  if (escape) {
122  See[state][0] += 17;
123  See[state][1] += 18;
124  }
125  else {
126  if (See[state][0] >= (alphasize >= 100 ? 500 : 4000)) {
127  See[state][0] = (See[state][0] >> 1) + 1;
128  See[state][1] = (See[state][1] >> 1) + 1;
129  }
130  See[state][1] += (alphasize >= 100 ? 16 : 17);
131  }
132 
133  if (See[state][1] >= (alphasize >= 100 ? 800 : 8000)) {
134  See[state][0] = (See[state][0] >> 1) + 1;
135  See[state][1] = (See[state][1] >> 1) + 1;
136  }
137 }
138 
139 void initSee () {
140  int i=0;
141 
142  for (i=0; i< 1<<14; i++) {
143  See[i][0]=20;
144  See[i][1]=50;
145  }
146 }
147 
Uint totalCount
&lt; Total number of symbols occuring at this state.
Definition: decoderTree.h:26
BOOL isRootFsmTree(const fsmTree_t tree)
Indicates if the parameter node is the root of the tree.
Definition: fsmTree.c:602
struct decoderTree * parent
Pointer to the parent of this node.
Definition: decoderTree.h:32
Uint totalCount
Definition: fsmTree.h:29
Uint alphasize
Alphabet size.
Definition: alpha.h:25
unsigned char Uchar
Unsigned char type.
Definition: types.h:48
struct decoderTree * origin
Pointer to the original node this one descends from.
Definition: decoderTree.h:32
Uchar * text
Input text to the encoder.
Definition: text.h:26
BOOL isRootDecoderTree(decoderTree_t tree)
Indicates if the parameter node is the root of the tree.
Definition: decoderTree.c:717
Uint totalSyms
Definition: decoderTree.h:26
struct fsmTree * origin
Pointer to the original node this one descends from.
Definition: fsmTree.h:39
unsigned long Uint
Unsigned int type.
Definition: types.h:54
#define BOOL
Boolean data type.
Definition: types.h:92
Encoder context tree structure.
Definition: fsmTree.h:28
static short pos
Current position in the buffer.
Definition: fsmTree.c:35
static int get3ByteRepresentation(const Uint num, const Uint alphasize)
Definition: see.c:24
int getSeeStateEncoder(fsmTree_t tree, Uint allCount, Uint pos, Uint numMasked, const Uchar *text, Uint alphasize)
Returns the contents of the SEE table for the encoder.
Definition: see.c:44
Uint totalSyms
Total number of symbols occuring at this state.
Definition: fsmTree.h:29
Uint See[1<< 14][2]
SEE table structure.
Definition: see.h:27
void initSee()
Initalizes the SEE table.
Definition: see.c:139
struct fsmTree * parent
Pointer to the parent of this node.
Definition: fsmTree.h:39
void updateSee(Uint state, BOOL escape, Uint alphasize)
Updates the SEE table.
Definition: see.c:120
Decoder context tree structure.
Definition: decoderTree.h:25
int getSeeStateDecoder(decoderTree_t tree, Uint allCount, Uint pos, Uint numMasked, const Uchar *text, Uint alphasize)
Returns the contents of the SEE table for the encoder.
Definition: see.c:82