-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharray.h
More file actions
111 lines (92 loc) · 2.07 KB
/
array.h
File metadata and controls
111 lines (92 loc) · 2.07 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef __ARRAY_H
#define __ARRAY_H
#include <stdbool.h>
#include <sys/types.h>
#include <buffer.h>
#include <kernel-types.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef buffer_t array_t;
// TODO - maybe make this a macro or something??
static inline size_t array_typesize(char sig)
{
switch (sig)
{
case T_ARRAY_BOOLEAN: return sizeof(bool);
case T_ARRAY_INTEGER: return sizeof(int);
case T_ARRAY_DOUBLE: return sizeof(double);
default: return 0;
}
}
static inline array_t * array_new()
{
return buffer_new();
}
static inline array_t * array_dup(const array_t * array)
{
return buffer_dup(array);
}
static inline size_t array_size(const array_t * array, char type)
{
// Sanity check
{
if unlikely(array_typesize(type) == 0)
{
return 0;
}
}
return buffer_size(array) / array_typesize(type);
}
static inline void array_free(array_t * array)
{
buffer_free(array);
}
static inline size_t array_read(const array_t * array, char type, off_t index, void * elems, size_t nelems)
{
// Sanity check
{
if unlikely(array_typesize(type) == 0 || index < 0)
{
return 0;
}
}
return buffer_read(array, elems, index * array_typesize(type), nelems * array_typesize(type)) / array_typesize(type);
}
static inline size_t array_write(array_t * array, char type, off_t index, const void * elems, size_t nelems)
{
// Sanity check
{
if unlikely(array_typesize(type) == 0 || index < 0)
{
return 0;
}
}
return buffer_write(array, elems, index * array_typesize(type), nelems * array_typesize(type)) / array_typesize(type);
}
static inline bool array_readindex(const array_t * array, char type, off_t index, void * elem)
{
// Sanity check
{
if unlikely(array_typesize(type) == 0 || index < 0)
{
return false;
}
}
return array_read(array, type, index, elem, 1) == 1;
}
static inline bool array_writeindex(array_t * array, char type, off_t index, const void * elem)
{
// Sanity check
{
if unlikely(array_typesize(type) == 0 || index < 0)
{
return false;
}
}
return array_write(array, type, index, elem, 1) == 1;
}
#ifdef __cplusplus
}
#endif
#endif