dict.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. */
  34. #include <stddef.h>
  35. #include "dict-list.h"
  36. #include "memalloc.h"
  37. /* really __gl_dictListNewDict */
  38. Dict *dictNewDict( void *frame,
  39. int (*leq)(void *frame, DictKey key1, DictKey key2) )
  40. {
  41. Dict *dict = (Dict *) memAlloc( sizeof( Dict ));
  42. DictNode *head;
  43. if (dict == NULL) return NULL;
  44. head = &dict->head;
  45. head->key = NULL;
  46. head->next = head;
  47. head->prev = head;
  48. dict->frame = frame;
  49. dict->leq = leq;
  50. return dict;
  51. }
  52. /* really __gl_dictListDeleteDict */
  53. void dictDeleteDict( Dict *dict )
  54. {
  55. DictNode *node, *next;
  56. for( node = dict->head.next; node != &dict->head; node = next ) {
  57. next = node->next;
  58. memFree( node );
  59. }
  60. memFree( dict );
  61. }
  62. /* really __gl_dictListInsertBefore */
  63. DictNode *dictInsertBefore( Dict *dict, DictNode *node, DictKey key )
  64. {
  65. DictNode *newNode;
  66. do {
  67. node = node->prev;
  68. } while( node->key != NULL && ! (*dict->leq)(dict->frame, node->key, key));
  69. newNode = (DictNode *) memAlloc( sizeof( DictNode ));
  70. if (newNode == NULL) return NULL;
  71. newNode->key = key;
  72. newNode->next = node->next;
  73. node->next->prev = newNode;
  74. newNode->prev = node;
  75. node->next = newNode;
  76. return newNode;
  77. }
  78. /* really __gl_dictListDelete */
  79. void dictDelete( Dict *dict, DictNode *node ) /*ARGSUSED*/
  80. {
  81. node->next->prev = node->prev;
  82. node->prev->next = node->next;
  83. memFree( node );
  84. }
  85. /* really __gl_dictListSearch */
  86. DictNode *dictSearch( Dict *dict, DictKey key )
  87. {
  88. DictNode *node = &dict->head;
  89. do {
  90. node = node->next;
  91. } while( node->key != NULL && ! (*dict->leq)(dict->frame, key, node->key));
  92. return node;
  93. }