Commit | Line | Data |
---|---|---|
bb780e4a C |
1 | /* |
2 | ****************************************************************************** | |
3 | * | |
4 | * Module: bb_hash.c | |
5 | * | |
6 | * Functions: | |
7 | * bb_put_hash() - Place an entry in the hash table. | |
8 | * bb_get_hash() - Return an index from the hash table. | |
9 | * | |
10 | * | |
11 | ****************************************************************************** | |
12 | */ | |
13 | ||
14 | /* | |
15 | ****************************************************************************** | |
16 | * Include Files | |
17 | ****************************************************************************** | |
18 | */ | |
19 | #include <stdio.h> | |
20 | #include <rpc/rpc.h> | |
21 | #include "common.h" | |
22 | #include "protocol.h" | |
23 | #include "server.h" | |
24 | ||
25 | static BB_hash bb_hash; /* The hash table of id, index. */ | |
26 | static BB_co_data bb_co_d[BB_MAX_IMP]; /* The company data table. */ | |
27 | ||
28 | int | |
29 | bb_put_hash( id, index) | |
30 | BB_id id; /* The identifier to hash. */ | |
31 | int index; /* Index of this record in co. data tbl.*/ | |
32 | { | |
33 | int i; /* Nice loop variable name. */ | |
34 | int hash_idx = 0; /* Index into hash table. */ | |
35 | int term_idx; /* Termination index of circular search.*/ | |
36 | ||
37 | /* | |
38 | ** Sum up all of the characters in the id and mod it by the | |
39 | ** hash list size. This is the initial hash index. | |
40 | */ | |
41 | for( i = 0; (id[i] != NUL) && (i < BB_ID_NAME_LEN); i++) | |
42 | { | |
43 | hash_idx += id[i]; | |
44 | } | |
45 | hash_idx = term_idx = (hash_idx % BB_MAX_HASH); | |
46 | ||
47 | /* | |
48 | ** Search the table for the first open hash bucket. If the hash | |
49 | ** table does not contain one break at the termination index. | |
50 | */ | |
51 | while( bb_hash[hash_idx].id_ptr != NULL ) | |
52 | { | |
53 | hash_idx = ((hash_idx + 1) % BB_MAX_HASH); | |
54 | if ( hash_idx == term_idx ) | |
55 | break; | |
56 | } | |
57 | ||
58 | /* | |
59 | ** If the hash bucket is empty then install the info here. | |
60 | ** Otherwise the table is full. | |
61 | */ | |
62 | if ( bb_hash[hash_idx].id_ptr == NULL ) | |
63 | { | |
64 | bb_hash[hash_idx].index = index; | |
65 | bb_hash[hash_idx].id_ptr = id; | |
66 | return BB_SUCCESS; | |
67 | } | |
68 | else | |
69 | { | |
70 | return BB_HASH_TABLE_FULL; | |
71 | } | |
72 | } | |
73 | ||
74 | int | |
75 | bb_get_hash( id) | |
76 | BB_id id; /* The identifier to hash. */ | |
77 | { | |
78 | int i; /* Nice loop variable name. */ | |
79 | int hash_idx = 0; /* Index into hash table. */ | |
80 | int term_idx; /* Termination index of circular search.*/ | |
81 | ||
82 | /* | |
83 | ** Sum up all of the characters in the id and mod it by the | |
84 | ** hash list size. This is the initial hash index. | |
85 | */ | |
86 | for( i = 0; (id[i] != NUL) && (i < BB_ID_NAME_LEN); i++) | |
87 | { | |
88 | hash_idx += id[i]; | |
89 | } | |
90 | hash_idx = term_idx = (hash_idx % BB_MAX_HASH); | |
91 | ||
92 | /* | |
93 | ** Search the hash table based upon the initial index. The search | |
94 | ** ends when the index is found or one complete cycle of the hash | |
95 | ** table has been done. This is linear resolution. | |
96 | */ | |
97 | while( ( bb_hash[hash_idx].id_ptr != NULL ) && | |
98 | ( strncmp( bb_hash[hash_idx].id_ptr, id, BB_ID_NAME_LEN) != 0 ) ) | |
99 | { | |
100 | hash_idx = ((hash_idx + 1) % BB_MAX_HASH); | |
101 | if ( hash_idx == term_idx ) | |
102 | break; | |
103 | } | |
104 | ||
105 | /* | |
106 | ** If the id is equal to that of the hash bucket then a match is | |
107 | ** found. Otherwise the table does not contain this id. | |
108 | */ | |
109 | if ( ( bb_hash[hash_idx].id_ptr != NUL ) && | |
110 | ( strncmp( bb_hash[hash_idx].id_ptr, id, BB_ID_NAME_LEN) == 0 ) ) | |
111 | { | |
112 | return bb_hash[hash_idx].index; | |
113 | } | |
114 | else | |
115 | { | |
116 | return BB_HASH_ID_NOT_FOUND; | |
117 | } | |
118 | } |