summaryrefslogtreecommitdiffhomepage
path: root/include/stc/carr2.h
blob: 439a5c2d401c253ae73a4d28c24ed238ee23fbc0 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* MIT License
 *
 * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef CARR2_H_INCLUDED
#define CARR2_H_INCLUDED
#include "ccommon.h"
#include "forward.h"
#include <stdlib.h>
#endif
/*
// carr2- 2D dynamic array in one memory block with easy indexing.
#define i_val int
#include <stc/carr2.h>
#include <stdio.h>

int main() {
    int w = 7, h = 5;
    c_autovar (carr2_int image = carr2_int_init(w, h), carr2_int_del(&image))
    {
        int *dat = carr2_int_data(&image);
        for (int i = 0; i < carr2_int_size(image); ++i)
            dat[i] = i;

        for (int x = 0; x < image.xdim; ++x)
            for (int y = 0; y < image.ydim; ++y)
                printf(" %d", image.data[x][y]);
        puts("\n");

        c_foreach (i, carr2_int, image)
            printf(" %d", *i.ref);
        puts("");
    }
}
*/

#ifndef i_prefix
#define i_prefix carr2_
#endif
#include "template.h"

#ifndef i_fwd
_cx_deftypes(_c_carr2_types, _cx_self, i_val);
#endif

STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value);
STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* storage);
STC_API _cx_self _cx_memb(_clone)(_cx_self src);
STC_API _cx_value* _cx_memb(_release)(_cx_self* self);
STC_API void _cx_memb(_del)(_cx_self* self);

STC_INLINE _cx_self _cx_memb(_init)(size_t xdim, size_t ydim) {
    return _cx_memb(_with_storage)(xdim, ydim, c_alloc_n(_cx_value, xdim*ydim));
}
STC_INLINE size_t _cx_memb(_size)(_cx_self arr)
    { return arr.xdim*arr.ydim; }

STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self)
    { return *self->data; }

STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t y) {
    assert(x < self->xdim && y < self->ydim);
    return *self->data + self->ydim*x + y;
}

STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) {
    return self->ydim*x + y;
}

STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
    if (self->data == other.data) return;
    _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
}

STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
    { return c_make(_cx_iter){*self->data}; }

STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
    { return c_make(_cx_iter){*self->data + self->xdim*self->ydim}; }

STC_INLINE void _cx_memb(_next)(_cx_iter* it)
    { ++it->ref; }

/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp)

STC_DEF _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* block) {
    _cx_self _arr = {c_alloc_n(_cx_value*, xdim), xdim, ydim};
    for (size_t x = 0; x < xdim; ++x, block += ydim)
        _arr.data[x] = block;
    return _arr;
}

STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) {
    _cx_self _arr = _cx_memb(_init)(xdim, ydim);
    for (_cx_value* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p)
        *p = value;
    return _arr;
}

STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) {
    _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim);
    for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(src); p != e; ++p, ++q)
        *p = i_valfrom(i_valto(q));
    return _arr;
}

STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) {
    _cx_value *values = self->data[0];
    c_free(self->data);
    self->data = NULL;
    return values;
}

STC_DEF void _cx_memb(_del)(_cx_self* self) {
    if (!self->data) return;
    for (_cx_value* p = self->data[0], *e = p + _cx_memb(_size)(*self); p != e; ++p)
        i_valdel(p);
    c_free(self->data[0]); /* values */
    c_free(self->data);    /* pointers */
}

#endif
#include "template.h"