31 #define obstack_chunk_alloc malloc
32 #define obstack_chunk_free free
70 if ((child->
right - child->
left) <= (xRight - xLeft)) {
81 if (xLeft <= xRight) {
83 *uRight = child->
left + xRight - xLeft;
110 if (xLeft > xRight) {
122 for (i=child->
left; (i<=child->
right) && (xLeft<=xRight) && (
text[i]==
text[xLeft]); i++, xLeft++);
123 if (i > child->
right) {
125 if (xLeft > xRight) {
133 *uLeft = child->
left;
162 if (uLeft > uRight) {
182 new->children[
GETINDEX(uRight+1)]->length =
new->length + uRight - uLeft + 1;
183 new->children[
GETINDEX(uRight+1)]->parent =
new;
190 if (vLeft <= vRight) {
193 newLeaf->left = vLeft;
194 newLeaf->right = vRight;
195 newLeaf->length =
new->length +
new->right -
new->left + 1;
196 newLeaf->parent =
new;
197 new->children[
GETINDEX(vLeft)] = newLeaf;
199 newLeaf->origin =
new->origin;
217 Uint xLeft, uLeft, uRight, vLeft, vRight, i, cidx;
228 canonize(root, xLeft, node->
right, &r, &uLeft, &uRight, &vLeft, &vRight);
231 if ((uLeft <= uRight) || (vLeft <= vRight)) {
232 x =
insert(r, uLeft, uRight, vLeft, vRight);
233 if (uLeft <= uRight) {
290 while ((totalN & 0xFFFFC000) != 0) {
328 if (tree->
left + offset == tree->
right) {
337 if (stop)
return True;
341 if (stop)
return True;
345 if (stop)
return True;
352 if (stop)
return True;
356 if (stop)
return True;
360 if (stop)
return True;
379 if (tree->
left + offset == tree->
right) {
395 int i, posDest, posOrig;
398 posDest = dest->
left + offsetDest;
399 posOrig = orig->
left + offsetOrig;
401 while (posOrig <= orig->
right && posDest <= dest->
right && !end) {
402 if (text2[posOrig] ==
text[posDest]) {
412 if (posDest > dest->
right) {
420 if (posOrig > orig->
right) {
448 static void printRec(
const fsmTree_t tree,
int level) {
452 for (i=1; i<=level; i++) {
458 printf(
"%ld - %ld (%p) parent: %p orig: %p\n", tree->
left, tree->
right, (
void*)tree, (
void*)tree->
parent, (
void*)tree->
origin);
459 for (i=tree->
left; i< tree->
right; i++) {
460 printf(
"%d-", *(
text + i));
476 printf (
"hijo %d ", i);
491 memset(ret, 0,
sizeof(
struct fsmTree));
495 if (obstack_chunk_size (&(ret->
nodeStack)) < 16384) {
496 obstack_chunk_size (&(ret->
nodeStack)) = 16384;
561 transitions[i] = tree;
573 Uint total,
internal;
593 printf(
"Representantion cost: %ld (internal: %ld, total: %ld)\n",
612 if (h > max) max = h;
616 return max + tree->
right - tree->
left + 1;
623 for (i=tree->
left; i< tree->
right; i++) {
624 printf(
"%c", *(
text + i));
638 int i, minLevel, actLevel;
640 if ((treeA == NULL && treeB != NULL) || (treeA != NULL && treeB == NULL)) {
643 else if (treeA == NULL && treeB == NULL) {
648 for (i=1; (i<
alphasize) && (minLevel != level+1); i++) {
650 if ((actLevel > 0) && (minLevel > actLevel)) {
661 printf(
"First different level = %d\n", level);
686 void printFsmTree(
const fsmTree_t tree) {
static void verify(const fsmTree_t root, fsmTree_t node, BOOL fast)
Verifies that the tail of root is in the tree, adding it if necessary and continuing recursively on t...
void addSymbol(fsmTree_t tree, const Uchar sym)
Adds a new symbol to this tree node.
fsmTree_t initFsmTree()
Creates and initializes a new fsm tree structure instance.
static fsmTree_t insert(fsmTree_t r, Uint uLeft, Uint uRight, Uint vLeft, Uint vRight)
Inserts new nodes in the FSM closure of the tree.
static void canonize(fsmTree_t tree, Uint xLeft, Uint xRight, fsmTree_t *r, Uint *uLeft, Uint *uRight, Uint *vLeft, Uint *vRight)
Calculates the canonical decomposition of a string.
void printContext(fsmTree_t tree)
Uint textlen
Length of the input text in the encoder.
void encode_symbol(FILE *stream, SYMBOL *s)
struct fsmTree ** transitions
List of FSM transitions from this state.
BOOL isRootFsmTree(const fsmTree_t tree)
Indicates if the parameter node is the root of the tree.
Uint getHeight(const fsmTree_t tree)
#define True
True boolean constant.
Uint alphasize
Alphabet size.
static void fastCanonize(fsmTree_t tree, Uint xLeft, Uint xRight, fsmTree_t *r, Uint *uLeft, Uint *uRight, Uint *vLeft, Uint *vRight)
Calculates the canonical decomposition of a string in a faster way.
unsigned char Uchar
Unsigned char type.
Uint right
Index of the rightmost character of the label of this node.
Uchar * text
Input text to the encoder.
BOOL used
Flag that indicates if this node has been used to encode a symbol.
BOOL * traversed
List of flags indicating an attempt was made to traverse each child edge.
static BOOL writeEncoder(BOOL internal, FILE *file)
Auxiliary function to write a tree node to a file using an arithmetic encoder.
#define FREE(P)
Frees a block of memory and makes the pointer NULL.
struct fsmTree * origin
Pointer to the original node this one descends from.
#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.
Uint * count
List containing the number of occurrences of each character in this state.
unsigned short int low_count
Encoder context tree structure.
static void copyStatisticsRec(const fsmTree_t orig, const int offsetOrig, fsmTree_t dest, const int offsetDest, const Uchar *text2)
void writeFsmTree(const fsmTree_t tree, FILE *file)
Writes this tree into a file.
static short pos
Current position in the buffer.
static BOOL writeFsmTreeRec(const fsmTree_t tree, Uint offset, FILE *file)
Auxiliary recursive function to write a tree to a file using an arithemtic encoder.
struct obstack nodeStack
Obstack used to allocate memory for this tree.
Uint right
Index of the rightmost character of this node label in the input string.
struct decoderTree ** transitions
List of FSM transitions from this state.
#define False
False boolean constant.
Uint left
Index of the leftmost character of this node label in the input string.
Uint totalSyms
Total number of symbols occuring at this state.
void makeFsm(fsmTree_t tree)
Calculates the FSM closure of this tree.
struct fsmTree ** children
List of pointers to all the children of this node.
unsigned short int high_count
Uint * count
Counts of the number of occurrences of each symbol in this state.
static Ushort getInternalNodeCount(const fsmTree_t tree, const Uint offset)
Counts the number of internal nodes in a full tree.
void copyStatistics(const fsmTree_t orig, fsmTree_t dest, const Uchar *text2)
#define GETINDEX(N)
Returns the index of the Nth character in the input text.
struct fsmTree * tail
Pointer to the node whose label is the tail of this one.
struct fsmTree * parent
Pointer to the parent of this node.
void freeFsmTree(fsmTree_t tree)
Deletes a fsm tree structure instance.
static Ushort internalNodes
Number of internal nodes in the tree.
Uint length
Distance from this node to the root of the tree.
static Uint totalNodes
Total number of nodes in the tree.
static int compareTreesRec(const fsmTree_t treeA, const fsmTree_t treeB, int level)
long bit_ftell_output(FILE *stream)
#define ROOT
Flag to indicate a node is the root.
unsigned short Ushort
Unsigned short type.
static void propagateTransitions(fsmTree_t *transitions, fsmTree_t tree)
Adds to the FSM a set of transitions originating from tree if they were not defined by verify...
void compareTrees(const fsmTree_t treeA, const fsmTree_t treeB)