33 #define obstack_chunk_alloc malloc
34 #define obstack_chunk_free free
68 xLeft += tree->
right + 1;
74 if ((child->
right - child->
left) <= (xRight - xLeft)) {
85 if (xLeft <= xRight) {
114 if (xLeft > xRight) {
120 xLeft += tree->
right + 1;
127 for (i=child->
left; (i<=child->right) && (xLeft<=xRight) && ((*child->
text)[i] == (*node->
text)[xLeft]); i++, xLeft++);
128 if (i > child->
right) {
130 if (xLeft > xRight) {
171 DEBUGCODE(printf(
"New node1: %p\n", (
void *)
new));
172 if (uLeft > uRight) {
176 if (r->
internal && vRight > vLeft) {
178 DEBUGCODE(printf(
"New node3: %p\n", (
void *)newLeaf));
179 newLeaf->left = newLeaf->right = (r->
right !=
ROOT ? r->
right + 1 : 0);
180 newLeaf->internal =
False;
181 newLeaf->internalFSM = !decoder;
184 newLeaf->text = r->
text;
185 REALLOC(*newLeaf->text, *newLeaf->text,
Uchar, newLeaf->right + 1);
190 if (newLeaf->left > 0) {
191 memcpy(*newLeaf->text, *r->
text, newLeaf->left);
194 (*newLeaf->text)[newLeaf->left] = (*node->
text)[vLeft];
197 newLeaf->
origin = newLeaf;
212 new->right =
new->left + vRight - vLeft;
213 new->internal =
False;
214 new->internalFSM = !decoder;
225 memcpy(*new->text, *r->
text, new->left);
228 for (i=vLeft, j=new->left; i<=vRight; i++, j++) {
229 (*
new->text)[j] = (*node->
text)[i];
254 new->right =
new->left + uRight - uLeft;
258 new->internalFSM = !decoder || child->internalFSM;
260 new->children[
alphaindex[(*child->text)[new->right+1]]] = child;
261 child->left = new->right + 1;
263 new->text = child->text;
283 if (i < new->totalSyms) {
284 new->
count[i] = (child->count[i] == 0 ? 0 : 1);
285 new->totalCount +=
new->count[i];
286 new->symbols[i] = child->symbols[i];
289 new->totalCount *= 2;
294 if (vLeft <= vRight) {
295 if (new->internal && vRight > vLeft) {
297 DEBUGCODE(printf(
"New node6: %p\n", (
void *)newLeaf));
298 newLeaf->left = newLeaf->right = (
new->right !=
ROOT ?
new->right + 1 : 0);
299 newLeaf->internal =
False;
300 newLeaf->internalFSM = !decoder;
304 if (newLeaf->left > 0) {
305 memcpy(*newLeaf->text, *new->text, newLeaf->left);
308 (*newLeaf->text)[newLeaf->left] = (*node->
text)[vLeft];
309 new->children[
GETINDEX2(vLeft)] = newLeaf;
310 newLeaf->parent =
new;
311 newLeaf->origin = newLeaf;
325 DEBUGCODE(printf(
"New node2: %p\n", (
void *)newLeaf));
327 newLeaf->internal =
False;
328 newLeaf->internalFSM = !decoder;
329 newLeaf->left =
new->right + 1;
330 newLeaf->right = newLeaf->left + vRight - vLeft;
331 newLeaf->parent =
new;
332 new->children[
GETINDEX2(vLeft)] = newLeaf;
336 memcpy(*newLeaf->text, *new->text, newLeaf->left);
337 for (i=vLeft, j=newLeaf->left; i<=vRight; i++, j++) {
338 (*newLeaf->text)[j] = (*node->
text)[i];
342 newLeaf->origin = newLeaf;
345 newLeaf->origin =
new->origin;
351 newLeaf->transitions[i] =
new->transitions[i];
375 Uint xLeft, uLeft, uRight, vLeft, vRight, i, cidx;
383 fastCanonize(root, xLeft, node->
right, node, &r, &uLeft, &uRight, &vLeft, &vRight);
386 canonize(root, xLeft, node->
right, node, &r, &uLeft, &uRight, &vLeft, &vRight);
389 if ((uLeft <= uRight) || (vLeft <= vRight)) {
390 x =
insert(r, node, uLeft, uRight, vLeft, vRight, decoder);
391 if (uLeft <= uRight) {
460 while ((totalN & 0xFFFFC000) != 0) {
472 internal = count < internalN;
498 int onlyChild = -1 , childStatus;
505 if (onlyChild == -1) {
508 else if (onlyChild >= 0) {
525 if (onlyChild >= 0) {
537 if (childStatus >= 0) {
559 for (i=1; i<=level; i++) {
563 printf(
"%ld - %ld (%p) parent: %p orig: %p\n", tree->
left, tree->
right, (
void*)tree, (
void*)tree->
parent, (
void*)tree->
origin);
564 for (i=0; i<tree->
right; i++) {
565 printf(
"%d-", *(*(tree->
text) + i));
567 printf(
"%d\n", *(*(tree->
text) + tree->
right));
576 printf (
"hijo %d ", i);
587 if (obstack_chunk_size (&
nodeStack) < 16384) {
672 transitions[i] = tree;
struct decoderTree ** children
List of pointers to all the children of this node.
struct decoderTree * decoderTree_t
Decoder context tree structure.
Uchar ** text
< Flag that indicates if this node is an internal node of the FSM closure of T(x) ...
void freeDecoderTree(decoderTree_t tree, BOOL deleteText)
Deletes a decoder tree structure instance.
BOOL * traversed
List of flags indicating an attempt was made to traverse each child edge.
static void verify(const decoderTree_t root, decoderTree_t node, BOOL fast, BOOL decoder)
Verifies that the tail of node is in the tree, adding it if necessary and continuing recursively on t...
struct decoderTree * tail
Pointer to the node whose label is the tail of this one.
struct decoderTree * parent
Pointer to the parent of this node.
#define True
True boolean constant.
Uint alphasize
Alphabet size.
unsigned char Uchar
Unsigned char type.
Uint right
Index of the rightmost character of the label of this node.
BOOL internalFSM
< Flag that indicates if this node is an internal node of T(x)
static Uint totalNodes
Total number of nodes in the tree.
#define ROOT
Flag to indicate a node is the root.
struct decoderTree * origin
Pointer to the original node this one descends from.
Uint left
Index of the leftmost character of the label of this node.
BOOL isRootDecoderTree(decoderTree_t tree)
Indicates if the parameter node is the root of the tree.
#define FREE(P)
Frees a block of memory and makes the pointer NULL.
#define CALLOC(S, T, N)
Allocs a new block of memory and sets all its bits to zero.
unsigned long Uint
Unsigned int type.
#define BOOL
Boolean data type.
unsigned short int low_count
void verifyDecoder(const decoderTree_t root, decoderTree_t node)
Verify*, only called by the decoder routine.
BOOL used
Flag that indicates if this node has been used to decode eny symbols.
static void canonize(decoderTree_t tree, Uint xLeft, Uint xRight, decoderTree_t node, decoderTree_t *r, Uint *uLeft, Uint *uRight, Uint *vLeft, Uint *vRight)
Calculates the canonical decomposition of a string.
void makeDecoderFsm(decoderTree_t tree)
Calculates the FSM closure of this tree.
Uchar characters[UCHAR_MAX+1]
Characters in text in alphabetical order.
struct decoderTree ** transitions
List of FSM transitions from this state.
#define False
False boolean constant.
void initDecoderTreeStack()
Uint alphaindex[UCHAR_MAX+1]
Index of each alphabet character.
#define GETINDEX2(N)
Returns the index of the Nth character in the input text.
decoderTree_t initDecoderTree(BOOL useMalloc)
Creates and initializes a new decoder tree structure instance.
#define REALLOC(V, S, T, N)
Resizes a block of memory and prints an error message if necessary.
unsigned short int high_count
Uint * count
Counts of the number of occurrences of each symbol in this state.
static decoderTree_t insert(decoderTree_t r, decoderTree_t node, Uint uLeft, Uint uRight, Uint vLeft, Uint vRight, BOOL decoder)
Inserts a new node in the FSM closure of the tree.
static struct obstack nodeStack
static void propagateTransitions(decoderTree_t *transitions, decoderTree_t tree)
Adds to the FSM a set of transitions originating from tree if thew were not defined by verify...
void remove_symbol_from_stream(FILE *stream, SYMBOL *s)
Decoder context tree structure.
short int get_current_count(SYMBOL *s)
static Ushort internalNodes
Number of internal nodes in the tree.
static int readDecoderTreeRec(decoderTree_t t, FILE *file)
Reads a decoder tree from its encoded representation on a file.
unsigned short Ushort
Unsigned short type.
decoderTree_t readDecoderTree(FILE *file)
Creates a new decoder tree reading it from a file.
static BOOL readEncoder(FILE *file)
Reads a bit from the file using an arithmetic decoder.
static void fastCanonize(decoderTree_t tree, Uint xLeft, Uint xRight, decoderTree_t node, decoderTree_t *r, Uint *uLeft, Uint *uRight, Uint *vLeft, Uint *vRight)
Calculates the canonical decomposition of a string in a faster way.