summaryrefslogtreecommitdiff
path: root/darray.h
blob: 3cf7e163806f5b2a784f407ed60f22949bfe5741 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* Some thx goes to http://c.learncodethehardway.org/book/ex34.html */
#ifndef __LIBADT_DARRAY_H
#define __LIBADT_DARRAY_H

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#include "debug.h"
#include "mmm.h"

#define DEFAULT_EXPAND_RATE 10

typedef struct darray {
	int    end;    //element amount. end <= max
	int    max;    //maximal element number
	size_t size;   //single element size
	size_t expand; //expand size
	void **data;
} darray;

darray* darr_create(ssize_t data_size, ssize_t init_size);
void    darr_destroy(darray *da);
void    darr_clear(darray *da);
void    darr_clear_idx(darray *da, int idx);
int     darr_expand(darray *da);
int     darr_resize(darray *da, ssize_t size);
int     darr_contract(darray *da);
int     darr_push(darray *da, void *val);
void*   darr_pop(darray *da);
void    darr_clear_destroy(darray *da);
int     darr_set(darray *da, int idx, void *val);
void*   darr_get(darray *da, int idx);
void*   darr_remove(darray *da, int idx);
void*   darr_new(darray *da);
int     darr_isidx(darray *da, int idx);

#define darr_last(A)  ((A)->data[(A)->end - 1])
#define darr_first(A) ((A)->data[0])
#define darr_end(A)   ((A)->end)
#define darr_count(A) darr_end(A)
#define darr_max(A)   ((A)->max)
#define darr_free(E)  free((E))

/*
when do set it just overwrite data without checking if there is some or not
if set more then max then no error and no resize happens
when get element if error then NULL if value NULL then NULL how distinguish
*/

#endif