diff options
author | Sergey Poznyakoff <gray@nxc.no> | 2018-02-04 20:24:29 +0100 |
---|---|---|
committer | Sergey Poznyakoff <gray@nxc.no> | 2018-02-04 20:24:29 +0100 |
commit | 027e83b6f5dc6a63ed9c69e534c0b994041df14f (patch) | |
tree | 871f1355410e8ccfe51cf59ce542b13769fa3230 | |
parent | 7651936f80b1bc6f5b01477ee9e648843097d7b8 (diff) | |
download | varnish-mib-027e83b6f5dc6a63ed9c69e534c0b994041df14f.tar.gz varnish-mib-027e83b6f5dc6a63ed9c69e534c0b994041df14f.tar.bz2 |
Document HashTable
-rw-r--r-- | lib/VarnishMib/HashTable.pm | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/lib/VarnishMib/HashTable.pm b/lib/VarnishMib/HashTable.pm index 0f3b47e..1faef96 100644 --- a/lib/VarnishMib/HashTable.pm +++ b/lib/VarnishMib/HashTable.pm | |||
@@ -127,86 +127,144 @@ sub create { | |||
127 | my $htab; | 127 | my $htab; |
128 | my $hsize; | 128 | my $hsize; |
129 | 129 | ||
130 | $self->{input} = $names; | 130 | $self->{input} = $names; |
131 | delete $self->{hash_table}; | 131 | delete $self->{hash_table}; |
132 | 132 | ||
133 | for ($hsize = (2 * @$names + 1);; $hsize++) { | 133 | for ($hsize = (2 * @$names + 1);; $hsize++) { |
134 | last if $self->{max_hash_size} && $hsize < $self->{max_hash_size}; | 134 | last if $self->{max_hash_size} && $hsize < $self->{max_hash_size}; |
135 | $self->_mktab($hsize) or next; | 135 | $self->_mktab($hsize) or next; |
136 | last unless (defined($self->{max_collisions}) | 136 | last unless (defined($self->{max_collisions}) |
137 | && $self->{collisions} > $self->{max_collisions}); | 137 | && $self->{collisions} > $self->{max_collisions}); |
138 | } | 138 | } |
139 | if ($self->{verbose}) { | 139 | if ($self->{verbose}) { |
140 | # print STDERR "Input: " . @$names . "\n"; | 140 | # print STDERR "Input: " . @$names . "\n"; |
141 | if ($self->{hash_table}) { | 141 | if ($self->{hash_table}) { |
142 | print STDERR "Table size: " . @{$self->{hash_table}} . "\n"; | 142 | print STDERR "Table size: " . @{$self->{hash_table}} . "\n"; |
143 | print STDERR "Collisions: " . $self->{collisions} . "\n"; | 143 | print STDERR "Collisions: " . $self->{collisions} . "\n"; |
144 | } else { | 144 | } else { |
145 | print STDERR "FAILED\n"; | 145 | print STDERR "FAILED\n"; |
146 | } | 146 | } |
147 | } | 147 | } |
148 | return $self->{hash_table}; | 148 | return $self->{hash_table}; |
149 | } | 149 | } |
150 | 150 | ||
151 | =head2 format_input_table | ||
152 | |||
153 | $ht->format_input_table([FILEHANDLE]); | ||
154 | |||
155 | Outputs to I<FILEHANDLE> (default B<STDOUT>) a C array of input names. The | ||
156 | array is declared as | ||
157 | |||
158 | char const *PFXname_table[] | ||
159 | |||
160 | where I<PFX> is replaced by the prefix given when creating the HashTable | ||
161 | object. | ||
162 | |||
163 | =cut | ||
164 | |||
151 | sub format_input_table { | 165 | sub format_input_table { |
152 | my ($self, $fh) = @_; | 166 | my ($self, $fh) = @_; |
153 | $fh ||= \*STDOUT; | 167 | $fh ||= \*STDOUT; |
154 | croak "no input data to format" unless $self->{input}; | 168 | croak "no input data to format" unless $self->{input}; |
155 | print $fh 'char const *' . $self->{prefix} . "name_table[] = {\n"; | 169 | print $fh 'static char const *' . $self->{prefix} . "name_table[] = {\n"; |
156 | foreach my $name (@{$self->{input}}) { | 170 | foreach my $name (@{$self->{input}}) { |
157 | printf $fh $self->{indent} . '"' . $name . "\",\n"; | 171 | printf $fh $self->{indent} . '"' . $name . "\",\n"; |
158 | } | 172 | } |
159 | print $fh "};\n"; | 173 | print $fh "};\n"; |
160 | 174 | ||
161 | } | 175 | } |
162 | 176 | ||
177 | =head2 format_data_table | ||
178 | |||
179 | $ht->format_data_table(CTYPE [, FILEHANDLE]) | ||
180 | |||
181 | Outputs a C array of data associated with input strings. The array is declared | ||
182 | as | ||
183 | |||
184 | CTYPE PFXdata_table[N]; | ||
185 | |||
186 | where I<CTYPE> is the first parameter to the method, I<PFX> is the prefix and | ||
187 | I<N> is the dimension (number of strings for which the hash table is built). | ||
188 | Both B<name_table> and B<data_table> have the same dimension. | ||
189 | |||
190 | =cut | ||
191 | |||
163 | sub format_data_table { | 192 | sub format_data_table { |
164 | my ($self, $type, $fh) = @_; | 193 | my ($self, $type, $fh) = @_; |
165 | $fh ||= \*STDOUT; | 194 | $fh ||= \*STDOUT; |
166 | croak "no data to format" unless $self->{input}; | 195 | croak "no data to format" unless $self->{input}; |
167 | my $n = @{$self->{input}}; | 196 | my $n = @{$self->{input}}; |
168 | print $fh $type . ' ' . $self->{prefix} . "data_table[$n];\n"; | 197 | print $fh 'static '. $type . ' ' . $self->{prefix} . "data_table[$n];\n"; |
169 | } | 198 | } |
170 | 199 | ||
200 | =head2 format_hash_table | ||
201 | |||
202 | $ht->format_hash_table([FILEHANDLE]); | ||
203 | |||
204 | |||
205 | Outputs the hash table to I<FILEHANDLE> (B<STDOUT> by default). The table | ||
206 | is declared as | ||
207 | |||
208 | int PFXhash_table[] | ||
209 | |||
210 | where I<PFX> is the prefix. | ||
211 | |||
212 | =cut | ||
213 | |||
171 | sub format_hash_table { | 214 | sub format_hash_table { |
172 | my ($self, $fh) = @_; | 215 | my ($self, $fh) = @_; |
173 | $fh ||= \*STDOUT; | 216 | $fh ||= \*STDOUT; |
174 | croak "no hash table to format" unless $self->{hash_table}; | 217 | croak "no hash table to format" unless $self->{hash_table}; |
175 | print $fh "static int ".$self->{prefix}."hash_table[] = {\n"; | 218 | print $fh "static int ".$self->{prefix}."hash_table[] = {\n"; |
176 | my $col = 0; | 219 | my $col = 0; |
177 | print $fh $self->{indent}; | 220 | print $fh $self->{indent}; |
178 | foreach my $p (@{$self->{hash_table}}) { | 221 | foreach my $p (@{$self->{hash_table}}) { |
179 | printf $fh "%3d,", defined($p) ? $p : -1; | 222 | printf $fh "%3d,", defined($p) ? $p : -1; |
180 | $col++; | 223 | $col++; |
181 | print $fh ($col % 10 == 0) ? "\n".$self->{indent} : ' '; | 224 | print $fh ($col % 10 == 0) ? "\n".$self->{indent} : ' '; |
182 | } | 225 | } |
183 | print $fh "\n" if ($col % 10); | 226 | print $fh "\n" if ($col % 10); |
184 | print $fh "};\n"; | 227 | print $fh "};\n"; |
185 | my $pfx = $self->{prefix} . 'hash_table'; | 228 | my $pfx = $self->{prefix} . 'hash_table'; |
186 | print $fh "unsigned ${pfx}_size = sizeof($pfx) / sizeof(${pfx}[0]);\n"; | 229 | print $fh "unsigned ${pfx}_size = sizeof($pfx) / sizeof(${pfx}[0]);\n"; |
187 | } | 230 | } |
188 | 231 | ||
232 | =head2 format_code | ||
233 | |||
234 | $ht->format_code([FILEHANDLE]) | ||
235 | |||
236 | Formats the supporting C code to the I<FILEHANDLE> (B<STDOUT>, if not given). | ||
237 | The code contains at least the following function: | ||
238 | |||
239 | unsigned string_hash(const char *str, unsigned size) | ||
240 | |||
241 | which, given the string I<str> and the size of the hash table (I<size>) | ||
242 | returns the index in the table starting from which the pointer to that | ||
243 | string and associated data can be located. | ||
244 | |||
245 | =cut | ||
246 | |||
189 | sub format_code { | 247 | sub format_code { |
190 | my ($self, $fh) = @_; | 248 | my ($self, $fh) = @_; |
191 | $fh ||= \*STDOUT; | 249 | $fh ||= \*STDOUT; |
192 | seek DATA, 0, 0; | 250 | seek DATA, 0, 0; |
193 | my $visible = 0; | 251 | my $visible = 0; |
194 | while (<DATA>) { | 252 | while (<DATA>) { |
195 | if (/^__C__$/) { | 253 | if (/^__C__$/) { |
196 | $visible = 1; | 254 | $visible = 1; |
197 | } elsif ($visible) { | 255 | } elsif ($visible) { |
198 | s{/\*\s*STATIC\s*\*/}{static}; | 256 | s{/\*\s*STATIC\s*\*/}{static}; |
199 | print $fh "$_"; | 257 | print $fh "$_"; |
200 | } | 258 | } |
201 | } | 259 | } |
202 | } | 260 | } |
203 | 261 | ||
204 | sub format_program { | 262 | sub format_program { |
205 | my ($self, $type, $fh) = @_; | 263 | my ($self, $type, $fh) = @_; |
206 | $fh ||= \*STDOUT; | 264 | $fh ||= \*STDOUT; |
207 | $self->format_input_table($fh); | 265 | $self->format_input_table($fh); |
208 | print $fh "\n"; | 266 | print $fh "\n"; |
209 | $self->format_data_table($type, $fh); | 267 | $self->format_data_table($type, $fh); |
210 | print $fh "\n"; | 268 | print $fh "\n"; |
211 | $self->format_hash_table($fh); | 269 | $self->format_hash_table($fh); |
212 | print $fh "\n"; | 270 | print $fh "\n"; |