rename shift to unshift and add real shift
This commit is contained in:
parent
ce1c05c6e6
commit
1de5b35258
1 changed files with 118 additions and 100 deletions
42
slibs.h
42
slibs.h
|
@ -45,7 +45,8 @@
|
||||||
// Vector
|
// Vector
|
||||||
|
|
||||||
#define sl_vec(type) \
|
#define sl_vec(type) \
|
||||||
struct { \
|
struct \
|
||||||
|
{ \
|
||||||
type *data; \
|
type *data; \
|
||||||
size_t size; \
|
size_t size; \
|
||||||
size_t capacity; \
|
size_t capacity; \
|
||||||
|
@ -66,7 +67,7 @@
|
||||||
(vec).data[(vec).size++] = (element); \
|
(vec).data[(vec).size++] = (element); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define sl_vec_shift(vec, element) \
|
#define sl_vec_unshift(vec, element) \
|
||||||
{ \
|
{ \
|
||||||
if ((vec).size >= (vec).capacity) \
|
if ((vec).size >= (vec).capacity) \
|
||||||
sl_vec_grow(vec); \
|
sl_vec_grow(vec); \
|
||||||
|
@ -75,9 +76,16 @@
|
||||||
(vec).size++; \
|
(vec).size++; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define sl_vec_shift(vec) \
|
||||||
|
{ \
|
||||||
|
memmove((vec).data, (vec).data + 1, (vec).size * sizeof(*(vec).data)); \
|
||||||
|
(vec).size--; \
|
||||||
|
}
|
||||||
|
|
||||||
#define sl_vec_pop(vec) \
|
#define sl_vec_pop(vec) \
|
||||||
{ \
|
{ \
|
||||||
if ((vec).size > 0) { \
|
if ((vec).size > 0) \
|
||||||
|
{ \
|
||||||
(vec).size--; \
|
(vec).size--; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
@ -130,11 +138,13 @@ typedef sl_vec(char) sl_string;
|
||||||
(str).data; \
|
(str).data; \
|
||||||
})
|
})
|
||||||
|
|
||||||
void sl_append_c_str(sl_string* sl_str, const char* c_str);
|
void sl_append_c_str(sl_string *sl_str, const char *c_str);
|
||||||
|
|
||||||
#ifdef SL_IMPLEMENTATION
|
#ifdef SL_IMPLEMENTATION
|
||||||
void sl_append_c_str(sl_string* sl_str, const char* c_str) {
|
void sl_append_c_str(sl_string *sl_str, const char *c_str)
|
||||||
for(int i = 0; i < strlen(c_str); i++) {
|
{
|
||||||
|
for (int i = 0; i < strlen(c_str); i++)
|
||||||
|
{
|
||||||
sl_vec_push(*sl_str, c_str[i]);
|
sl_vec_push(*sl_str, c_str[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -143,18 +153,22 @@ void sl_append_c_str(sl_string* sl_str, const char* c_str) {
|
||||||
// Pointers
|
// Pointers
|
||||||
|
|
||||||
#define sl_ptr(type) \
|
#define sl_ptr(type) \
|
||||||
struct { \
|
struct \
|
||||||
|
{ \
|
||||||
type *ptr; \
|
type *ptr; \
|
||||||
int ref_count; \
|
int ref_count; \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define sl_ptr_make(raw_ptr) \
|
#define sl_ptr_make(raw_ptr) \
|
||||||
{ raw_ptr, 1 }
|
{ \
|
||||||
|
raw_ptr, 1 \
|
||||||
|
}
|
||||||
|
|
||||||
#define sl_ptr_release(smart_ptr) \
|
#define sl_ptr_release(smart_ptr) \
|
||||||
({ \
|
({ \
|
||||||
smart_ptr.ref_count--; \
|
smart_ptr.ref_count--; \
|
||||||
if (smart_ptr.ref_count <= 0) { \
|
if (smart_ptr.ref_count <= 0) \
|
||||||
|
{ \
|
||||||
free(smart_ptr.ptr); \
|
free(smart_ptr.ptr); \
|
||||||
} \
|
} \
|
||||||
})
|
})
|
||||||
|
@ -177,9 +191,11 @@ void sl_append_c_str(sl_string* sl_str, const char* c_str) {
|
||||||
void sl_read_file(const char *filename, sl_string *buffer);
|
void sl_read_file(const char *filename, sl_string *buffer);
|
||||||
|
|
||||||
#ifdef SL_IMPLEMENTATION
|
#ifdef SL_IMPLEMENTATION
|
||||||
void sl_read_file(const char *filename, sl_string *buffer) {
|
void sl_read_file(const char *filename, sl_string *buffer)
|
||||||
|
{
|
||||||
FILE *file = fopen(filename, "r");
|
FILE *file = fopen(filename, "r");
|
||||||
if (!file) {
|
if (!file)
|
||||||
|
{
|
||||||
fprintf(stderr, "Error: could not open file %s\n", filename);
|
fprintf(stderr, "Error: could not open file %s\n", filename);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -188,7 +204,8 @@ void sl_read_file(const char *filename, sl_string *buffer) {
|
||||||
size_t file_size = ftell(file);
|
size_t file_size = ftell(file);
|
||||||
fseek(file, 0, SEEK_SET);
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
for (size_t i = 0; i < file_size; i++) {
|
for (size_t i = 0; i < file_size; i++)
|
||||||
|
{
|
||||||
sl_vec_push(*buffer, fgetc(file));
|
sl_vec_push(*buffer, fgetc(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,3 +214,4 @@ void sl_read_file(const char *filename, sl_string *buffer) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // SLIBS_H
|
#endif // SLIBS_H
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue