Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Reverted optional bounds checking, vec_at() and tests
Browse files Browse the repository at this point in the history
The bounds checking introduced some problems when a vector of structs
was used.
  • Loading branch information
rxi committed Aug 8, 2014
1 parent 86d8386 commit e193590
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 52 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ To define a new vector type the `vec_t()` macro should be used:
typedef vec_t(unsigned int) uint_vec_t;
```
## Debug Mode
If the macro `VEC_DEBUG` is defined then all functions will do bounds checking
and abort if a check fails.
## Functions
All vector functions are macro functions. The parameter `v` in each function
Expand Down Expand Up @@ -84,11 +80,6 @@ successful, otherwise -1 is returned and the vector remains unchanged.
### vec\_pop(v)
Removes and returns the value at the end of the vector.

### vec\_at(v, idx)
Returns the value at the given index `idx`. Using `vec_at()` as opposed to
accessing the `.data` field directly has the advantage of out-of-bounds
checking when VEC_DEBUG is enabled.

### vec\_splice(v, start, count)
Removes the number of values specified by `count`, starting at the index
`start`.
Expand Down
38 changes: 8 additions & 30 deletions src/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
#define VEC_VERSION "0.2.0"


#ifdef VEC_DEBUG
#include <assert.h>
#define vec_checkidx_(len, idx) assert((idx) >= 0 && (idx) < (len))
#else
#define vec_checkidx_(len, idx) ((void) 0)
#endif


#define vec_unpack_(v)\
(char**)&(v)->data, &(v)->length, &(v)->capacity, sizeof(*(v)->data)

Expand All @@ -45,32 +37,21 @@


#define vec_pop(v)\
( vec_checkidx_((v)->length, 0),\
(v)->data[--(v)->length] )


#define vec_at(v, idx)\
( vec_checkidx_((v)->length, idx),\
(v)->data[idx] )
(v)->data[--(v)->length]


#define vec_splice(v, start, count)\
( vec_checkidx_((v)->length, start),\
vec_checkidx_((v)->length, (start) + (count) - 1),\
vec_splice_(vec_unpack_(v), start, count),\
( vec_splice_(vec_unpack_(v), start, count),\
(v)->length -= (count) )


#define vec_swapsplice(v, start, count)\
( vec_checkidx_((v)->length, start),\
vec_checkidx_((v)->length, (start) + (count) - 1),\
vec_swapsplice_(vec_unpack_(v), start, count),\
( vec_swapsplice_(vec_unpack_(v), start, count),\
(v)->length -= (count) )


#define vec_insert(v, idx, val)\
( vec_checkidx_((v)->length + 1, idx),\
vec_insert_(vec_unpack_(v), idx) ? -1 :\
( vec_insert_(vec_unpack_(v), idx) ? -1 :\
((v)->data[idx] = (val)), (v)->length++, 0 )


Expand All @@ -79,9 +60,7 @@


#define vec_swap(v, idx1, idx2)\
( vec_checkidx_((v)->length, idx1),\
vec_checkidx_((v)->length, idx2),\
vec_swap_(vec_unpack_(v), idx1, idx2) )
vec_swap_(vec_unpack_(v), idx1, idx2)


#define vec_truncate(v, len)\
Expand All @@ -93,13 +72,11 @@


#define vec_first(v)\
( vec_checkidx_((v)->length, 0),\
(v)->data[0] )
(v)->data[0]


#define vec_last(v)\
( vec_checkidx_((v)->length, 0),\
(v)->data[(v)->length - 1] )
(v)->data[(v)->length - 1]


#define vec_reserve(v, n)\
Expand Down Expand Up @@ -186,6 +163,7 @@ void vec_swapsplice_(char **data, int *length, int *capacity, int memsz,
void vec_swap_(char **data, int *length, int *capacity, int memsz,
int idx1, int idx2);


typedef vec_t(void*) vec_void_t;
typedef vec_t(char*) vec_str_t;
typedef vec_t(int) vec_int_t;
Expand Down
2 changes: 1 addition & 1 deletion test/build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
os.makedirs(BIN_DIR)

os.system(
"gcc -Wall -Wextra -D VEC_DEBUG -I%s -o %s/test_vec.bin ../src/*.c test_vec.c"
"gcc -Wall -Wextra -I%s -o %s/test_vec.bin ../src/*.c test_vec.c"
% (INCLUDE_DIR, BIN_DIR))

12 changes: 0 additions & 12 deletions test/test_vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ int main(void) {
vec_deinit(&v);
}

{ test_section("vec_at");
vec_int_t v;
vec_init(&v);
vec_push(&v, 'a');
vec_push(&v, 'b');
vec_push(&v, 'c');
test_assert(vec_at(&v, 0) == 'a');
test_assert(vec_at(&v, 1) == 'b');
test_assert(vec_at(&v, 2) == 'c');
vec_deinit(&v);
}

{ test_section("vec_splice");
vec_int_t v;
vec_init(&v);
Expand Down

0 comments on commit e193590

Please sign in to comment.