2010-09-24 02:13:48 +03:00
|
|
|
/*
|
|
|
|
* array.h - Growable baseless 2D array
|
|
|
|
*
|
|
|
|
* Written 2010 by Werner Almesberger
|
|
|
|
* Copyright 2010 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
2010-09-22 23:04:43 +03:00
|
|
|
#ifndef ARRAY_H
|
|
|
|
#define ARRAY_H
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define UNDEF INT_MAX
|
|
|
|
|
|
|
|
|
|
|
|
struct array {
|
|
|
|
int min_x, max_x;
|
|
|
|
int min_y, max_y;
|
|
|
|
int min_z, max_z;
|
|
|
|
int *data; /* NULL if there are no points */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct array *new_array(void);
|
|
|
|
void free_array(struct array *a);
|
|
|
|
|
|
|
|
void set(struct array *a, int x, int y, int z);
|
|
|
|
|
|
|
|
|
|
|
|
static inline int get(const struct array *a, int x, int y)
|
|
|
|
{
|
|
|
|
return a->data[(x)-a->min_x+(a->max_x-a->min_x+1)*((y)-a->min_y)];
|
|
|
|
}
|
|
|
|
|
2010-09-24 23:56:55 +03:00
|
|
|
|
|
|
|
static inline int get_bounded(const struct array *a, int x, int y)
|
|
|
|
{
|
|
|
|
if (x < a->min_x || x > a->max_x)
|
|
|
|
return UNDEF;
|
|
|
|
if (y < a->min_y || y > a->max_y)
|
|
|
|
return UNDEF;
|
|
|
|
return get(a, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 23:04:43 +03:00
|
|
|
#endif /* ARRAY_H */
|