Back to index

tetex-bin  3.0
avlstuff.c
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2004 Han The Thanh, <thanh@pdftex.org>
00003 
00004 This file is free software; you can redistribute it and/or modify it
00005 under the terms of the GNU General Public License as published by Free
00006 Software Foundation; either version 2 of the License, or (at your option)
00007 any later version.
00008 
00009 This file is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00012 for more details.
00013 
00014 You should have received a copy of the GNU General Public License along
00015 with this file; if not, write to the Free Software Foundation, Inc.,
00016 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00017 
00018 $Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/avlstuff.c#5 $
00019 
00020 formatted by indent -kr
00021 */
00022 
00023 #include "ptexlib.h"
00024 #include <kpathsea/c-vararg.h>
00025 #include <kpathsea/c-proto.h>
00026 #include "avl.h"
00027 
00028 static const char perforce_id[] =
00029     "$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/avlstuff.c#5 $";
00030 
00031 /* One AVL tree for each obj_type 0...pdfobjtypemax */
00032 
00033 static struct avl_table *PdfObjTree[pdfobjtypemax + 1] =
00034     { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
00035 
00036 
00037 /* memory management functions for avl */
00038 
00039 void *avl_xmalloc(struct libavl_allocator *allocator, size_t size)
00040 {
00041     assert(allocator != NULL && size > 0);
00042     return xmalloc(size);
00043 }
00044 
00045 void avl_xfree(struct libavl_allocator *allocator, void *block)
00046 {
00047     assert(allocator != NULL && block != NULL);
00048     xfree(block);
00049 }
00050 
00051 struct libavl_allocator avl_xallocator = {
00052     avl_xmalloc,
00053     avl_xfree
00054 };
00055 
00056 
00057 /* AVL sort objentry into avl_table[] */
00058 
00059 int compare_info(const void *pa, const void *pb, void *param)
00060 {
00061     integer a, b;
00062     int as, ae, bs, be, al, bl;
00063 
00064     a = ((const objentry *) pa)->int0;
00065     b = ((const objentry *) pb)->int0;
00066     if (a < 0 && b < 0) {        /* string comparison */
00067         as = strstart[-a];
00068         ae = strstart[-a + 1];        /* start of next string in pool */
00069         bs = strstart[-b];
00070         be = strstart[-b + 1];
00071         al = ae - as;
00072         bl = be - bs;
00073         if (al < bl)                /* compare first by string length */
00074             return -1;
00075         else if (al > bl)
00076             return 1;
00077         else
00078             for (; as < ae; as++, bs++) {
00079                 if (strpool[as] < strpool[bs])
00080                     return -1;
00081                 if (strpool[as] > strpool[bs])
00082                     return 1;
00083             }
00084         return 0;
00085     } else {                        /* integer comparison */
00086         if (a < b)
00087             return -1;
00088         else if (a > b)
00089             return 1;
00090         else
00091             return 0;
00092     }
00093 }
00094 
00095 
00096 void avlputobj(integer objptr, integer t)
00097 {
00098     static void **pp;
00099 
00100     if (PdfObjTree[t] == NULL) {
00101         PdfObjTree[t] = avl_create(compare_info, NULL, &avl_xallocator);
00102         if (PdfObjTree[t] == NULL)
00103             pdftex_fail("avlstuff.c: avl_create() PdfObjTree failed");
00104     }
00105     pp = avl_probe(PdfObjTree[t], &(objtab[objptr]));
00106     if (pp == NULL)
00107         pdftex_fail("avlstuff.c: avl_probe() out of memory in insertion");
00108 }
00109 
00110 
00111 /* replacement for linear search pascal function "find_obj" */
00112 
00113 integer avlfindobj(integer t, integer i, integer byname)
00114 {
00115     static objentry *p;
00116     static objentry tmp;
00117 
00118     if (byname > 0)
00119         tmp.int0 = -i;
00120     else
00121         tmp.int0 = i;
00122     if (PdfObjTree[t] == NULL)
00123         return 0;
00124     p = (objentry *) avl_find(PdfObjTree[t], &tmp);
00125     if (p == NULL)
00126         return 0;
00127     else
00128         return (int) (p - (objentry *) objtab);
00129 }
00130 
00131 /* end of file avlstuff.c */