GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <stdio.h> 00002 #include <stdlib.h> 00003 #include <sys/types.h> 00004 #include <unistd.h> 00005 #include <grass/G3d.h> 00006 #include "G3d_intern.h" 00007 00008 /*---------------------------------------------------------------------------*/ 00009 #ifndef GRASS_G3D_H 00010 typedef struct 00011 { 00012 00013 int nofNames; 00014 int *index; 00015 char *active; 00016 int lastName; 00017 int lastIndex; 00018 int lastIndexActive; 00019 00020 } G3d_cache_hash; 00021 #endif 00022 00023 /*---------------------------------------------------------------------------*/ 00024 00025 void G3d_cache_hash_reset(G3d_cache_hash * h) 00026 { 00027 int i; 00028 00029 for (i = 0; i < h->nofNames; i++) 00030 h->active[i] = 0; 00031 00032 h->lastIndexActive = 0; 00033 } 00034 00035 /*---------------------------------------------------------------------------*/ 00036 00037 void G3d_cache_hash_dispose(G3d_cache_hash * h) 00038 { 00039 if (h == NULL) 00040 return; 00041 00042 if (h->index != NULL) 00043 G3d_free(h->index); 00044 if (h->active != NULL) 00045 G3d_free(h->active); 00046 G3d_free(h); 00047 } 00048 00049 /*---------------------------------------------------------------------------*/ 00050 00051 void *G3d_cache_hash_new(int nofNames) 00052 { 00053 G3d_cache_hash *tmp; 00054 00055 tmp = G3d_malloc(sizeof(G3d_cache_hash)); 00056 if (tmp == NULL) { 00057 G3d_error("G3d_cache_hash_new: error in G3d_malloc"); 00058 return (void *)NULL; 00059 } 00060 00061 tmp->nofNames = nofNames; 00062 tmp->index = G3d_malloc(sizeof(int) * tmp->nofNames); 00063 tmp->active = G3d_malloc(sizeof(char) * tmp->nofNames); 00064 if ((tmp->index == NULL) || (tmp->active == NULL)) { 00065 G3d_cache_hash_dispose(tmp); 00066 G3d_error("G3d_cache_hash_new: error in G3d_malloc"); 00067 return (void *)NULL; 00068 } 00069 00070 G3d_cache_hash_reset(tmp); 00071 00072 return tmp; 00073 } 00074 00075 /*---------------------------------------------------------------------------*/ 00076 00077 void G3d_cache_hash_remove_name(G3d_cache_hash * h, int name) 00078 { 00079 if (name >= h->nofNames) 00080 G3d_fatalError("G3d_cache_hash_remove_name: name out of range"); 00081 00082 if (h->active[name] == 0) 00083 G3d_fatalError("G3d_cache_hash_remove_name: name not in hashtable"); 00084 00085 h->active[name] = 0; 00086 if (name == h->lastName) 00087 h->lastIndexActive = 0; 00088 } 00089 00090 /*---------------------------------------------------------------------------*/ 00091 00092 void G3d_cache_hash_load_name(G3d_cache_hash * h, int name, int index) 00093 { 00094 if (name >= h->nofNames) 00095 G3d_fatalError("G3d_cache_hash_load_name: name out of range"); 00096 00097 if (h->active[name] != 0) 00098 G3d_fatalError("G3d_cache_hash_load_name: name already in hashtable"); 00099 00100 h->index[name] = index; 00101 h->active[name] = 1; 00102 } 00103 00104 /*---------------------------------------------------------------------------*/ 00105 00106 int G3d_cache_hash_name2index(G3d_cache_hash * h, int name) 00107 { 00108 int index; 00109 00110 if (h->lastIndexActive) 00111 if (h->lastName == name) 00112 return h->lastIndex; 00113 00114 if (!h->active[name]) 00115 return -1; 00116 00117 index = h->index[name]; 00118 00119 h->lastName = name; 00120 h->lastIndex = index; 00121 h->lastIndexActive = 1; 00122 00123 return index; 00124 }