~ubuntu-branches/ubuntu/natty/luatex/natty

« back to all changes in this revision

Viewing changes to source/texk/web2c/luatexdir/utils/avlstuff.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2009-12-25 09:47:05 UTC
  • mfrom: (1.1.9 upstream) (4.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091225094705-y33rpflo8t4u9nag
Tags: 0.50.0-1
* new upstream release
* disable fix-hurd-ftbfs patch, included upstream
* disable upstram-fixes, included upstream
* disable ubuntu_libpoppler-0.11, not needed anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
20
20
 
21
21
#include "ptexlib.h"
22
 
#include <kpathsea/c-proto.h>
23
22
#include "avl.h"
24
23
 
25
24
static const char __svn_version[] =
26
 
    "$Id: avlstuff.c 2599 2009-06-25 10:35:26Z taco $ "
27
 
    "$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.40.6/source/texk/web2c/luatexdir/utils/avlstuff.c $";
28
 
 
29
 
static struct avl_table **PdfObjTree = NULL;
 
25
    "$Id: avlstuff.c 2817 2009-07-12 00:45:27Z oneiros $ "
 
26
    "$URL: http://foundry.supelec.fr/svn/luatex/tags/beta-0.50.0/source/texk/web2c/luatexdir/utils/avlstuff.c $";
30
27
 
31
28
/**********************************************************************/
32
29
/* memory management functions for AVL */
63
60
    (void) p;
64
61
    return strcmp((const char *) pa, (const char *) pb);
65
62
}
66
 
 
67
 
/**********************************************************************/
68
 
/* One AVL tree for each obj_type 0...pdf_objtype_max */
69
 
 
70
 
typedef struct oentry_ {
71
 
    integer int0;
72
 
    integer objptr;
73
 
} oentry;
74
 
 
75
 
/* AVL sort oentry into avl_table[] */
76
 
 
77
 
static int compare_info(const void *pa, const void *pb, void *param)
78
 
{
79
 
    integer a, b;
80
 
    int as, ae, bs, be, al, bl;
81
 
    (void) param;
82
 
    a = ((const oentry *) pa)->int0;
83
 
    b = ((const oentry *) pb)->int0;
84
 
    if (a < 0 && b < 0) {       /* string comparison */
85
 
        if (a <= 2097152 && b <= 2097152) {
86
 
            a += 2097152;
87
 
            b += 2097152;
88
 
            as = str_start[-a];
89
 
            ae = str_start[-a + 1];     /* start of next string in pool */
90
 
            bs = str_start[-b];
91
 
            be = str_start[-b + 1];
92
 
            al = ae - as;
93
 
            bl = be - bs;
94
 
            if (al < bl)        /* compare first by string length */
95
 
                return -1;
96
 
            if (al > bl)
97
 
                return 1;
98
 
            for (; as < ae; as++, bs++) {
99
 
                if (str_pool[as] < str_pool[bs])
100
 
                    return -1;
101
 
                if (str_pool[as] > str_pool[bs])
102
 
                    return 1;
103
 
            }
104
 
        } else {
105
 
            pdftex_fail
106
 
                ("avlstuff.c: compare_items() for single characters: NI");
107
 
        }
108
 
    } else {                    /* integer comparison */
109
 
        if (a < b)
110
 
            return -1;
111
 
        if (a > b)
112
 
            return 1;
113
 
    }
114
 
    return 0;
115
 
}
116
 
 
117
 
void avl_put_obj(integer objptr, integer t)
118
 
{
119
 
    static void **pp;
120
 
    static oentry *oe;
121
 
    int i;
122
 
 
123
 
    if (PdfObjTree == NULL) {
124
 
        PdfObjTree = xtalloc(pdf_objtype_max + 1, struct avl_table *);
125
 
        for (i = 0; i <= pdf_objtype_max; i++)
126
 
            PdfObjTree[i] = NULL;
127
 
    }
128
 
    if (PdfObjTree[t] == NULL) {
129
 
        PdfObjTree[t] = avl_create(compare_info, NULL, &avl_xallocator);
130
 
        if (PdfObjTree[t] == NULL)
131
 
            pdftex_fail("avlstuff.c: avl_create() PdfObjTree failed");
132
 
    }
133
 
    oe = xtalloc(1, oentry);
134
 
    oe->int0 = obj_info(objptr);
135
 
    oe->objptr = objptr;        /* allows to relocate obj_tab */
136
 
    pp = avl_probe(PdfObjTree[t], oe);
137
 
    if (pp == NULL)
138
 
        pdftex_fail("avlstuff.c: avl_probe() out of memory in insertion");
139
 
}
140
 
 
141
 
/* replacement for linear search pascal function "find_obj()" */
142
 
 
143
 
integer avl_find_obj(integer t, integer i, integer byname)
144
 
{
145
 
    static oentry *p;
146
 
    static oentry tmp;
147
 
 
148
 
    if (byname > 0)
149
 
        tmp.int0 = -i;
150
 
    else
151
 
        tmp.int0 = i;
152
 
    if (PdfObjTree == NULL || PdfObjTree[t] == NULL)
153
 
        return 0;
154
 
    p = (oentry *) avl_find(PdfObjTree[t], &tmp);
155
 
    if (p == NULL)
156
 
        return 0;
157
 
    return p->objptr;
158
 
}
159
 
 
160
 
/**********************************************************************/
161
 
/* cleaning up... */
162
 
 
163
 
static void destroy_oentry(void *pa, void *pb)
164
 
{
165
 
    oentry *p = (oentry *) pa;
166
 
    (void) pb;
167
 
    xfree(p);
168
 
}
169
 
 
170
 
void PdfObjTree_free()
171
 
{
172
 
    int i;
173
 
 
174
 
    if (PdfObjTree == NULL)
175
 
        return;
176
 
    for (i = 0; i <= pdf_objtype_max; i++) {
177
 
        if (PdfObjTree[i] != NULL)
178
 
            avl_destroy(PdfObjTree[i], destroy_oentry);
179
 
    }
180
 
    xfree(PdfObjTree);
181
 
    PdfObjTree = NULL;
182
 
}