home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src-glu / tess_hash.c < prev    next >
C/C++ Source or Header  |  1999-12-06  |  6KB  |  272 lines

  1. /* $Id: tess_hash.c,v 1.6.2.3 1999/12/05 02:04:31 gareth Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  *
  7.  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
  8.  *
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  *
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  *
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27. /*****************************************************************************
  28.  *
  29.  * GLU 1.3 Polygon Tessellation - Implementation of hash table
  30.  *
  31.  * Gareth Hughes <garethh@bell-labs.com>, August 1999
  32.  *
  33.  *****************************************************************************/
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <math.h>
  38.  
  39. #include "gluP.h"
  40.  
  41. #include "tess.h"
  42. #include "tess_hash.h"
  43.  
  44.  
  45.  
  46. /*****************************************************************************
  47.  *
  48.  *                HASHTABLE FUNCTIONS
  49.  *
  50.  *****************************************************************************/
  51.  
  52.  
  53. /*****************************************************************************
  54.  * hashtable_init
  55.  *
  56.  * Allocate and initialize a new heap.
  57.  *****************************************************************************/
  58. hashtable_t *hashtable_init( GLint size )
  59. {
  60.     hashtable_t    *table;
  61.     GLint    i;
  62.  
  63.     table = ( hashtable_t * )
  64.     malloc( size * sizeof(hashtable_t) );
  65.     if ( table == NULL ) {
  66.     return NULL;
  67.     }
  68.  
  69.     table->elements = ( hashtable_elt_t ** )
  70.     malloc( size * sizeof(hashtable_elt_t *) );
  71.     if ( table->elements == NULL ) {
  72.     free( table );
  73.     return NULL;
  74.     }
  75.  
  76.     for ( i = 0; i < size; i++ ) {
  77.     table->elements[ i ] = NULL;
  78.     }
  79.  
  80.     table->size = size;
  81.     table->num_elements = 0;
  82.  
  83.     return table;
  84. }
  85.  
  86.  
  87. /*****************************************************************************
  88.  * hashtable_insert
  89.  *****************************************************************************/
  90. void hashtable_insert( hashtable_t *table, GLint key, void *ptr )
  91. {
  92.     hashtable_elt_t    *elt = (hashtable_elt_t *)
  93.     malloc( sizeof(hashtable_elt_t) );
  94.     GLint        index = key % table->size;
  95.  
  96.     elt->key = key;
  97.     elt->ptr = ptr;
  98.  
  99.     if ( ! table->elements[ index ] )
  100.     {
  101.     table->elements[ index ] = elt;
  102.  
  103.     elt->next = elt->prev = NULL;
  104.     }
  105.     else
  106.     {
  107.     elt->next = table->elements[ index ];
  108.     elt->next->prev = elt;
  109.  
  110.     elt->prev = NULL;
  111.  
  112.     table->elements[ index ] = elt;
  113.     }
  114.  
  115.     table->num_elements++;
  116. }
  117.  
  118.  
  119. /*****************************************************************************
  120.  * hashtable_search
  121.  *****************************************************************************/
  122. GLboolean hashtable_search( hashtable_t *table, GLint key )
  123. {
  124.     hashtable_elt_t    *elt;
  125.     GLint        index = key % table->size;
  126.  
  127.     if ( table->elements[ index ] )
  128.     {
  129.     elt = table->elements[ index ];
  130.  
  131.     while ( elt )
  132.     {
  133.         if ( elt->key == key ) {
  134.         return GL_TRUE;
  135.         }
  136.  
  137.         elt = elt->next;
  138.     }
  139.     }
  140.  
  141.     return GL_FALSE;
  142. }
  143.  
  144.  
  145. /*****************************************************************************
  146.  * hashtable_element
  147.  *****************************************************************************/
  148. void *hashtable_element( hashtable_t *table, GLint key )
  149. {
  150.     hashtable_elt_t    *elt;
  151.     GLint        index = key % table->size;
  152.  
  153.     if ( table->elements[ index ] )
  154.     {
  155.     elt = table->elements[ index ];
  156.  
  157.     while ( elt )
  158.     {
  159.         if ( elt->key == key ) {
  160.         return elt->ptr;
  161.         }
  162.  
  163.         elt = elt->next;
  164.     }
  165.     }
  166.  
  167.     return NULL;
  168. }
  169.  
  170.  
  171. /*****************************************************************************
  172.  * hashtable_delete
  173.  *****************************************************************************/
  174. void *hashtable_delete( hashtable_t *table, GLint key )
  175. {
  176.     hashtable_elt_t    *elt;
  177.     void        *ret = NULL;
  178.     GLint        index = key % table->size;
  179.  
  180.     if ( table->elements[ index ] )
  181.     {
  182.     elt = table->elements[ index ];
  183.  
  184.     while ( elt )
  185.     {
  186.         if ( elt->key == key )
  187.         {
  188.         if ( elt == table->elements[ index ] )
  189.         {
  190.             /* Remove the entry from the head of the chain. */
  191.             if ( elt->next )
  192.             {
  193.             table->elements[ index ] = elt->next;
  194.             elt->next->prev = NULL;
  195.             }
  196.             else
  197.             {
  198.             table->elements[ index ] = NULL;
  199.             }
  200.         }
  201.         else
  202.         {
  203.             if ( elt->next )
  204.             {
  205.             /* Remove the entry from inside the chain. */
  206.             elt->next->prev = elt->prev;
  207.             elt->prev->next = elt->next;
  208.             }
  209.             else
  210.             {
  211.             /* Remove the entry from the end of the chain. */
  212.             elt->prev->next = NULL;
  213.             }
  214.         }
  215.  
  216.         ret = elt->ptr;
  217.         free( elt );
  218.  
  219.         break;
  220.         }
  221.  
  222.         elt = elt->next;
  223.     }
  224.     }
  225.  
  226.     if ( ret ) {
  227.     table->num_elements--;
  228.     }
  229.     return ret;
  230. }
  231.  
  232.  
  233. /*****************************************************************************
  234.  * hashtable_cleanup
  235.  *****************************************************************************/
  236. void hashtable_cleanup( hashtable_t **table )
  237. {
  238.     if ( *table )
  239.     {
  240.     if ( (*table)->elements )
  241.     {
  242.         free( (*table)->elements );
  243.     }
  244.  
  245.     free( *table );
  246.     *table = NULL;
  247.     }
  248.  
  249.     if ( *table )
  250.     {
  251.     GLint        i;
  252.  
  253.     for ( i = 0; i < (*table)->size; i++ )
  254.     {
  255.         hashtable_elt_t    *elt = (*table)->elements[ i ];
  256.  
  257.         while ( elt )
  258.         {
  259.         hashtable_elt_t    *next;
  260.  
  261.         next = elt->next;
  262.         free( elt );
  263.         elt = next;
  264.         }
  265.     }
  266.  
  267.     free( (*table)->elements );
  268.     free( *table );
  269.     *table = NULL;
  270.     }
  271. }
  272.