diff options
Diffstat (limited to 'collections')
| -rw-r--r-- | collections/vector/vector.adoc | 28 | ||||
| -rw-r--r-- | collections/vector/vector.c | 16 | ||||
| -rw-r--r-- | collections/vector/vector.h | 3 | 
3 files changed, 46 insertions, 1 deletions
| diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc index 0409b05..cc83375 100644 --- a/collections/vector/vector.adoc +++ b/collections/vector/vector.adoc @@ -1,7 +1,7 @@  Vector  ======  Tucker Evans -v0.10, 2020-07-03 +v0.11, 2020-07-03  A basic vector, that hold pointers to your data structures. @@ -257,6 +257,32 @@ vec_push(vector, str_dup(str3));  assert(str_cmp(vec_index(vector, 1), str2) == 0);  ---- +[[vec_swap]] ++void vec_swap(vec *self, int i, int j)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Swaps elements at positions +i+ and +j+, does nothing if +i+ or +j+ are out of +bounds. + +Examples +^^^^^^^^ +[source,c] +---- +#include "dvector.h" +#include <string.h> + +char *str1 = "ONE"; +char *str2 = "TWO"; + +vec *vector = vec_new(); +vec_push(vector, str_dup(str1)); +vec_push(vector, str_dup(str2)); + +vec_swap(vector, 0, 1); + +assert(str_cmp(vec_index(vector, 0), str2) == 0); +assert(str_cmp(vec_back(vector), str1) == 0); +---- +  [[vec_clear]]  +void vec_clear(vec *self)+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/collections/vector/vector.c b/collections/vector/vector.c index 5cb56ef..a7e0845 100644 --- a/collections/vector/vector.c +++ b/collections/vector/vector.c @@ -174,6 +174,22 @@ int index;  	return root->base[index];  } +void vec_swap(root, i, j) +vec *root; +int i,j; +{ +	void *tmp; + +	if (!root || i >= root->end || i < 0 || j >= root->end || j < 0) +		return; + +	tmp = root->base[i]; +	root->base[i] = root->base[j]; +	root->base[j] = tmp; + +	return; +} +  void vec_clear(root)  vec *root;  { diff --git a/collections/vector/vector.h b/collections/vector/vector.h index 7d38694..2b1e8e9 100644 --- a/collections/vector/vector.h +++ b/collections/vector/vector.h @@ -21,6 +21,9 @@ void* vec_back(vec*);  void vec_set(vec*, int, void*);  void* vec_index(vec*, int); +void vec_swap(vec*, int, int); + +  /*memory*/  void vec_clear(vec*);  void vec_free(vec*); | 
