Root/
| 1 | /* |
| 2 | * array.h - Growable baseless 2D array |
| 3 | * |
| 4 | * Written 2010 by Werner Almesberger |
| 5 | * Copyright 2010 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #ifndef ARRAY_H |
| 14 | #define ARRAY_H |
| 15 | |
| 16 | #include <limits.h> |
| 17 | |
| 18 | |
| 19 | #define UNDEF INT_MAX |
| 20 | |
| 21 | |
| 22 | struct array { |
| 23 | int min_x, max_x; |
| 24 | int min_y, max_y; |
| 25 | int min_z, max_z; |
| 26 | int *data; /* NULL if there are no points */ |
| 27 | }; |
| 28 | |
| 29 | |
| 30 | struct array *new_array(void); |
| 31 | void free_array(struct array *a); |
| 32 | |
| 33 | void set(struct array *a, int x, int y, int z); |
| 34 | |
| 35 | |
| 36 | static inline int get(const struct array *a, int x, int y) |
| 37 | { |
| 38 | return a->data[(x)-a->min_x+(a->max_x-a->min_x+1)*((y)-a->min_y)]; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | static inline int get_bounded(const struct array *a, int x, int y) |
| 43 | { |
| 44 | if (x < a->min_x || x > a->max_x) |
| 45 | return UNDEF; |
| 46 | if (y < a->min_y || y > a->max_y) |
| 47 | return UNDEF; |
| 48 | return get(a, x, y); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | #endif /* !ARRAY_H */ |
| 53 |
Branches:
master
