cachehash.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/G3d.h>
  6. #include "G3d_intern.h"
  7. /*---------------------------------------------------------------------------*/
  8. #ifndef GRASS_G3D_H
  9. typedef struct
  10. {
  11. int nofNames;
  12. int *index;
  13. char *active;
  14. int lastName;
  15. int lastIndex;
  16. int lastIndexActive;
  17. } G3d_cache_hash;
  18. #endif
  19. /*---------------------------------------------------------------------------*/
  20. void G3d_cache_hash_reset(G3d_cache_hash * h)
  21. {
  22. int i;
  23. for (i = 0; i < h->nofNames; i++)
  24. h->active[i] = 0;
  25. h->lastIndexActive = 0;
  26. }
  27. /*---------------------------------------------------------------------------*/
  28. void G3d_cache_hash_dispose(G3d_cache_hash * h)
  29. {
  30. if (h == NULL)
  31. return;
  32. if (h->index != NULL)
  33. G3d_free(h->index);
  34. if (h->active != NULL)
  35. G3d_free(h->active);
  36. G3d_free(h);
  37. }
  38. /*---------------------------------------------------------------------------*/
  39. void *G3d_cache_hash_new(int nofNames)
  40. {
  41. G3d_cache_hash *tmp;
  42. tmp = G3d_malloc(sizeof(G3d_cache_hash));
  43. if (tmp == NULL) {
  44. G3d_error("G3d_cache_hash_new: error in G3d_malloc");
  45. return (void *)NULL;
  46. }
  47. tmp->nofNames = nofNames;
  48. tmp->index = G3d_malloc(sizeof(int) * tmp->nofNames);
  49. tmp->active = G3d_malloc(sizeof(char) * tmp->nofNames);
  50. if ((tmp->index == NULL) || (tmp->active == NULL)) {
  51. G3d_cache_hash_dispose(tmp);
  52. G3d_error("G3d_cache_hash_new: error in G3d_malloc");
  53. return (void *)NULL;
  54. }
  55. G3d_cache_hash_reset(tmp);
  56. return tmp;
  57. }
  58. /*---------------------------------------------------------------------------*/
  59. void G3d_cache_hash_remove_name(G3d_cache_hash * h, int name)
  60. {
  61. if (name >= h->nofNames)
  62. G3d_fatalError("G3d_cache_hash_remove_name: name out of range");
  63. if (h->active[name] == 0)
  64. G3d_fatalError("G3d_cache_hash_remove_name: name not in hashtable");
  65. h->active[name] = 0;
  66. if (name == h->lastName)
  67. h->lastIndexActive = 0;
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. void G3d_cache_hash_load_name(G3d_cache_hash * h, int name, int index)
  71. {
  72. if (name >= h->nofNames)
  73. G3d_fatalError("G3d_cache_hash_load_name: name out of range");
  74. if (h->active[name] != 0)
  75. G3d_fatalError("G3d_cache_hash_load_name: name already in hashtable");
  76. h->index[name] = index;
  77. h->active[name] = 1;
  78. }
  79. /*---------------------------------------------------------------------------*/
  80. int G3d_cache_hash_name2index(G3d_cache_hash * h, int name)
  81. {
  82. int index;
  83. if (h->lastIndexActive)
  84. if (h->lastName == name)
  85. return h->lastIndex;
  86. if (!h->active[name])
  87. return -1;
  88. index = h->index[name];
  89. h->lastName = name;
  90. h->lastIndex = index;
  91. h->lastIndexActive = 1;
  92. return index;
  93. }