GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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/G3d.h>
6 #include "G3d_intern.h"
7 
8 /*---------------------------------------------------------------------------*/
9 #ifndef GRASS_G3D_H
10 typedef struct
11 {
12 
13  int nofNames;
14  int *index;
15  char *active;
16  int lastName;
17  int lastIndex;
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  G3d_free(h->index);
44  if (h->active != NULL)
45  G3d_free(h->active);
46  G3d_free(h);
47 }
48 
49 /*---------------------------------------------------------------------------*/
50 
51 void *G3d_cache_hash_new(int nofNames)
52 {
53  G3d_cache_hash *tmp;
54 
55  tmp = G3d_malloc(sizeof(G3d_cache_hash));
56  if (tmp == NULL) {
57  G3d_error("G3d_cache_hash_new: error in G3d_malloc");
58  return (void *)NULL;
59  }
60 
61  tmp->nofNames = nofNames;
62  tmp->index = G3d_malloc(sizeof(int) * tmp->nofNames);
63  tmp->active = G3d_malloc(sizeof(char) * tmp->nofNames);
64  if ((tmp->index == NULL) || (tmp->active == NULL)) {
66  G3d_error("G3d_cache_hash_new: error in G3d_malloc");
67  return (void *)NULL;
68  }
69 
71 
72  return tmp;
73 }
74 
75 /*---------------------------------------------------------------------------*/
76 
78 {
79  if (name >= h->nofNames)
80  G3d_fatalError("G3d_cache_hash_remove_name: name out of range");
81 
82  if (h->active[name] == 0)
83  G3d_fatalError("G3d_cache_hash_remove_name: name not in hashtable");
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  G3d_fatalError("G3d_cache_hash_load_name: name out of range");
96 
97  if (h->active[name] != 0)
98  G3d_fatalError("G3d_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 G3d_free(void *buf)
Same as free (ptr).
Definition: g3dalloc.c:71
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
string name
Definition: render.py:1314
char * active
Definition: cachehash.c:15
void G3d_cache_hash_reset(G3d_cache_hash *h)
Definition: cachehash.c:25
void G3d_cache_hash_dispose(G3d_cache_hash *h)
Definition: cachehash.c:37
int lastIndexActive
Definition: cachehash.c:18
void * G3d_cache_hash_new(int nofNames)
Definition: cachehash.c:51
return NULL
Definition: dbfopen.c:1394
void * G3d_malloc(int nBytes)
Same as malloc (nBytes), except that in case of error G3d_error() is invoked.
Definition: g3dalloc.c:24
void G3d_cache_hash_load_name(G3d_cache_hash *h, int name, int index)
Definition: cachehash.c:92
void G3d_cache_hash_remove_name(G3d_cache_hash *h, int name)
Definition: cachehash.c:77
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
tuple h
panel.defaultSize = wx.CheckBox(panel, id = wx.ID_ANY, label = _(&quot;Use default size&quot;)) panel...
int G3d_cache_hash_name2index(G3d_cache_hash *h, int name)
Definition: cachehash.c:106