GRASS Programmer's Manual
6.4.3(2013)-r
Main Page
Related Pages
Namespaces
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
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
;
18
int
lastIndexActive
;
19
20
}
G3d_cache_hash
;
21
#endif
22
23
/*---------------------------------------------------------------------------*/
24
25
void
G3d_cache_hash_reset
(
G3d_cache_hash
*
h
)
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
37
void
G3d_cache_hash_dispose
(
G3d_cache_hash
*
h
)
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
)) {
65
G3d_cache_hash_dispose
(tmp);
66
G3d_error
(
"G3d_cache_hash_new: error in G3d_malloc"
);
67
return
(
void
*)
NULL
;
68
}
69
70
G3d_cache_hash_reset
(tmp);
71
72
return
tmp;
73
}
74
75
/*---------------------------------------------------------------------------*/
76
77
void
G3d_cache_hash_remove_name
(
G3d_cache_hash
*
h
,
int
name
)
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
92
void
G3d_cache_hash_load_name
(
G3d_cache_hash
*
h
,
int
name
,
int
index)
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
106
int
G3d_cache_hash_name2index
(
G3d_cache_hash
*
h
,
int
name
)
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
}
lib
g3d
cachehash.c
Generated on Thu Sep 26 2013 09:47:58 for GRASS Programmer's Manual by
1.8.4