| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
| 3 | * | ||
| 4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "cx/iterator.h" | ||
| 30 | |||
| 31 | #include <string.h> | ||
| 32 | #include <assert.h> | ||
| 33 | |||
| 34 | 796 | static bool cx_iter_valid(const void *it) { | |
| 35 | 796 | const struct cx_iterator_s *iter = it; | |
| 36 | 796 | return iter->index < iter->elem_count; | |
| 37 | } | ||
| 38 | |||
| 39 | 51 | static void *cx_iter_current(const void *it) { | |
| 40 | 51 | const struct cx_iterator_s *iter = it; | |
| 41 | 51 | return iter->elem_handle; | |
| 42 | } | ||
| 43 | |||
| 44 | 659 | static void *cx_iter_current_ptr(const void *it) { | |
| 45 | 659 | const struct cx_iterator_s *iter = it; | |
| 46 | 659 | return *(void**)iter->elem_handle; | |
| 47 | } | ||
| 48 | |||
| 49 | 710 | static void cx_iter_next(void *it) { | |
| 50 | 710 | struct cx_iterator_s *iter = it; | |
| 51 | assert(!iter->base.remove); | ||
| 52 | 710 | iter->index++; | |
| 53 | 710 | iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; | |
| 54 | 710 | } | |
| 55 | |||
| 56 | 86 | CxIterator cxIterator(const void *array, size_t elem_size, size_t elem_count) { | |
| 57 | CxIterator iter; | ||
| 58 | |||
| 59 | 86 | iter.index = 0; | |
| 60 | 86 | iter.src_handle = (void*) array; | |
| 61 | 86 | iter.elem_handle = (void*) array; | |
| 62 | 86 | iter.elem_size = elem_size; | |
| 63 |
2/2✓ Branch 0 (2→3) taken 84 times.
✓ Branch 1 (2→4) taken 2 times.
|
86 | iter.elem_count = array == NULL ? 0 : elem_count; |
| 64 | 86 | iter.base.valid = cx_iter_valid; | |
| 65 | 86 | iter.base.current = cx_iter_current; | |
| 66 | 86 | iter.base.next = cx_iter_next; | |
| 67 | 86 | iter.base.valid_impl = NULL; | |
| 68 | 86 | iter.base.current_impl = NULL; | |
| 69 | 86 | iter.base.next_impl = NULL; | |
| 70 | 86 | iter.base.remove = false; | |
| 71 | 86 | iter.base.allow_remove = true; | |
| 72 | |||
| 73 | 86 | return iter; | |
| 74 | } | ||
| 75 | |||
| 76 | 83 | CxIterator cxIteratorPtr(const void *array, size_t elem_count) { | |
| 77 | 83 | CxIterator iter = cxIterator(array, sizeof(void*), elem_count); | |
| 78 | 83 | iter.base.current = cx_iter_current_ptr; | |
| 79 | 83 | return iter; | |
| 80 | } | ||
| 81 |