GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-3a95b7a4e6
cachehash.c
Go to the documentation of this file.
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 /*---------------------------------------------------------------------------*/
9 #ifndef GRASS_RASTER3D_H
10 typedef struct {
11 
12  int nofNames;
13  int *index;
14  char *active;
15  int lastName;
16  int lastIndex;
17  int lastIndexActive;
18 
20 #endif
21 
22 /*---------------------------------------------------------------------------*/
23 
25 {
26  int i;
27 
28  for (i = 0; i < h->nofNames; i++)
29  h->active[i] = 0;
30 
31  h->lastIndexActive = 0;
32 }
33 
34 /*---------------------------------------------------------------------------*/
35 
37 {
38  if (h == NULL)
39  return;
40 
41  if (h->index != NULL)
42  Rast3d_free(h->index);
43  if (h->active != NULL)
44  Rast3d_free(h->active);
45  Rast3d_free(h);
46 }
47 
48 /*---------------------------------------------------------------------------*/
49 
50 void *Rast3d_cache_hash_new(int nofNames)
51 {
52  Rast3d_cache_hash *tmp;
53 
55  if (tmp == NULL) {
56  Rast3d_error("Rast3d_cache_hash_new: error in Rast3d_malloc");
57  return (void *)NULL;
58  }
59 
60  tmp->nofNames = nofNames;
61  tmp->index = (int *)Rast3d_malloc(tmp->nofNames * sizeof(int));
62  tmp->active = (char *)Rast3d_malloc(tmp->nofNames * sizeof(char));
63  if ((tmp->index == NULL) || (tmp->active == NULL)) {
65  Rast3d_error("Rast3d_cache_hash_new: error in Rast3d_malloc");
66  return (void *)NULL;
67  }
68 
70 
71  return tmp;
72 }
73 
74 /*---------------------------------------------------------------------------*/
75 
77 {
78  if (name >= h->nofNames)
80  "Rast3d_cache_hash_remove_name: name %i out of range", name);
81 
82  if (h->active[name] == 0)
84  "Rast3d_cache_hash_remove_name: name %i not in hashtable", name);
85 
86  h->active[name] = 0;
87  if (name == h->lastName)
88  h->lastIndexActive = 0;
89 }
90 
91 /*---------------------------------------------------------------------------*/
92 
94 {
95  if (name >= h->nofNames)
96  Rast3d_fatal_error("Rast3d_cache_hash_load_name: name out of range");
97 
98  if (h->active[name] != 0)
100  "Rast3d_cache_hash_load_name: name already in hashtable");
101 
102  h->index[name] = index;
103  h->active[name] = 1;
104 }
105 
106 /*---------------------------------------------------------------------------*/
107 
109 {
110  int index;
111 
112  if (h->lastIndexActive)
113  if (h->lastName == name)
114  return h->lastIndex;
115 
116  if (!h->active[name])
117  return -1;
118 
119  index = h->index[name];
120 
121  h->lastName = name;
122  h->lastIndex = index;
123  h->lastIndexActive = 1;
124 
125  return index;
126 }
void Rast3d_cache_hash_reset(Rast3d_cache_hash *h)
Definition: cachehash.c:24
void Rast3d_cache_hash_load_name(Rast3d_cache_hash *h, int name, int index)
Definition: cachehash.c:93
void * Rast3d_cache_hash_new(int nofNames)
Definition: cachehash.c:50
void Rast3d_cache_hash_dispose(Rast3d_cache_hash *h)
Definition: cachehash.c:36
int Rast3d_cache_hash_name2index(Rast3d_cache_hash *h, int name)
Definition: cachehash.c:108
void Rast3d_cache_hash_remove_name(Rast3d_cache_hash *h, int name)
Definition: cachehash.c:76
#define NULL
Definition: ccmath.h:32
void Rast3d_free(void *)
Same as free (ptr).
void Rast3d_error(const char *,...) __attribute__((format(printf
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void * Rast3d_malloc(int)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
const char * name
Definition: named_colr.c:6