static void test_Keys_Values_Iter(TestBatch *batch) { Hash *hash = Hash_new(0); // trigger multiple rebuilds. VArray *expected = VA_new(100); VArray *keys; VArray *values; for (uint32_t i = 0; i < 500; i++) { CharBuf *cb = CB_newf("%u32", i); Hash_Store(hash, (Obj*)cb, (Obj*)cb); VA_Push(expected, INCREF(cb)); } VA_Sort(expected, NULL, NULL); keys = Hash_Keys(hash); values = Hash_Values(hash); VA_Sort(keys, NULL, NULL); VA_Sort(values, NULL, NULL); TEST_TRUE(batch, VA_Equals(keys, (Obj*)expected), "Keys"); TEST_TRUE(batch, VA_Equals(values, (Obj*)expected), "Values"); VA_Clear(keys); VA_Clear(values); { Obj *key; Obj *value; Hash_Iterate(hash); while (Hash_Next(hash, &key, &value)) { VA_Push(keys, INCREF(key)); VA_Push(values, INCREF(value)); } } VA_Sort(keys, NULL, NULL); VA_Sort(values, NULL, NULL); TEST_TRUE(batch, VA_Equals(keys, (Obj*)expected), "Keys from Iter"); TEST_TRUE(batch, VA_Equals(values, (Obj*)expected), "Values from Iter"); { ZombieCharBuf *forty = ZCB_WRAP_STR("40", 2); ZombieCharBuf *nope = ZCB_WRAP_STR("nope", 4); Obj *key = Hash_Find_Key(hash, (Obj*)forty, ZCB_Hash_Sum(forty)); TEST_TRUE(batch, Obj_Equals(key, (Obj*)forty), "Find_Key"); key = Hash_Find_Key(hash, (Obj*)nope, ZCB_Hash_Sum(nope)), TEST_TRUE(batch, key == NULL, "Find_Key returns NULL for non-existent key"); } DECREF(hash); DECREF(expected); DECREF(keys); DECREF(values); }
void Inverter_Clear_IMP(Inverter *self) { InverterIVARS *const ivars = Inverter_IVARS(self); for (uint32_t i = 0, max = VA_Get_Size(ivars->entries); i < max; i++) { InvEntry_Clear(VA_Fetch(ivars->entries, i)); } VA_Clear(ivars->entries); ivars->tick = -1; DECREF(ivars->doc); ivars->doc = NULL; }
void PolyLexReader_Close_IMP(PolyLexiconReader *self) { PolyLexiconReaderIVARS *const ivars = PolyLexReader_IVARS(self); if (ivars->readers) { for (uint32_t i = 0, max = VA_Get_Size(ivars->readers); i < max; i++) { LexiconReader *reader = (LexiconReader*)VA_Fetch(ivars->readers, i); if (reader) { LexReader_Close(reader); } } VA_Clear(ivars->readers); } }
void PolyDocReader_close(PolyDocReader *self) { if (self->readers) { u32_t i, max; for (i = 0, max = VA_Get_Size(self->readers); i < max; i++) { DocReader *reader = (DocReader*)VA_Fetch(self->readers, i); if (reader) { DocReader_Close(reader); } } VA_Clear(self->readers); } }
static void test_flatten_spans(TestBatchRunner *runner) { VArray *spans = VA_new(8); VArray *wanted = VA_new(8); HeatMap *heat_map = HeatMap_new(spans, 133); VArray *flattened, *boosts; VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f)); VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f)); flattened = HeatMap_Flatten_Spans(heat_map, spans); VA_Push(wanted, (Obj*)Span_new(10, 6, 1.0f)); VA_Push(wanted, (Obj*)Span_new(16, 4, 3.0f)); VA_Push(wanted, (Obj*)Span_new(20, 10, 2.0f)); TEST_TRUE(runner, VA_Equals(flattened, (Obj*)wanted), "flatten two overlapping spans"); VA_Clear(wanted); boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans); VA_Push(wanted, (Obj*)Span_new(10, 20, 3.0f)); TEST_TRUE(runner, VA_Equals(boosts, (Obj*)wanted), "prox boosts for overlap"); VA_Clear(wanted); VA_Clear(spans); DECREF(boosts); DECREF(flattened); VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f)); VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f)); VA_Push(spans, (Obj*)Span_new(50, 1, 1.0f)); flattened = HeatMap_Flatten_Spans(heat_map, spans); VA_Push(wanted, (Obj*)Span_new(10, 6, 1.0f)); VA_Push(wanted, (Obj*)Span_new(16, 4, 3.0f)); VA_Push(wanted, (Obj*)Span_new(20, 10, 2.0f)); VA_Push(wanted, (Obj*)Span_new(50, 1, 1.0f)); TEST_TRUE(runner, VA_Equals(flattened, (Obj*)wanted), "flatten two overlapping spans, leave hole, then third span"); VA_Clear(wanted); boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans); TEST_TRUE(runner, VA_Get_Size(boosts) == 2 + 1, "boosts generated for each unique pair, since all were in range"); VA_Clear(spans); DECREF(boosts); DECREF(flattened); VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f)); VA_Push(spans, (Obj*)Span_new(14, 4, 4.0f)); VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f)); flattened = HeatMap_Flatten_Spans(heat_map, spans); VA_Push(wanted, (Obj*)Span_new(10, 4, 1.0f)); VA_Push(wanted, (Obj*)Span_new(14, 2, 5.0f)); VA_Push(wanted, (Obj*)Span_new(16, 2, 7.0f)); VA_Push(wanted, (Obj*)Span_new(18, 2, 3.0f)); VA_Push(wanted, (Obj*)Span_new(20, 10, 2.0f)); TEST_TRUE(runner, VA_Equals(flattened, (Obj*)wanted), "flatten three overlapping spans"); VA_Clear(wanted); boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans); TEST_TRUE(runner, VA_Get_Size(boosts) == 2 + 1, "boosts generated for each unique pair, since all were in range"); VA_Clear(spans); DECREF(boosts); DECREF(flattened); VA_Push(spans, (Obj*)Span_new(10, 10, 1.0f)); VA_Push(spans, (Obj*)Span_new(16, 14, 4.0f)); VA_Push(spans, (Obj*)Span_new(16, 14, 2.0f)); VA_Push(spans, (Obj*)Span_new(30, 10, 10.0f)); flattened = HeatMap_Flatten_Spans(heat_map, spans); VA_Push(wanted, (Obj*)Span_new(10, 6, 1.0f)); VA_Push(wanted, (Obj*)Span_new(16, 4, 7.0f)); VA_Push(wanted, (Obj*)Span_new(20, 10, 6.0f)); VA_Push(wanted, (Obj*)Span_new(30, 10, 10.0f)); TEST_TRUE(runner, VA_Equals(flattened, (Obj*)wanted), "flatten 4 spans, middle two have identical range"); VA_Clear(wanted); boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans); TEST_TRUE(runner, VA_Get_Size(boosts) == 3 + 2 + 1, "boosts generated for each unique pair, since all were in range"); VA_Clear(spans); DECREF(boosts); DECREF(flattened); VA_Push(spans, (Obj*)Span_new( 10, 10, 1.0f)); VA_Push(spans, (Obj*)Span_new( 16, 4, 4.0f)); VA_Push(spans, (Obj*)Span_new( 16, 14, 2.0f)); VA_Push(spans, (Obj*)Span_new(230, 10, 10.0f)); flattened = HeatMap_Flatten_Spans(heat_map, spans); VA_Push(wanted, (Obj*)Span_new( 10, 6, 1.0f)); VA_Push(wanted, (Obj*)Span_new( 16, 4, 7.0f)); VA_Push(wanted, (Obj*)Span_new( 20, 10, 2.0f)); VA_Push(wanted, (Obj*)Span_new(230, 10, 10.0f)); TEST_TRUE(runner, VA_Equals(flattened, (Obj*)wanted), "flatten 4 spans, middle two have identical starts but different ends"); VA_Clear(wanted); boosts = HeatMap_Generate_Proximity_Boosts(heat_map, spans); TEST_TRUE(runner, VA_Get_Size(boosts) == 2 + 1, "boosts not generated for out of range span"); VA_Clear(spans); DECREF(boosts); DECREF(flattened); DECREF(heat_map); DECREF(wanted); DECREF(spans); }
void SortWriter_finish(SortWriter *self) { SortWriterIVARS *const ivars = SortWriter_IVARS(self); VArray *const field_writers = ivars->field_writers; // If we have no data, bail out. if (!ivars->temp_ord_out) { return; } // If we've either flushed or added segments, flush everything so that any // one field can use the entire margin up to mem_thresh. if (ivars->flush_at_finish) { for (uint32_t i = 1, max = VA_Get_Size(field_writers); i < max; i++) { SortFieldWriter *field_writer = (SortFieldWriter*)VA_Fetch(field_writers, i); if (field_writer) { SortFieldWriter_Flush(field_writer); } } } // Close down temp streams. OutStream_Close(ivars->temp_ord_out); OutStream_Close(ivars->temp_ix_out); OutStream_Close(ivars->temp_dat_out); for (uint32_t i = 1, max = VA_Get_Size(field_writers); i < max; i++) { SortFieldWriter *field_writer = (SortFieldWriter*)VA_Delete(field_writers, i); if (field_writer) { CharBuf *field = Seg_Field_Name(ivars->segment, i); SortFieldWriter_Flip(field_writer); int32_t count = SortFieldWriter_Finish(field_writer); Hash_Store(ivars->counts, (Obj*)field, (Obj*)CB_newf("%i32", count)); int32_t null_ord = SortFieldWriter_Get_Null_Ord(field_writer); if (null_ord != -1) { Hash_Store(ivars->null_ords, (Obj*)field, (Obj*)CB_newf("%i32", null_ord)); } int32_t ord_width = SortFieldWriter_Get_Ord_Width(field_writer); Hash_Store(ivars->ord_widths, (Obj*)field, (Obj*)CB_newf("%i32", ord_width)); } DECREF(field_writer); } VA_Clear(field_writers); // Store metadata. Seg_Store_Metadata_Str(ivars->segment, "sort", 4, (Obj*)SortWriter_Metadata(self)); // Clean up. Folder *folder = ivars->folder; CharBuf *seg_name = Seg_Get_Name(ivars->segment); CharBuf *path = CB_newf("%o/sort_ord_temp", seg_name); Folder_Delete(folder, path); CB_setf(path, "%o/sort_ix_temp", seg_name); Folder_Delete(folder, path); CB_setf(path, "%o/sort_dat_temp", seg_name); Folder_Delete(folder, path); DECREF(path); }