Skip to content
Browse files

move posarray into a generic resizable array

  • Loading branch information...
1 parent cf0dfcc commit 9a03c07e6341c0411f71eb0fb24929501eaee697 @wmorgan committed Apr 5, 2012
Showing with 85 additions and 61 deletions.
  1. +27 −21 Makefile
  2. +13 −33 entry.c
  3. +3 −7 entry.h
  4. +41 −0 rarray.h
  5. +1 −0 whistlepig.h
View
48 Makefile
@@ -49,77 +49,83 @@ loc: $(CSRCFILES) $(LEXFILES) $(YFILES) $(HEADERFILES)
## deps (use `make dep` to generate this (in vi: :r !make dep)
batch-run-queries.o: batch-run-queries.c whistlepig.h defaults.h index.h \
segment.h stringmap.h stringpool.h error.h termhash.h query.h search.h \
- mmap-obj.h entry.h khash.h query-parser.h lock.h timer.h
+ mmap-obj.h entry.h khash.h rarray.h query-parser.h lock.h timer.h
benchmark-queries.o: benchmark-queries.c whistlepig.h defaults.h index.h \
segment.h stringmap.h stringpool.h error.h termhash.h query.h search.h \
- mmap-obj.h entry.h khash.h query-parser.h lock.h timer.h
+ mmap-obj.h entry.h khash.h rarray.h query-parser.h lock.h timer.h
dump.o: dump.c whistlepig.h defaults.h index.h segment.h stringmap.h \
stringpool.h error.h termhash.h query.h search.h mmap-obj.h entry.h \
- khash.h query-parser.h lock.h
+ khash.h rarray.h query-parser.h lock.h
entry.o: entry.c whistlepig.h defaults.h index.h segment.h stringmap.h \
stringpool.h error.h termhash.h query.h search.h mmap-obj.h entry.h \
- khash.h query-parser.h lock.h tokenizer.lex.h
+ khash.h rarray.h query-parser.h lock.h tokenizer.lex.h
error.o: error.c error.h
file-indexer.o: file-indexer.c timer.h whistlepig.h defaults.h index.h \
segment.h stringmap.h stringpool.h error.h termhash.h query.h search.h \
- mmap-obj.h entry.h khash.h query-parser.h lock.h
+ mmap-obj.h entry.h khash.h rarray.h query-parser.h lock.h
index.o: index.c whistlepig.h defaults.h index.h segment.h stringmap.h \
stringpool.h error.h termhash.h query.h search.h mmap-obj.h entry.h \
- khash.h query-parser.h lock.h
+ khash.h rarray.h query-parser.h lock.h
interactive.o: interactive.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h timer.h
-lock.o: lock.c
+ entry.h khash.h rarray.h query-parser.h lock.h timer.h
+lock.o: lock.c whistlepig.h defaults.h index.h segment.h stringmap.h \
+ stringpool.h error.h termhash.h query.h search.h mmap-obj.h entry.h \
+ khash.h rarray.h query-parser.h lock.h
make-queries.o: make-queries.c tokenizer.lex.h segment.h defaults.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h
mbox-indexer.o: mbox-indexer.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h timer.h
+ entry.h khash.h rarray.h query-parser.h lock.h timer.h
mmap-obj.o: mmap-obj.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h
+ entry.h khash.h rarray.h query-parser.h lock.h
query-parser.o: query-parser.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h query-parser.tab.h
+ entry.h khash.h rarray.h query-parser.h lock.h query-parser.tab.h
query-parser.lex.o: query-parser.lex.c whistlepig.h defaults.h index.h \
segment.h stringmap.h stringpool.h error.h termhash.h query.h search.h \
- mmap-obj.h entry.h khash.h query-parser.h lock.h query-parser.tab.h
+ mmap-obj.h entry.h khash.h rarray.h query-parser.h lock.h \
+ query-parser.tab.h
query-parser.tab.o: query-parser.tab.c query.h segment.h defaults.h \
stringmap.h stringpool.h error.h termhash.h search.h mmap-obj.h \
query-parser.h query-parser.tab.h
query.o: query.c whistlepig.h defaults.h index.h segment.h stringmap.h \
stringpool.h error.h termhash.h query.h search.h mmap-obj.h entry.h \
- khash.h query-parser.h lock.h
+ khash.h rarray.h query-parser.h lock.h
search.o: search.c whistlepig.h defaults.h index.h segment.h stringmap.h \
stringpool.h error.h termhash.h query.h search.h mmap-obj.h entry.h \
- khash.h query-parser.h lock.h
+ khash.h rarray.h query-parser.h lock.h
segment.o: segment.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h
+ entry.h khash.h rarray.h query-parser.h lock.h
+snippeter.o: snippeter.c whistlepig.h defaults.h index.h segment.h \
+ stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
+ entry.h khash.h rarray.h query-parser.h lock.h tokenizer.lex.h
stringmap.o: stringmap.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h
+ entry.h khash.h rarray.h query-parser.h lock.h
stringpool.o: stringpool.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h
+ entry.h khash.h rarray.h query-parser.h lock.h
termhash.o: termhash.c whistlepig.h defaults.h index.h segment.h \
stringmap.h stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- entry.h khash.h query-parser.h lock.h
+ entry.h khash.h rarray.h query-parser.h lock.h
test-labels.o: test-labels.c test.h query.h segment.h defaults.h \
stringmap.h stringpool.h error.h termhash.h search.h mmap-obj.h \
- query-parser.h index.h entry.h khash.h
+ query-parser.h index.h entry.h khash.h rarray.h
test-labels_main.o: test-labels_main.c error.h test.h
test-queries.o: test-queries.c test.h query.h segment.h defaults.h \
stringmap.h stringpool.h error.h termhash.h search.h mmap-obj.h \
query-parser.h
test-queries_main.o: test-queries_main.c error.h test.h
test-search.o: test-search.c test.h query.h segment.h defaults.h \
stringmap.h stringpool.h error.h termhash.h search.h mmap-obj.h \
- query-parser.h index.h entry.h khash.h
+ query-parser.h index.h entry.h khash.h rarray.h
test-search_main.o: test-search_main.c error.h test.h
test-segment.o: test-segment.c test.h segment.h defaults.h stringmap.h \
stringpool.h error.h termhash.h query.h search.h mmap-obj.h \
- tokenizer.lex.h index.h entry.h khash.h
+ tokenizer.lex.h index.h entry.h khash.h rarray.h
test-segment_main.o: test-segment_main.c error.h test.h
test-stringmap.o: test-stringmap.c stringmap.h stringpool.h error.h \
test.h
View
46 entry.c
@@ -1,29 +1,6 @@
#include "whistlepig.h"
#include "tokenizer.lex.h"
-static posarray* posarray_new(pos_t i) {
- posarray* ret = malloc(sizeof(posarray));
- ret->data = malloc(sizeof(pos_t));
- ret->data[0] = i;
- ret->size = ret->next = 1;
- return ret;
-}
-
-static void posarray_free(posarray* p) {
- free(p->data);
- free(p);
-}
-
-static void posarray_add(posarray* p, pos_t a) {
- while(p->next >= p->size) {
- p->size *= 2;
- p->data = realloc(p->data, p->size * sizeof(pos_t));
- }
- p->data[p->next++] = a;
-}
-
-static inline pos_t posarray_get(posarray* p, int i) { return p->data[i]; }
-
static inline khint_t khash_hash_string(const char *s) {
khint_t h = *s;
if (h) for (++s ; *s; ++s) h = (h << 5) - h + *s;
@@ -59,10 +36,13 @@ RAISING_STATIC(add_token(wp_entry* entry, const char* field, const char* term, s
khiter_t k = kh_put(entries, entry->entries, ft, &status);
if(status == 1) { // not found
- kh_value(entry->entries, k) = posarray_new(entry->next_offset);
+ RARRAY(pos_t) pa;
+ RARRAY_INIT(pos_t, pa);
+ RARRAY_ADD(pos_t, pa, entry->next_offset);
+ kh_value(entry->entries, k) = pa;
}
else { // just add the next offset to the array
- posarray_add(kh_value(entry->entries, k), entry->next_offset);
+ RARRAY_ADD(pos_t, kh_value(entry->entries, k), entry->next_offset);
// don't need these guys any more
free(ft.field);
@@ -79,8 +59,8 @@ uint32_t wp_entry_size(wp_entry* entry) {
for(khiter_t i = kh_begin(entry->entries); i < kh_end(entry->entries); i++) {
if(kh_exist(entry->entries, i)) {
- posarray* positions = kh_val(entry->entries, i);
- ret += positions->next;
+ RARRAY(pos_t) positions = kh_val(entry->entries, i);
+ ret += RARRAY_NELEM(positions);
}
}
@@ -136,8 +116,8 @@ wp_error* wp_entry_write_to_segment(wp_entry* entry, wp_segment* seg, docid_t do
for(khiter_t i = kh_begin(entry->entries); i < kh_end(entry->entries); i++) {
if(kh_exist(entry->entries, i)) {
fielded_term ft = kh_key(entry->entries, i);
- posarray* positions = kh_val(entry->entries, i);
- RELAY_ERROR(wp_segment_add_posting(seg, ft.field, ft.term, doc_id, positions->next, positions->data));
+ RARRAY(pos_t) positions = kh_val(entry->entries, i);
+ RELAY_ERROR(wp_segment_add_posting(seg, ft.field, ft.term, doc_id, RARRAY_NELEM(positions), RARRAY_ALL(positions)));
}
}
@@ -150,10 +130,10 @@ wp_error* wp_entry_sizeof_postings_region(wp_entry* entry, wp_segment* seg, uint
*size = 0;
for(khiter_t i = kh_begin(entry->entries); i < kh_end(entry->entries); i++) {
if(kh_exist(entry->entries, i)) {
- posarray* positions = kh_val(entry->entries, i);
+ RARRAY(pos_t) positions = kh_val(entry->entries, i);
uint32_t this_size;
- RELAY_ERROR(wp_segment_sizeof_posarray(seg, positions->next, positions->data, &this_size));
+ RELAY_ERROR(wp_segment_sizeof_posarray(seg, RARRAY_NELEM(positions), RARRAY_ALL(positions), &this_size));
*size += this_size;
}
}
@@ -165,10 +145,10 @@ wp_error* wp_entry_free(wp_entry* entry) {
for(khiter_t k = kh_begin(entry->entries); k < kh_end(entry->entries); k++) {
if(kh_exist(entry->entries, k)) {
fielded_term ft = kh_key(entry->entries, k);
- posarray* positions = kh_val(entry->entries, k);
+ RARRAY(pos_t) positions = kh_val(entry->entries, k);
free(ft.term);
free(ft.field);
- posarray_free(positions);
+ RARRAY_FREE(pos_t, positions);
}
}
View
10 entry.h
@@ -13,12 +13,7 @@
#include "error.h"
#include "segment.h"
#include "khash.h"
-
-typedef struct posarray {
- uint32_t size;
- uint32_t next;
- pos_t* data;
-} posarray;
+#include "rarray.h"
typedef struct fielded_term {
char* field;
@@ -28,7 +23,8 @@ typedef struct fielded_term {
khint_t fielded_term_hash(fielded_term ft);
khint_t fielded_term_equals(fielded_term a, fielded_term b);
-KHASH_INIT(entries, fielded_term, posarray*, 1, fielded_term_hash, fielded_term_equals);
+RARRAY_DECLARE(pos_t);
+KHASH_INIT(entries, fielded_term, RARRAY(pos_t), 1, fielded_term_hash, fielded_term_equals);
typedef struct wp_entry {
khash_t(entries)* entries;
View
41 rarray.h
@@ -0,0 +1,41 @@
+#ifndef RARRAY_H_
+#define RARRAY_H_
+
+// a growable array list of things
+#include "defaults.h"
+
+#define RARRAY(type) wp_rarray_##type*
+#define RARRAY_DECLARE(type) \
+ typedef struct wp_rarray_##type {\
+ uint32_t size; \
+ uint32_t next; \
+ type* data; \
+ } wp_rarray_##type;
+
+#define RARRAY_INIT(type, name) do { \
+ RARRAY(type) _a = malloc(sizeof(wp_rarray_##type)); \
+ _a->size = 1; \
+ _a->next = 0; \
+ _a->data = malloc(sizeof(type)); \
+ (name) = _a; \
+} while(0)
+
+#define RARRAY_NELEM(name) (name)->next
+#define RARRAY_ALL(name) (name)->data
+#define RARRAY_GET(name, idx) (name)->data[idx]
+#define RARRAY_FREE(type, name) do { \
+ RARRAY(type) _a = (name); \
+ free(_a->data); \
+ free(_a); \
+} while(0)
+
+#define RARRAY_ADD(type, name, val) do { \
+ RARRAY(type) _a = (name); \
+ while(_a->next >= _a->size) { \
+ _a->size *= 2; \
+ _a->data = realloc(_a->data, _a->size * sizeof(type)); \
+ } \
+ _a->data[_a->next++] = (val); \
+} while(0)
+
+#endif
View
1 whistlepig.h
@@ -12,6 +12,7 @@
#include "query-parser.h"
#include "lock.h"
#include "error.h"
+#include "rarray.h"
// see comments in index.c
char* strdup(const char* old);

0 comments on commit 9a03c07

Please sign in to comment.
Something went wrong with that request. Please try again.