Update to SGI FreeB 2.0.
[mesa.git] / src / glu / sgi / libtess / dict.c
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30 /*
31 ** Author: Eric Veach, July 1994.
32 **
33 ** $Date: 2006/04/19 14:42:01 $ $Revision: 1.3 $
34 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libtess/dict.c,v 1.3 2006/04/19 14:42:01 brianp Exp $
35 */
36
37 #include <stddef.h>
38 #include "dict-list.h"
39 #include "memalloc.h"
40
41 /* really __gl_dictListNewDict */
42 Dict *dictNewDict( void *frame,
43 int (*leq)(void *frame, DictKey key1, DictKey key2) )
44 {
45 Dict *dict = (Dict *) memAlloc( sizeof( Dict ));
46 DictNode *head;
47
48 if (dict == NULL) return NULL;
49
50 head = &dict->head;
51
52 head->key = NULL;
53 head->next = head;
54 head->prev = head;
55
56 dict->frame = frame;
57 dict->leq = leq;
58
59 return dict;
60 }
61
62 /* really __gl_dictListDeleteDict */
63 void dictDeleteDict( Dict *dict )
64 {
65 DictNode *node, *next;
66
67 for( node = dict->head.next; node != &dict->head; node = next ) {
68 next = node->next;
69 memFree( node );
70 }
71 memFree( dict );
72 }
73
74 /* really __gl_dictListInsertBefore */
75 DictNode *dictInsertBefore( Dict *dict, DictNode *node, DictKey key )
76 {
77 DictNode *newNode;
78
79 do {
80 node = node->prev;
81 } while( node->key != NULL && ! (*dict->leq)(dict->frame, node->key, key));
82
83 newNode = (DictNode *) memAlloc( sizeof( DictNode ));
84 if (newNode == NULL) return NULL;
85
86 newNode->key = key;
87 newNode->next = node->next;
88 node->next->prev = newNode;
89 newNode->prev = node;
90 node->next = newNode;
91
92 return newNode;
93 }
94
95 /* really __gl_dictListDelete */
96 void dictDelete( Dict *dict, DictNode *node ) /*ARGSUSED*/
97 {
98 node->next->prev = node->prev;
99 node->prev->next = node->next;
100 memFree( node );
101 }
102
103 /* really __gl_dictListSearch */
104 DictNode *dictSearch( Dict *dict, DictKey key )
105 {
106 DictNode *node = &dict->head;
107
108 do {
109 node = node->next;
110 } while( node->key != NULL && ! (*dict->leq)(dict->frame, key, node->key));
111
112 return node;
113 }