cachehash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <grass/raster3d.h>
  6. #include "raster3d_intern.h"
  7. /*---------------------------------------------------------------------------*/
  8. #ifndef GRASS_RASTER3D_H
  9. typedef struct
  10. {
  11. int nofNames;
  12. int *index;
  13. char *active;
  14. int lastName;
  15. int lastIndex;
  16. int lastIndexActive;
  17. } Rast3d_cache_hash;
  18. #endif
  19. /*---------------------------------------------------------------------------*/
  20. void Rast3d_cache_hash_reset(Rast3d_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 Rast3d_cache_hash_dispose(Rast3d_cache_hash * h)
  29. {
  30. if (h == NULL)
  31. return;
  32. if (h->index != NULL)
  33. Rast3d_free(h->index);
  34. if (h->active != NULL)
  35. Rast3d_free(h->active);
  36. Rast3d_free(h);
  37. }
  38. /*---------------------------------------------------------------------------*/
  39. void *Rast3d_cache_hash_new(int nofNames)
  40. {
  41. Rast3d_cache_hash *tmp;
  42. tmp = (Rast3d_cache_hash *)Rast3d_malloc(sizeof(Rast3d_cache_hash));
  43. if (tmp == NULL) {
  44. Rast3d_error("Rast3d_cache_hash_new: error in Rast3d_malloc");
  45. return (void *)NULL;
  46. }
  47. tmp->nofNames = nofNames;
  48. tmp->index = (int*) Rast3d_malloc(tmp->nofNames * sizeof(int));
  49. tmp->active = (char*) Rast3d_malloc(tmp->nofNames * sizeof(char));
  50. if ((tmp->index == NULL) || (tmp->active == NULL)) {
  51. Rast3d_cache_hash_dispose(tmp);
  52. Rast3d_error("Rast3d_cache_hash_new: error in Rast3d_malloc");
  53. return (void *)NULL;
  54. }
  55. Rast3d_cache_hash_reset(tmp);
  56. return tmp;
  57. }
  58. /*---------------------------------------------------------------------------*/
  59. void Rast3d_cache_hash_remove_name(Rast3d_cache_hash * h, int name)
  60. {
  61. if (name >= h->nofNames)
  62. Rast3d_fatal_error("Rast3d_cache_hash_remove_name: name %i out of range", name);
  63. if (h->active[name] == 0)
  64. Rast3d_fatal_error("Rast3d_cache_hash_remove_name: name %i not in hashtable", name);
  65. h->active[name] = 0;
  66. if (name == h->lastName)
  67. h->lastIndexActive = 0;
  68. }
  69. /*---------------------------------------------------------------------------*/
  70. void Rast3d_cache_hash_load_name(Rast3d_cache_hash * h, int name, int index)
  71. {
  72. if (name >= h->nofNames)
  73. Rast3d_fatal_error("Rast3d_cache_hash_load_name: name out of range");
  74. if (h->active[name] != 0)
  75. Rast3d_fatal_error("Rast3d_cache_hash_load_name: name already in hashtable");
  76. h->index[name] = index;
  77. h->active[name] = 1;
  78. }
  79. /*---------------------------------------------------------------------------*/
  80. int Rast3d_cache_hash_name2index(Rast3d_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. }