Re: offset-based hash table for ASCII data
Rainer Weikusat wrote:
Mark Space <markspace@sbc.global.net> writes:
Rex Mottram wrote:
I'm looking for an offset-based data structure to hold character
data.
I'm not sure what you mean by "offset based data structure."
This fairly obviously means 'replacing pointers by offsets relative to
the start of the data structure' (because pointer values usually
cannot even be meaningfully communicated between different processes
running on the same machine, let alone processes running on different
machines).
Is that all you need? The Java version should be a tad easier, actually.
Note the last loop uses nothing but offsets from the buffer to print
out the data.
If you where hoping for the magic library that does this for you, I
think it's called "hands on keyboard."
lut_test a 12 longer_string_test C
Total buffer size: 63
Numer of entries: 5
String 0: filename
String 1: a
String 2: 12
String 3: longer_string_test
String 4: C
/*
* File: lut_test.c
*
* Created on April 15, 2008, 12:43 PM
*/
#include <stdio.h>
#include <stdlib.h>
struct pre_parsed {
int size;
int length;
int indexes[];
};
int main(int argc, char** argv) {
struct pre_parsed *buffer;
char ** argv_copy = argv;
if( argc < 2 )
{
fprintf( stderr, "Usage: lut_test outfile string_list\n");
}
size_t total_string_size = 0;
while( *++argv )
{
size_t len = strlen( *argv );
total_string_size += len+1;
}
// Test some values in debugger
size_t struct_size = sizeof (struct pre_parsed);
size_t int_size = sizeof (int);
size_t array_size = sizeof (int) * (argc-1);
// Build the buffer
buffer = malloc( sizeof (struct pre_parsed) + sizeof (int) *
(argc-1) + total_string_size );
(*buffer).length = argc -1;
(*buffer).size = sizeof (struct pre_parsed) + sizeof (int) *
(argc-1) + total_string_size;
int index = 0;
size_t offset = sizeof (struct pre_parsed)
+ sizeof (int) * (argc-1);
while( *++argv_copy )
{
(*buffer).indexes[index] = offset;
strcpy( (char*)(buffer + offset), *argv_copy );
offset += strlen( *argv_copy ) + 1;
index++;
}
// Read back from the buffer
printf( "Total buffer size: %d\n", (*buffer).size );
printf( "Numer of entries: %d\n", (*buffer).length );
int i;
for( i = 0; i < (*buffer).length; i++ )
{
printf( "String %d: %s\n", i,
(buffer + (*buffer).indexes[i]) );
}
return (EXIT_SUCCESS);
}