Bool Vector_append_binary_file(Vector *v, Allocator v_allocator, FILE *in) { size_t const original_size = Vector_size(v); size_t buffer_size = 8192; for (;;) { size_t const offset_to_read_to = Vector_size(v); size_t const max_size_t = ~(size_t)0; size_t resizing = original_size + buffer_size; size_t read_size; size_t actually_read; if (resizing > max_size_t) { resizing = max_size_t; } if (!Vector_resize(v, resizing, v_allocator)) { Vector_resize_smaller(v, original_size); return False; } read_size = resizing - offset_to_read_to; actually_read = fread(Vector_data(v) + offset_to_read_to, 1, read_size, in); if (actually_read < read_size) { Vector_resize_smaller(v, offset_to_read_to + actually_read); return True; } } }
Bool Vector_printf(Vector *v, Allocator allocator, char const *format, ...) { size_t const speculative_result_length = strlen(format) * 2; va_list args; ptrdiff_t printed; if (!Vector_resize(v, speculative_result_length, allocator)) { return False; } va_start(args, format); printed = vsnprintf(Vector_data(v), Vector_size(v), format, args); assert(printed >= 0); if ((size_t)printed == Vector_size(v)) { if (!Vector_resize(v, (size_t)printed + 1, allocator)) { va_end(args); return False; } printed = vsnprintf(Vector_data(v), Vector_size(v), format, args); assert((size_t)printed == Vector_size(v)); } Vector_resize_smaller(v, (size_t)printed + 1); va_end(args); return True; }
/* * display all the entrants based on status/duration */ void display_results(Event* event) { Entrant* entrant; int i = 0; entrants_sort(event); /* since the entrants are now sorted, we know that all entrants with a particular * status will be grouped together */ /* display finished */ if (count_by_status(event, FINISHED) > 0) { printf("\n\tFinished:\n"); for (; i < Vector_size(event->entrants); i++) { Vector_get(event->entrants, i, &entrant); if (entrant->status != FINISHED) break; /* if entrant hasn't finished, skip to the next block of entrants */ printf("\t\t%3d: %-50s\n", entrant->id, entrant->name); printf("\t\t\tCourse: %c Total time: %3d mins\n", entrant->course->id, entrant->duration); } } /* display started and stopped */ if (count_by_status(event, STARTED) + count_by_status(event, STOPPED) > 0) { printf("\n\tRunning:\n"); for (; i < Vector_size(event->entrants); i++) { Vector_get(event->entrants, i, &entrant); if (entrant->status != STARTED && entrant->status != STOPPED) break; printf("\t\t%3d: %-50s\n", entrant->id, entrant->name); printf("\t\t\tCourse: %c Track: %2d Run time: %3d mins\n", entrant->course->id, entrant->curr_track->id, entrant->duration); } } /* display disqualified */ if (count_by_status(event, DISQUAL_SAFETY) + count_by_status(event, DISQUAL_INCORR) > 0) { printf("\n\tDisqualified:\n"); for (; i < Vector_size(event->entrants); i++) { Vector_get(event->entrants, i, &entrant); if (entrant->status != DISQUAL_SAFETY && entrant->status != DISQUAL_INCORR) break; printf("\t\t%3d: %-50s\n", entrant->id, entrant->name); printf("\t\t\tCourse: %c ", entrant->course->id); if (entrant->status == DISQUAL_SAFETY) printf("Disqualified for safety\n"); else printf("Disqualified for incorrect route\n"); } } /* display waiting to start */ if (count_by_status(event, NOT_STARTED) > 0) { printf("\n\tWaiting to start:\n"); for (; i < Vector_size(event->entrants); i++) { Vector_get(event->entrants, i, &entrant); printf("\t\t%3d: %-50s\n", entrant->id, entrant->name); printf("\t\t\tCourse: %c\n", entrant->course->id); } } }
static HandlerResult MetersPanel_eventHandler(Panel* super, int ch) { MetersPanel* this = (MetersPanel*) super; int selected = Panel_getSelectedIndex(super); HandlerResult result = IGNORED; switch(ch) { case 0x0a: case 0x0d: case KEY_ENTER: case KEY_F(4): case 't': { Meter* meter = (Meter*) Vector_get(this->meters, selected); int mode = meter->mode + 1; if (mode == LAST_METERMODE) mode = 1; Meter_setMode(meter, mode); Panel_set(super, selected, (Object*) Meter_toListItem(meter)); result = HANDLED; break; } case KEY_F(7): case '[': case '-': { Vector_moveUp(this->meters, selected); Panel_moveSelectedUp(super); result = HANDLED; break; } case KEY_F(8): case ']': case '+': { Vector_moveDown(this->meters, selected); Panel_moveSelectedDown(super); result = HANDLED; break; } case KEY_F(9): case KEY_DC: { if (selected < Vector_size(this->meters)) { Vector_remove(this->meters, selected); Panel_remove(super, selected); } result = HANDLED; break; } } if (result == HANDLED) { Header* header = this->settings->header; this->settings->changed = true; Header_calculateHeight(header); Header_draw(header); ScreenManager_resize(this->scr, this->scr->x1, header->height, this->scr->x2, this->scr->y2); } return result; }
// Test your vector here int main() { Vector* temp = Vector_create(); //create a vector Vector_append( temp, "a");//1 Vector_append( temp, "b");//2 Vector_append( temp, "c");//3 Vector_append( temp, "d");//4 Vector_append( temp, NULL);//5 Vector_append( temp, "f");//6 Vector_append( temp, "g");//7 Vector_append( temp, NULL);//8 Vector_append( temp, "");//9 Vector_append( temp, "h");//10 Vector_append( temp, "i");//11 Vector_append( temp, NULL);//12 Vector_resize( temp, 20 ); if( Vector_size(temp) != 20 || strcmp( Vector_get( temp, 10 ), "i" ) || Vector_get( temp, 15 ) != NULL ) perror( "something wrong.\n"); //done for append, resize, get, size Vector_set( temp, 19, "caibi" ); Vector_insert( temp, 20, "niubi" ); Vector_insert( temp, 30, "wori" ); if( Vector_size(temp) != 31 || strcmp( Vector_get( temp, 19 ), "caibi" ) || strcmp( Vector_get( temp, 20 ), "niubi" ) || Vector_get( temp, 15 ) != NULL || strcmp( Vector_get( temp, 30 ), "wori" ) ) perror( "something wrong.\n"); Vector_delete( temp, 11 ); Vector_delete( temp, 27 ); Vector_delete( temp, 1 ); Vector_delete( temp, 18 ); if( Vector_size(temp) != 27 || strcmp( Vector_get( temp, 4 ), "f" ) || strcmp( Vector_get( temp, 26 ), "wori") || Vector_get( temp, 18 ) !=NULL || strcmp( Vector_get( temp, 17 ), "caibi") ) perror( "something wrong.\n"); Vector_destroy( temp ); return 0; }
/* * returns how many entrants have a particular status */ int count_by_status(Event* event, entrant_status status) { Entrant* entrant; int ret_val = 0; int i = 0; for (i = 0; i < Vector_size(event->entrants); i++) { Vector_get(event->entrants, i, &entrant); if (entrant->status == status) ret_val++; } return ret_val; }
void solve(Vector stack){ while(Vector_size(stack)){ struct Node *n; Vector_pop(stack, &n); if(n->type == Value){ add_input(stack, n->low, n->id); } else if(n->type == Bot){ add_input(stack, n->low, min(n->inputs[0], n->inputs[1])); add_input(stack, n->high, max(n->inputs[0], n->inputs[1])); } } }
static void test_vector(void) { MemoryManager memory = create_standard_memory_manager(); { Vector v; Vector_init(&v); TEST(Vector_empty(&v)); TEST(Vector_size(&v) == 0); Vector_free(&v, memory.deallocator); } { Vector v; Vector_init(&v); TEST(Vector_reserve(&v, 100, memory.allocator)); TEST(Vector_reserve(&v, 200, memory.allocator)); TEST(Vector_reserve(&v, 50, memory.allocator)); TEST(Vector_reserve(&v, 0, memory.allocator)); Vector_free(&v, memory.deallocator); } { Vector v; Vector_init(&v); TEST(Vector_resize(&v, 100, memory.allocator)); TEST(Vector_resize(&v, 200, memory.allocator)); TEST(Vector_resize(&v, 50, memory.allocator)); TEST(Vector_resize(&v, 0, memory.allocator)); Vector_free(&v, memory.deallocator); } { size_t i; size_t const count = 1000; Vector v; Vector_init(&v); for (i = 0; i < count; ++i) { char c = (char)('0' + (i % 10)); ASSERT(Vector_push_back(&v, &c, 1, memory.allocator)); } for (i = 0; i < count; ++i) { char c = (char)('0' + (i % 10)); ASSERT(Vector_data(&v)[i] == c); } Vector_free(&v, memory.deallocator); } }
bool Order_list_contains_pat_inst_ref(const Order_list* ol, const Pat_inst_ref* piref) { rassert(ol != NULL); rassert(piref != NULL); for (int64_t i = 0; i < Vector_size(ol->pat_insts); ++i) { const Pat_inst_ref* cur_piref = Vector_get_ref(ol->pat_insts, i); if (Pat_inst_ref_cmp(cur_piref, piref) == 0) return true; } return false; }
MetersPanel* MetersPanel_new(Settings* settings, const char* header, Vector* meters, ScreenManager* scr) { MetersPanel* this = AllocThis(MetersPanel); Panel* super = (Panel*) this; Panel_init(super, 1, 1, 1, 1, Class(ListItem), true); this->settings = settings; this->meters = meters; this->scr = scr; Panel_setHeader(super, header); for (int i = 0; i < Vector_size(meters); i++) { Meter* meter = (Meter*) Vector_get(meters, i); Panel_add(super, (Object*) Meter_toListItem(meter)); } return this; }
/* * list entrants excluded for getting lost */ void list_excluded_incorrect(Event* event) { Entrant* entrant; int i = 0; /* make sure there's at least one */ if (count_by_status(event, DISQUAL_INCORR) == 0) { printf("\tNo entrants disqualified for incorrect route\n"); } else { for (i = 0; i < Vector_size(event->entrants); i++) { Vector_get(event->entrants, i, &entrant); if (entrant->status == DISQUAL_INCORR) { printf("\t%2d: %-50s\n", entrant->id, entrant->name); printf("\t\tCourse: %c Last node: %2d\n", entrant->course->id, entrant->last_cp_node->id); } } } }
void run(Vector v, int *r){ int size = Vector_size(v); struct Inst *instrs = Vector_data(v); for(int i = 0;i < size; ++i){ struct Inst *inst = &instrs[i]; switch(inst->op_code){ case cpy: r[inst->y.x] = to_value(r, inst->x); break; case inc: ++r[inst->x.x]; break; case dec: --r[inst->x.x]; break; case jnz: if (to_value(r, inst->x)) i += to_value(r, inst->y)-1; break; default: printf("Error bad instruction\n"); } } }
/* * update entrants from a file */ int refresh(Event* event, char* filename, int t_index) { Vector* lines = read_file(filename); char* line; char* token; char type; int node_id; int entrant_id; Time* time; int i; for (i = t_index; i < Vector_size(lines); i++) { Vector_get(lines, i, &line); /* type */ token = strtok(line, " "); type = token[0]; /* node id */ token = strtok(NULL, " "); node_id = atoi(token); /* entrant id */ token = strtok(NULL, " "); entrant_id = atoi(token); /* time */ token = strtok(NULL, "\n"); time = str_to_time(token); update_time(event, time, entrant_id); entrant_update_location(event, type, entrant_id, node_id); } Vector_dispose(lines); return i; }
static inline void addLine(const char* line, Vector* lines, Panel* panel, const char* incFilter) { Vector_add(lines, (Object*) ListItem_new(line, 0)); if (!incFilter || String_contains_i(line, incFilter)) Panel_add(panel, (Object*)Vector_get(lines, Vector_size(lines)-1)); }
int Order_list_get_len(const Order_list* ol) { rassert(ol != NULL); return (int)Vector_size(ol->pat_insts); }