Compare commits
1 Commits
master
...
vector_sor
Author | SHA1 | Date | |
---|---|---|---|
04e4d07e99 |
@ -61,3 +61,12 @@ for (int i = 0; i < 10; i++) {
|
|||||||
}
|
}
|
||||||
free(pd);
|
free(pd);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## reallocarray
|
||||||
|
|
||||||
|
reallocarray is reimplemented for macOS because Apple doesn't expose their implementation. This is taken straight
|
||||||
|
from the OpenBSD source
|
||||||
|
|
||||||
|
```c
|
||||||
|
void *reallocarray(void *optr, size_t nmemb, size_t size);
|
||||||
|
```
|
||||||
|
15
docs/math.md
15
docs/math.md
@ -57,3 +57,18 @@ Works the same as `bresenham()` but uses the `Point` struct instead of `int`
|
|||||||
```c
|
```c
|
||||||
Point *bresenham_p(Point p1, Point p2, size_t *sz);
|
Point *bresenham_p(Point p1, Point p2, size_t *sz);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Comparison Functions
|
||||||
|
|
||||||
|
Comparison functions to compare two items. These functions must return `1` if `a > b`, `-1` if
|
||||||
|
`a < b`, or `0` if `a == b`. This follows the pattern defined by `compar` functions in the C standard
|
||||||
|
library, making these usuable in the standard sorting functions like `qsort`
|
||||||
|
|
||||||
|
```c
|
||||||
|
int compar_int(const void *a, const void *b);
|
||||||
|
int compar_char(const void *a, const void *b);
|
||||||
|
|
||||||
|
// Example
|
||||||
|
qsort(arr, arr_sz, sizeof(int), compar_int);
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -137,16 +137,6 @@ comparison function to compare the data in the vector. This function must return
|
|||||||
const void *vec_max(const Vector *vec, int(*cmp)(const void *a, const void *b));
|
const void *vec_max(const Vector *vec, int(*cmp)(const void *a, const void *b));
|
||||||
```
|
```
|
||||||
|
|
||||||
## Comparison Functions
|
|
||||||
|
|
||||||
Comparison functions to compare data in a vector. These functions must return `1` if `a > b`, `-1` if
|
|
||||||
`a < b`, or `0` if `a == b`.
|
|
||||||
|
|
||||||
```c
|
|
||||||
int vec_cmp_int(const void *a, const void *b);
|
|
||||||
int vec_cmp_char(const void *a, const void *b);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Macros
|
## Macros
|
||||||
|
|
||||||
### vec_at
|
### vec_at
|
||||||
|
@ -15,4 +15,9 @@ Point *bresenham(int x0, int y0, int x1, int y1, size_t *sz);
|
|||||||
|
|
||||||
Point *bresenham_p(Point p1, Point p2, size_t *sz);
|
Point *bresenham_p(Point p1, Point p2, size_t *sz);
|
||||||
|
|
||||||
|
int compar_int(const void *a, const void *b);
|
||||||
|
|
||||||
|
int compar_char(const void *a, const void *b);
|
||||||
|
|
||||||
|
|
||||||
#endif // LIBFLINT_H_MATH
|
#endif // LIBFLINT_H_MATH
|
||||||
|
@ -30,9 +30,7 @@ const void *vec_min(const Vector *vec, int(*cmp)(const void *a, const void *b));
|
|||||||
|
|
||||||
const void *vec_max(const Vector *vec, int(*cmp)(const void *a, const void *b));
|
const void *vec_max(const Vector *vec, int(*cmp)(const void *a, const void *b));
|
||||||
|
|
||||||
int vec_cmp_int(const void *a, const void *b);
|
int vec_sort(Vector *vec, int(*cmp)(const void *a, const void *b));
|
||||||
|
|
||||||
int vec_cmp_char(const void *a, const void *b);
|
|
||||||
|
|
||||||
#define vec_at(v, i) (v)->elements[(i)]
|
#define vec_at(v, i) (v)->elements[(i)]
|
||||||
|
|
||||||
|
@ -77,3 +77,11 @@ Point *bresenham_p(Point p1, Point p2, size_t *sz) {
|
|||||||
return bresenham(p1.x, p1.y, p2.x, p2.y, sz);
|
return bresenham(p1.x, p1.y, p2.x, p2.y, sz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int compar_int(const void *a, const void *b) {
|
||||||
|
return (*(int*)a - *(int*)b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int compar_char(const void *a, const void *b) {
|
||||||
|
return (*(char*)a - *(char*)b);
|
||||||
|
}
|
||||||
|
34
src/vector.c
34
src/vector.c
@ -35,9 +35,7 @@ int vec_init_with_capacity(Vector *vec, void (*destroy)(void *data), size_t capa
|
|||||||
|
|
||||||
static int vec_grow(Vector *const vec) {
|
static int vec_grow(Vector *const vec) {
|
||||||
vec->capacity *= 2;
|
vec->capacity *= 2;
|
||||||
|
|
||||||
vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *));
|
vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *));
|
||||||
|
|
||||||
if (vec->elements == NULL) {
|
if (vec->elements == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -110,19 +108,11 @@ int vec_shrink(Vector *vec) {
|
|||||||
if (vec_len(vec) == vec_cap(vec)) {
|
if (vec_len(vec) == vec_cap(vec)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec->capacity = vec_len(vec);
|
vec->capacity = vec_len(vec);
|
||||||
|
|
||||||
#ifdef __OpenBSD__
|
|
||||||
vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *));
|
vec->elements = reallocarray(vec->elements, vec->capacity, sizeof(void *));
|
||||||
#else
|
|
||||||
vec->elements = reallocf(vec->elements, sizeof(void *) * vec->capacity);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (vec->elements == NULL) {
|
if (vec->elements == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,28 +136,10 @@ const void *vec_max(const Vector *vec, int(*cmp)(const void *a, const void *b))
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
int vec_cmp_int(const void *a, const void *b) {
|
int vec_sort(Vector *vec, int(*cmp)(const void *a, const void *b)) {
|
||||||
const int x = *(int*)a;
|
if (vec_len(vec) == 0) {
|
||||||
const int y = *(int*)b;
|
|
||||||
|
|
||||||
if (x > y) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (x < y) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
qsort(vec->elements, vec->length, sizeof(void *), cmp);
|
||||||
int vec_cmp_char(const void *a, const void *b) {
|
|
||||||
const char x = *(int*)a;
|
|
||||||
const char y = *(int*)b;
|
|
||||||
|
|
||||||
if (x > y) {
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (x < y) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
@ -239,8 +239,8 @@ void test_vector() {
|
|||||||
t = (int*)vec_at(v, 4);
|
t = (int*)vec_at(v, 4);
|
||||||
assert(*t == e4);
|
assert(*t == e4);
|
||||||
|
|
||||||
const int *min = vec_min(v, vec_cmp_int);
|
const int *min = vec_min(v, compar_int);
|
||||||
const int *max = vec_max(v, vec_cmp_int);
|
const int *max = vec_max(v, compar_int);
|
||||||
printf("min: %d\n", *min);
|
printf("min: %d\n", *min);
|
||||||
printf("max: %d\n", *max);
|
printf("max: %d\n", *max);
|
||||||
assert(*min == e0);
|
assert(*min == e0);
|
||||||
@ -260,6 +260,19 @@ void test_vector() {
|
|||||||
assert(vec_len(v) == vec_cap(v));
|
assert(vec_len(v) == vec_cap(v));
|
||||||
printf("cap after shrink: %zu\n", vec_cap(v));
|
printf("cap after shrink: %zu\n", vec_cap(v));
|
||||||
|
|
||||||
|
int s1 = 10;
|
||||||
|
int s2 = 2;
|
||||||
|
int s3 = 1;
|
||||||
|
vec_push(v, &s1);
|
||||||
|
vec_push(v, &s2);
|
||||||
|
vec_push(v, &s3);
|
||||||
|
|
||||||
|
printf("Before sort: ");
|
||||||
|
print_vector(v);
|
||||||
|
vec_sort(v, compar_int);
|
||||||
|
printf("After sort: ");
|
||||||
|
print_vector(v);
|
||||||
|
|
||||||
vec_destroy(v);
|
vec_destroy(v);
|
||||||
free(v);
|
free(v);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user