-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from qpfiffer/sort_by_datetime
Sort by datetime
- Loading branch information
Showing
4 changed files
with
244 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// vim: noet ts=4 sw=4 | ||
#pragma once | ||
#include <stdlib.h> | ||
|
||
/* xXx STRUCT=vector xXx | ||
* xXx DESCRIPTION=A simple vector object. Auto-expands and whatever. xXx | ||
* xXx item_size=The maximum size of each item. xXx | ||
* xXx max_size=Used internally to track the current vector's maximum number of elements. xXx | ||
* xXx count=Used internally to track the current vector's current count of items. xXx | ||
* xXx *items=The actual memory used for the items stored. xXx | ||
*/ | ||
typedef struct vector { | ||
const size_t item_size; | ||
size_t max_size; | ||
size_t count; | ||
void *items; | ||
} vector; | ||
|
||
/* xXx FUNCTION=vector_new xXx | ||
* xXx DESCRIPTION=Creates a new vector object. xXx | ||
* xXx RETURNS=A new vector object. xXx | ||
* xXx item_size=The maximum size of each item. xXx | ||
* xXx initial_element_count=If you know your amount of objects ahead of time, set this accordingly. Otherwise just guess. The closer you get the fewer mallocs will happen. xXx | ||
*/ | ||
vector *vector_new(const size_t item_size, const size_t initial_element_count); | ||
|
||
/* xXx FUNCTION=vector_append xXx | ||
* xXx DESCRIPTION=Adds a new element to a vector. xXx | ||
* xXx RETURNS=1 on success. xXx | ||
* xXx *vec=The vector to add the new item to. xXx | ||
* xXx *item=The item to add to the vector. xXx | ||
* xXx item_size=The size of the item to be added. xXx | ||
*/ | ||
int vector_append(vector *vec, const void *item, const size_t item_size); | ||
|
||
/* xXx FUNCTION=vector_append_ptr xXx | ||
* xXx DESCRIPTION=Similar to vector_append but copies just the pointer value, not what it points to. xXx | ||
* xXx RETURNS=1 on success. xXx | ||
* xXx *vec=The vector to add the pointer to. xXx | ||
* xXx *pointer=The item to add to the vector. xXx | ||
*/ | ||
int vector_append_ptr(vector *vec, const void *pointer); | ||
|
||
/* xXx FUNCTION=vector_get xXx | ||
* xXx DESCRIPTION=Gets the nth element of the given vector. xXx | ||
* xXx RETURNS=A constant pointer to the nth item in the vector. xXx | ||
* xXx *vec=The vector to get the item from. xXx | ||
* xXx i=The item you want to retrieve. xXx | ||
*/ | ||
const void *vector_get(const vector *vec, const unsigned int i); | ||
|
||
/* xXx FUNCTION=vector_reverse xXx | ||
* xXx DESCRIPTION=Reverses the vector, from beginning to end. xXx | ||
* xXx RETURNS=1 on success. xXx | ||
* xXx *vec=The vector to be reversed. xXx | ||
*/ | ||
int vector_reverse(vector *vec); | ||
|
||
/* xXx FUNCTION=vector_free xXx | ||
* xXx DESCRIPTION=Cleans up and removes a vector's allocated memory. xXx | ||
* xXx RETURNS=Nothing. xXx | ||
* xXx *to_free=The vector to free. xXx | ||
*/ | ||
void vector_free(vector *to_free); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// vim: noet ts=4 sw=4 | ||
#include <assert.h> | ||
#include <string.h> | ||
#include "vector.h" | ||
|
||
#define nth(I) (void *)(vec->items + (I * vec->item_size)) | ||
|
||
vector *vector_new(const size_t item_size, const size_t initial_element_count) { | ||
vector _vec = { | ||
.item_size = item_size, | ||
.max_size = initial_element_count, | ||
.count = 0, | ||
.items = calloc(1, initial_element_count * item_size + 1) | ||
}; | ||
|
||
vector *to_return = calloc(1, sizeof(vector)); | ||
memcpy(to_return, &_vec, sizeof(vector)); | ||
|
||
return to_return; | ||
} | ||
|
||
inline int vector_append(vector *vec, const void *item, const size_t item_size) { | ||
if (vec == NULL) | ||
return 0; | ||
|
||
if (item_size > vec->item_size) | ||
return 0; | ||
|
||
if (vec->count == vec->max_size) { | ||
vec->max_size *= 2; | ||
void *array = realloc(vec->items, vec->max_size * vec->item_size + 1); | ||
if (!array) | ||
return 0; | ||
vec->items = array; | ||
} | ||
|
||
if (item_size != 0 && item != NULL) { | ||
memcpy(nth(vec->count), item, item_size); | ||
memset(nth(vec->count) + item_size, '\0', sizeof(char)); | ||
} else { | ||
/* Just zero out the memory so we can check for NULLs. */ | ||
memset(nth(vec->count), 0, vec->item_size); | ||
} | ||
vec->count++; | ||
return 1; | ||
} | ||
|
||
inline int vector_append_ptr(vector *vec, const void *pointer) { | ||
if (vec == NULL) | ||
return 0; | ||
|
||
if (vec->item_size != sizeof(pointer)) | ||
return 0; | ||
|
||
if (vec->count == vec->max_size) { | ||
vec->max_size *= 2; | ||
void *array = realloc(vec->items, (vec->max_size * vec->item_size)); | ||
if (!array) | ||
return 0; | ||
vec->items = array; | ||
} | ||
|
||
memcpy(nth(vec->count), &pointer, sizeof(void *)); | ||
vec->count++; | ||
return 1; | ||
} | ||
|
||
inline const void *vector_get(const vector *vec, const unsigned int i) { | ||
if (vec == NULL) | ||
return NULL; | ||
if (i > vec->max_size) | ||
return NULL; | ||
return nth(i); | ||
} | ||
|
||
int vector_reverse(vector *vec) { | ||
if (vec == NULL) | ||
return 0; | ||
|
||
const size_t item_size = vec->item_size; | ||
unsigned int i = 0; | ||
|
||
unsigned char *buf = malloc(item_size); | ||
if (!buf) | ||
return 0; | ||
|
||
for (;i < (unsigned int)(vec->count / 2); i++) { | ||
const void *item = vector_get(vec, i); | ||
const unsigned int offset = vec->count - i - 1; | ||
if (!memcpy(buf, vector_get(vec, offset), item_size)) | ||
return 0; | ||
|
||
if (!memcpy(nth(offset), item, item_size)) | ||
return 0; | ||
|
||
if (!memcpy(nth(i), buf, item_size)) | ||
return 0; | ||
} | ||
free(buf); | ||
|
||
return 1; | ||
} | ||
|
||
void vector_free(vector *vec) { | ||
if (vec == NULL) | ||
return; | ||
free(vec->items); | ||
free(vec); | ||
} | ||
|