diff options
author | Dalai Felinto <dfelinto@gmail.com> | 2016-12-16 20:22:05 +0300 |
---|---|---|
committer | Dalai Felinto <dfelinto@gmail.com> | 2016-12-19 12:49:11 +0300 |
commit | 57a5f2ef443d79d4394cf028a422392f1b7c1515 (patch) | |
tree | 0fe7350af063e94cac8fcfa7720ecb5634c30789 /source/blender/blenlib | |
parent | a3fd4274cf20341c7ab6ed38f3e62adb6d83892d (diff) |
Iterator util function
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_iterator.h | 58 | ||||
-rw-r--r-- | source/blender/blenlib/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_iterator.c | 63 |
3 files changed, 123 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_iterator.h b/source/blender/blenlib/BLI_iterator.h new file mode 100644 index 00000000000..86b885f24ec --- /dev/null +++ b/source/blender/blenlib/BLI_iterator.h @@ -0,0 +1,58 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Dalai Felinto + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#ifndef __BLI_ITERATOR_H__ +#define __BLI_ITERATOR_H__ + +/** \file BLI_iterator.h + * \ingroup bli + */ + +typedef struct Iterator { + void **array; + int tot, cur; + + void *data; + int valid; +} Iterator; + +typedef void (*IteratorCb)(Iterator *iter, void *data); + +void BLI_iterator_begin(Iterator *iter, IteratorCb callback, void *data); +void BLI_iterator_next(Iterator *iter); +void BLI_iterator_end(Iterator *iter); + +#define ITER_BEGIN(callback, _data_in, _data_out) \ + { \ + Iterator iter_macro; \ + for (BLI_iterator_begin(&iter_macro, callback, _data_in); \ + iter_macro.valid; \ + BLI_iterator_next(&iter_macro)) \ + { \ + _data_out = iter_macro.data; + +#define ITER_END \ + } \ + BLI_iterator_end(&iter_macro); \ + } + +#endif /* __BLI_ITERATOR_H__ */ diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index 6e717a3ae7e..003ad9bdc10 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -47,6 +47,7 @@ set(SRC intern/BLI_ghash.c intern/BLI_heap.c intern/BLI_kdopbvh.c + intern/BLI_iterator.c intern/BLI_kdtree.c intern/BLI_linklist.c intern/BLI_memarena.c @@ -154,6 +155,7 @@ set(SRC BLI_hash_md5.h BLI_hash_mm2a.h BLI_heap.h + BLI_iterator.h BLI_jitter.h BLI_kdopbvh.h BLI_kdtree.h diff --git a/source/blender/blenlib/intern/BLI_iterator.c b/source/blender/blenlib/intern/BLI_iterator.c new file mode 100644 index 00000000000..83097deccad --- /dev/null +++ b/source/blender/blenlib/intern/BLI_iterator.c @@ -0,0 +1,63 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Dalai Felinto + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/blenlib/intern/iterator.c + * \ingroup bli + * + * Iterator defines + */ + +#include <string.h> + +#include "BLI_iterator.h" +#include "MEM_guardedalloc.h" + +void BLI_iterator_begin(Iterator *iter, IteratorCb callback, void *data) +{ + memset(iter, 0, sizeof(*iter)); + callback(iter, data); + + if (iter->tot) { + iter->cur = 0; + iter->data = iter->array[iter->cur]; + iter->valid = 1; + } +} + +void BLI_iterator_next(Iterator *iter) +{ + if (++iter->cur < iter->tot) { + iter->data = iter->array[iter->cur]; + } + else { + iter->valid = 0; + } +} + +void BLI_iterator_end(Iterator *iter) +{ + if (iter->array) { + MEM_freeN(iter->array); + } + iter->valid = 0; +} + |