/** * raptor_sequence_set_at: * @seq: sequence object * @idx: index into sequence to operate at * @data: new data item. * * Replace/set an item in a sequence. * * The item at the offset @idx in the sequence is replaced with the * new item @data (which may be NULL). Any existing item is freed * with the sequence's free_handler. If necessary the sequence * is extended (with NULLs) to handle a larger offset. * * The sequence takes ownership of the new data item. On failure, the * item is freed immediately. * * Return value: non-0 on failure **/ int raptor_sequence_set_at(raptor_sequence* seq, int idx, void *data) { int need_capacity; RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(seq, raptor_sequence, 1); /* Cannot provide a negative index */ if(idx < 0) { if(data) { if(seq->free_handler) seq->free_handler(data); else if(seq->free_handler_v2) seq->free_handler_v2(seq->handler_context, data); } return 1; } need_capacity=seq->start+idx+1; if(need_capacity > seq->capacity) { if(seq->capacity*2 > need_capacity) need_capacity = seq->capacity*2; if(raptor_sequence_ensure(seq, need_capacity, 0)) { if(data) { if(seq->free_handler) seq->free_handler(data); else if(seq->free_handler_v2) seq->free_handler_v2(seq->handler_context, data); } return 1; } } if(idx < seq->size) { /* if there is old data, delete it if there is a free handler */ if(seq->sequence[seq->start+idx]) { if(seq->free_handler) seq->free_handler(seq->sequence[seq->start+idx]); else if(seq->free_handler_v2) seq->free_handler_v2(seq->handler_context, seq->sequence[seq->start+idx]); } /* size remains the same */ } else { /* if there is no old data, size is increasing */ /* make sure there are seq->size items starting from seq->start */ seq->size=idx+1; } seq->sequence[seq->start+idx]=data; return 0; }
/* Store methods */ int raptor_sequence_set_at(raptor_sequence* seq, int idx, void *data) { if(idx+1 > seq->capacity) { if(raptor_sequence_ensure(seq, idx+1)) return 1; } if(seq->sequence[idx] && seq->free_handler) seq->free_handler(seq->sequence[idx]); seq->sequence[idx]=data; if(idx+1 > seq->size) seq->size=idx+1; return 0; }
/** * raptor_sequence_join: * @dest: #raptor_sequence destination sequence * @src: #raptor_sequence source sequence * * Join two sequences moving all items from one sequence to the end of another. * * After this operation, sequence src will be empty (zero size) but * will have the same item capacity as before. * * Return value: non-0 on failure */ int raptor_sequence_join(raptor_sequence* dest, raptor_sequence *src) { RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(dest, raptor_sequence, 1); RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(src, raptor_sequence, 1); if(raptor_sequence_ensure(dest, dest->size + src->size, 0)) return 1; memcpy(&dest->sequence[dest->start+dest->size], &src->sequence[src->start], sizeof(void*)*src->size); dest->size += src->size; src->size=0; return 0; }
/** * raptor_sequence_shift: * @seq: sequence to add to * @data: item to add * * Add an item to the start of the sequence. * * The sequence takes ownership of the shifted item and frees it with the * free_handler. On failure, the item is freed immediately. * * Return value: non-0 on failure **/ int raptor_sequence_shift(raptor_sequence* seq, void *data) { RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(seq, raptor_sequence, 1); if(!seq->start) { if(raptor_sequence_ensure(seq, seq->capacity*2, 1)) { if(data) { if(seq->free_handler) seq->free_handler(data); else if(seq->free_handler_v2) seq->free_handler_v2(seq->handler_context, data); } return 1; } } seq->sequence[--seq->start]=data; seq->size++; return 0; }
static int raptor_sequence_grow(raptor_sequence *seq) { return raptor_sequence_ensure(seq, seq->capacity*2); }