Skip to content

Commit

Permalink
[libvector] fix repeated vector_next with tmp_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Jul 24, 2023
1 parent 7efee34 commit bffbfd2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
17 changes: 6 additions & 11 deletions gear-lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,12 @@ ADD_SUBDIRECTORY(libbitmap)
ADD_SUBDIRECTORY(libdict)
ADD_SUBDIRECTORY(libfile)
ADD_SUBDIRECTORY(libhash)


IF (NOT DEFINED ENV_MINGW)
ADD_SUBDIRECTORY(libdarray)
ADD_SUBDIRECTORY(libthread)
ADD_SUBDIRECTORY(libsock)
ADD_SUBDIRECTORY(libgevent)
ENDIF ()

IF (NOT DEFINED OS_WINDOWS)
ADD_SUBDIRECTORY(libqueue)
ADD_SUBDIRECTORY(libthread)
ADD_SUBDIRECTORY(libgevent)
ADD_SUBDIRECTORY(libdebug)
ADD_SUBDIRECTORY(libhash)
ADD_SUBDIRECTORY(libdarray)
ADD_SUBDIRECTORY(libqueue)
ADD_SUBDIRECTORY(libtime)
ADD_SUBDIRECTORY(liblog)
ADD_SUBDIRECTORY(libmedia-io)
Expand All @@ -79,6 +68,12 @@ ADD_SUBDIRECTORY(libworkq)
ADD_SUBDIRECTORY(libhal)
ADD_SUBDIRECTORY(librpc)
ADD_SUBDIRECTORY(libmp4)

IF (NOT DEFINED ENV_MINGW)
ENDIF ()

IF (NOT DEFINED OS_WINDOWS)

ENDIF ()

#libcollections
Expand Down
32 changes: 18 additions & 14 deletions gear-lib/libvector/libvector.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ int vector_empty(struct vector *v)
printf("%s: paraments invalid!\n", __func__);
return -1;
}
v->tmp_cursor = 0;
return (v->size == 0);
}

Expand Down Expand Up @@ -104,28 +103,30 @@ vector_iter vector_last(struct vector *v)
return (void *)((uint8_t *)v->buf.iov_base + (v->size-1) * v->type_size);
}

vector_iter vector_next(struct vector *v)
vector_iter vector_next(struct vector *v, vector_iter iter)
{
if (!v) {
if (!v || !iter) {
printf("%s: paraments invalid!\n", __func__);
return NULL;
}
if (v->tmp_cursor < v->size) {
v->tmp_cursor++;
} else {
return NULL;
if (iter == vector_end(v)) {
printf("%s: vector reach the end!\n", __func__);
return iter;
}
return (void *)((uint8_t *)v->buf.iov_base + v->tmp_cursor * v->type_size);
return (void *)((uint8_t *)iter + v->type_size);
}

vector_iter vector_prev(struct vector *v)
vector_iter vector_prev(struct vector *v, vector_iter iter)
{
if (!v) {
if (!v || !iter) {
printf("%s: paraments invalid!\n", __func__);
return NULL;
}
v->tmp_cursor--;
return (void *)((uint8_t *)v->buf.iov_base + v->tmp_cursor * v->type_size);
if (iter == vector_begin(v)) {
printf("%s: vector reach the begin!\n", __func__);
return iter;
}
return (void *)((uint8_t *)iter - v->type_size);
}

void *_vector_iter_value(struct vector *v, vector_iter iter)
Expand All @@ -134,7 +135,11 @@ void *_vector_iter_value(struct vector *v, vector_iter iter)
printf("%s: paraments invalid!\n", __func__);
return NULL;
}
return (void *)((uint8_t *)v->buf.iov_base + v->tmp_cursor * v->type_size);
if (iter > vector_end(v) || iter < vector_begin(v)) {
printf("%s: iter out of range!\n", __func__);
return NULL;
}
return (void *)((uint8_t *)iter);
}

void *_vector_at(struct vector *v, int pos)
Expand All @@ -154,7 +159,6 @@ struct vector *_vector_create(size_t size)
return NULL;
}
v->size = 0;
v->tmp_cursor = 0;
v->type_size = size;
v->max_size = (size_t)(-1/size);
v->capacity = VECTOR_DEFAULT_BUF_LEN;
Expand Down
5 changes: 2 additions & 3 deletions gear-lib/libvector/libvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ typedef struct vector {
size_t max_size; //max number of element
size_t capacity; //size of allocated storage capacity
size_t type_size;
size_t tmp_cursor;
struct iovec buf;
vector_iter iterator;
} vector_t;
Expand Down Expand Up @@ -69,8 +68,8 @@ void _vector_push_back(struct vector *v, void *e, size_t type_size);
vector_iter vector_begin(struct vector *v);
vector_iter vector_end(struct vector *v);
vector_iter vector_last(struct vector *v);//last=end-1
vector_iter vector_next(struct vector *v);
vector_iter vector_prev(struct vector *v);
vector_iter vector_next(struct vector *v, vector_iter iter);
vector_iter vector_prev(struct vector *v, vector_iter iter);
void *_vector_iter_value(struct vector *v, vector_iter iter);
void *_vector_at(struct vector *v, int pos);

Expand Down
4 changes: 2 additions & 2 deletions gear-lib/libvector/test_libvector.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void mix_struct()
c = _vector_create(sizeof(struct tmp_box));
#endif
vector_push_back(c, tb);
for (iter = vector_begin(c); iter != vector_end(c); iter = vector_next(c)) {
for (iter = vector_begin(c); iter != vector_end(c); iter = vector_next(c, iter)) {
struct tmp_box *tt = vector_iter_valuep(c, iter, struct tmp_box);
printf("vector member.c: %c\n", tt->c);
printf("vector member.i: %d\n", tt->i);
Expand Down Expand Up @@ -79,7 +79,7 @@ void default_struct()
vector_push_back(a, t1);
vector_push_back(a, t2);
vector_push_back(a, t3);
for (iter = vector_begin(a); iter != vector_end(a); iter = vector_next(a)) {
for (iter = vector_begin(a); iter != vector_end(a); iter = vector_next(a, iter)) {
printf("vector member: %d\n", *vector_iter_valuep(a, iter, int));
}
for (i = 0; i < a->size; i++) {
Expand Down

0 comments on commit bffbfd2

Please sign in to comment.