//TODO failure? return 0 on success int remove_interval(struct pmm_interval_list *l, struct pmm_interval *i) { LOGPRINTF("removing interval with type: %d\n", i->type); //if interval is at top or bottom, rewire top/bottom pointers if(l->top == i) { l->top = i->previous; } if(l->bottom == i) { l->bottom = i->next; } //rewire intervals that were previous/next intervals of the remove-ee if(i->next != NULL) { i->next->previous = i->previous; } if(i->previous != NULL) { i->previous->next = i->next; } //free interval free_interval(&i); l->size -= 1; LOGPRINTF("size: %d\n", l->size); return 1; }
/*! * Initialize an interval with parameters passed. * * Function maybe be used with _INFLECT and _BISECT intervals and with * _POINT intervals if the start parameter is set to the point. May also * be used to on initial IT_GBBP_CLIMB but not once climb_step is non-zero * * @param plane plane that the interval belongs to (defunct) * @param n_p number of parameters of the interval points * @param type interval tyle * @param start pointer to the start parameter array * @param end pointer to the end parameter array * * @return pointer to a newly allocated interval with values initialised as * per the arguments passed * */ struct pmm_interval* init_interval(int plane, int n_p, enum pmm_interval_type type, int *start, int *end) { struct pmm_interval *i; i = new_interval(); i->plane = plane; i->n_p = n_p; i->type = type; switch(i->type) { case IT_GBBP_CLIMB : case IT_GBBP_BISECT : case IT_GBBP_INFLECT : i->start = init_param_array_copy(start, n_p); i->end = init_param_array_copy(end, n_p); break; case IT_POINT : i->start = init_param_array_copy(start, n_p); break; default : ERRPRINTF("Interval type not supported; %s\n", interval_type_to_string(i->type)); free_interval(&i); return NULL; } return i; }
int main(void) { time_t start = get_current_time(); sleep(1); time_t end = get_current_time(); Interval *interval = make_interval(start, end); double diff = interval_duration(interval); print_interval(interval); free_interval(interval); (void) printf("Elapsed time in seconds %lf\n", diff); return EXIT_SUCCESS; }
/*! * frees an interval list structure and members it contains * * @param il pointer to address of the interval list */ void free_interval_list(struct pmm_interval_list **il) { struct pmm_interval *this, *next; this = (*il)->top; while(this != NULL) { next = this->next; free_interval(&this); this = next; } free(*il); *il = NULL; }
//TODO fix naming of functions so 'remove'/'delete' indicates deallocation also //TODO fix return to -1 on failure, 0 on success int remove_top_interval(struct pmm_interval_list *l) { // remove top destructively removes the top element of the list, remember // to read it first or it will be gone forever struct pmm_interval *zombie; if(isempty_interval_list(l)) { return 0; // failure } else { if(l->top == l->bottom) { /* only one element in list */ zombie = l->top; l->top = NULL; l->bottom = NULL; } else { /* swap old top with new top */ zombie = l->top; l->top = zombie->previous; l->top->next=NULL; } free_interval(&zombie); l->size -= 1; return 1; // success } }