GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72846
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
13  int nofNames;
14  int *index;
15  char *active;
16  int lastName;
17  int lastIndex;
18  int lastIndexActive;
19 
21 #endif
22 
23 /*---------------------------------------------------------------------------*/
24 
26 {
27  int i;
28 
29  for (i = 0; i < h->nofNames; i++)
30  h->active[i] = 0;
31 
32  h->lastIndexActive = 0;
33 }
34 
35 /*---------------------------------------------------------------------------*/
36 
38 {
39  if (h == NULL)
40  return;
41 
42  if (h->index != NULL)
43  Rast3d_free(h->index);
44  if (h->active != NULL)
45  Rast3d_free(h->active);
46  Rast3d_free(h);
47 }
48 
49 /*---------------------------------------------------------------------------*/
50 
51 void *Rast3d_cache_hash_new(int nofNames)
52 {
53  Rast3d_cache_hash *tmp;
54 
56  if (tmp == NULL) {
57  Rast3d_error("Rast3d_cache_hash_new: error in Rast3d_malloc");
58  return (void *)NULL;
59  }
60 
61  tmp->nofNames = nofNames;
62  tmp->index = (int*) Rast3d_malloc(tmp->nofNames * sizeof(int));
63  tmp->active = (char*) Rast3d_malloc(tmp->nofNames * sizeof(char));
64  if ((tmp->index == NULL) || (tmp->active == NULL)) {
66  Rast3d_error("Rast3d_cache_hash_new: error in Rast3d_malloc");
67  return (void *)NULL;
68  }
69 
71 
72  return tmp;
73 }
74 
75 /*---------------------------------------------------------------------------*/
76 
78 {
79  if (name >= h->nofNames)
80  Rast3d_fatal_error("Rast3d_cache_hash_remove_name: name %i out of range", name);
81 
82  if (h->active[name] == 0)
83  Rast3d_fatal_error("Rast3d_cache_hash_remove_name: name %i not in hashtable", name);
84 
85  h->active[name] = 0;
86  if (name == h->lastName)
87  h->lastIndexActive = 0;
88 }
89 
90 /*---------------------------------------------------------------------------*/
91 
93 {
94  if (name >= h->nofNames)
95  Rast3d_fatal_error("Rast3d_cache_hash_load_name: name out of range");
96 
97  if (h->active[name] != 0)
98  Rast3d_fatal_error("Rast3d_cache_hash_load_name: name already in hashtable");
99 
100  h->index[name] = index;
101  h->active[name] = 1;
102 }
103 
104 /*---------------------------------------------------------------------------*/
105 
107 {
108  int index;
109 
110  if (h->lastIndexActive)
111  if (h->lastName == name)
112  return h->lastIndex;
113 
114  if (!h->active[name])
115  return -1;
116 
117  index = h->index[name];
118 
119  h->lastName = name;
120  h->lastIndex = index;
121  h->lastIndexActive = 1;
122 
123  return index;
124 }
void Rast3d_cache_hash_load_name(Rast3d_cache_hash *h, int name, int index)
Definition: cachehash.c:92
void Rast3d_fatal_error(const char *msg,...)
Prints fatal error message.
int Rast3d_cache_hash_name2index(Rast3d_cache_hash *h, int name)
Definition: cachehash.c:106
#define NULL
Definition: ccmath.h:32
void * Rast3d_cache_hash_new(int nofNames)
Definition: cachehash.c:51
void Rast3d_error(const char *msg,...)
void Rast3d_free(void *buf)
Same as free (ptr).
void * Rast3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error Rast3d_error() is invoked.
void Rast3d_cache_hash_dispose(Rast3d_cache_hash *h)
Definition: cachehash.c:37
const char * name
Definition: named_colr.c:7
void Rast3d_cache_hash_remove_name(Rast3d_cache_hash *h, int name)
Definition: cachehash.c:77
void Rast3d_cache_hash_reset(Rast3d_cache_hash *h)
Definition: cachehash.c:25