Skip to content

Commit

Permalink
Fix vec_pivot_m3, minor fixes for vecs of 0-size types
Browse files Browse the repository at this point in the history
Improve some doc comments and includes
  • Loading branch information
hacatu committed Feb 11, 2024
1 parent d265e3e commit 1c7ea86
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/crater/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ inline static void *cr8r_hash_next(cr8r_hashtbl_t *self, const cr8r_hashtbl_ft *
if(self->table_b){
if(cur){
b = (cur - self->table_b)/ft->base.size;
if(b++ >= self->len_b){
if(b++ >= self->len_b){// also goes to SEARCH_A if "b < 0" due to unsigned overflow wrapping
goto SEARCH_A;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/crater/prand.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ cr8r_prng *cr8r_prng_init_splitmix(uint64_t seed){
return res;
}

// need to create anonymous struct to statically allocate fla
static struct{
uint64_t state_size;
uint32_t (*get_u32)(void*);
Expand Down
24 changes: 15 additions & 9 deletions src/lib/crater/vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ void *cr8r_default_resize(cr8r_base_ft *base, void *p, uint64_t cap){
}
return NULL;
}
return p ? realloc(p, cap*base->size) : malloc(cap*base->size);
if(base->size){
return p ? realloc(p, cap*base->size) : malloc(cap*base->size);
}
return p;
}

void *cr8r_default_resize_pass(cr8r_base_ft *base, void *p, uint64_t cap){
Expand Down Expand Up @@ -208,7 +211,7 @@ void cr8r_vec_shuffle(cr8r_vec *self, cr8r_vec_ft *ft, cr8r_prng *prng){
}

static inline bool ensure_cap(cr8r_vec *self, cr8r_vec_ft *ft, uint64_t cap){
if(cap > self->cap && ft->base.size){
if(cap > self->cap){
uint64_t new_cap = ft->new_size(&ft->base, self->cap);
if(cap > new_cap){
new_cap = cap;
Expand Down Expand Up @@ -577,21 +580,24 @@ void *cr8r_vec_pivot_m3(const cr8r_vec *self, cr8r_vec_ft *ft, uint64_t a, uint6
return NULL;
}
void *fst = self->buf + a*ft->base.size;
if(b - a < 3){
return fst;
}
void *lst = self->buf + (b - 1)*ft->base.size;
void *mid = self->buf + (a + b - 1)/2*ft->base.size;
int ord = ft->cmp(&ft->base, fst, mid);
int sorted;
if(!ord){
if(!ord){// if *fst == *mid, then both are valid pivots regardless of *lst
return mid;
}else if((sorted = ord*ft->cmp(&ft->base, mid, lst))){
if(sorted > 0){
}else if((sorted = ord*ft->cmp(&ft->base, mid, lst))){// multiplying the ord of *mid and *lst by the ord of *fst and *mid lets us easily detect if the sequence is monotonic
if(sorted > 0){// in this case, either *fst < *mid < *lst OR *fst > *mid > *lst
return mid;
}else if(ft->cmp(&ft->base, lst, fst)*ord >= 0){
}else if(ft->cmp(&ft->base, lst, fst)*ord >= 0){// if sorted < 0, *mid is either the max or min, determined by ord (the comparison between *fst and *mid)
return fst;// if the ord of *fst and *mid is the same as the ord of *lst and *fst, then either *lst < *fst < *mid or *lst > *fst > *mid. Alternatively, if *lst == *fst, ten both are valid pivots
}else{// otherwise, *fst is either the max or min, but *mid is also either the max or min, and since none of the three are equal in this case, we can conclude tht *lst is the median
return lst;
}else{
return fst;
}
}else{
}else{// if *fst != *mid but *mid == *lst, then both mid and lst are valid pivots regardless of *fst
return mid;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/dlog_t64.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "crater/container.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <crater/container.h>
#include <crater/prand.h>

const uint64_t gs[4] = {1, (1ull << 63) + 3, (1ull << 63) - 1, -1};
Expand Down
1 change: 0 additions & 1 deletion src/test/minmax_heap.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "crater/container.h"
#include "crater/vec.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down
1 change: 0 additions & 1 deletion src/test/pandigital_primes.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//This test is based on Project Euler 41
#include "crater/container.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
2 changes: 1 addition & 1 deletion src/test/pointcloud.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "crater/kd_check.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>

#include <crater/kd_check.h>
#include <crater/kd_tree.h>

int main(){
Expand Down
1 change: 0 additions & 1 deletion src/test/vec_bsearch.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "crater/container.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down
8 changes: 6 additions & 2 deletions src/test/vec_oom.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#define _GNU_SOURCE
#include "crater/container.h"
#include "crater/vec.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include <crater/vec.h>
#include <crater/minmax_heap.h>

const char *__asan_default_options(){
return "detect_leaks=0";
// there is a bogus leak that asan finds but valgrind doesn't
}

inline static void default_copy(cr8r_base_ft *ft, void *dest, const void *src){
memcpy(dest, src, ft->size);
}
Expand Down
1 change: 0 additions & 1 deletion src/test/wc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#define _POSIX_C_SOURCE 200809L
#include "crater/container.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down

0 comments on commit 1c7ea86

Please sign in to comment.