Thu, 23 May 2024 18:21:36 +0200
fix documentation
relates to #389
universe@850 | 1 | /* |
universe@850 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@850 | 3 | * |
universe@850 | 4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. |
universe@850 | 5 | * |
universe@850 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@850 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@850 | 8 | * |
universe@850 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@850 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@850 | 11 | * |
universe@850 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@850 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@850 | 14 | * documentation and/or other materials provided with the distribution. |
universe@850 | 15 | * |
universe@850 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@850 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@850 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@850 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@850 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@850 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@850 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@850 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@850 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@850 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@850 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@850 | 27 | */ |
universe@850 | 28 | |
universe@850 | 29 | #include "cx/iterator.h" |
universe@850 | 30 | |
universe@850 | 31 | #include <string.h> |
universe@850 | 32 | |
universe@850 | 33 | static bool cx_iter_valid(void const *it) { |
universe@850 | 34 | struct cx_iterator_s const *iter = it; |
universe@850 | 35 | return iter->index < iter->elem_count; |
universe@850 | 36 | } |
universe@850 | 37 | |
universe@850 | 38 | static void *cx_iter_current(void const *it) { |
universe@850 | 39 | struct cx_iterator_s const *iter = it; |
universe@850 | 40 | return iter->elem_handle; |
universe@850 | 41 | } |
universe@850 | 42 | |
universe@850 | 43 | static void cx_iter_next_fast(void *it) { |
universe@850 | 44 | struct cx_iterator_base_s *itbase = it; |
universe@850 | 45 | if (itbase->remove) { |
universe@850 | 46 | struct cx_mut_iterator_s *iter = it; |
universe@850 | 47 | itbase->remove = false; |
universe@850 | 48 | iter->elem_count--; |
universe@850 | 49 | // only move the last element when we are not currently aiming |
universe@850 | 50 | // at the last element already |
universe@850 | 51 | if (iter->index < iter->elem_count) { |
universe@850 | 52 | void *last = ((char *) iter->src_handle) |
universe@850 | 53 | + iter->elem_count * iter->elem_size; |
universe@850 | 54 | memcpy(iter->elem_handle, last, iter->elem_size); |
universe@850 | 55 | } |
universe@850 | 56 | } else { |
universe@850 | 57 | struct cx_iterator_s *iter = it; |
universe@850 | 58 | iter->index++; |
universe@850 | 59 | iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; |
universe@850 | 60 | } |
universe@850 | 61 | } |
universe@850 | 62 | |
universe@850 | 63 | static void cx_iter_next_slow(void *it) { |
universe@850 | 64 | struct cx_iterator_base_s *itbase = it; |
universe@850 | 65 | if (itbase->remove) { |
universe@850 | 66 | struct cx_mut_iterator_s *iter = it; |
universe@850 | 67 | itbase->remove = false; |
universe@850 | 68 | iter->elem_count--; |
universe@850 | 69 | |
universe@850 | 70 | // number of elements to move |
universe@850 | 71 | size_t remaining = iter->elem_count - iter->index; |
universe@850 | 72 | if (remaining > 0) { |
universe@850 | 73 | memmove( |
universe@850 | 74 | iter->elem_handle, |
universe@850 | 75 | ((char *) iter->elem_handle) + iter->elem_size, |
universe@850 | 76 | remaining * iter->elem_size |
universe@850 | 77 | ); |
universe@850 | 78 | } |
universe@850 | 79 | } else { |
universe@850 | 80 | struct cx_iterator_s *iter = it; |
universe@850 | 81 | iter->index++; |
universe@850 | 82 | iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; |
universe@850 | 83 | } |
universe@850 | 84 | } |
universe@850 | 85 | |
universe@851 | 86 | CxMutIterator cxMutIterator( |
universe@850 | 87 | void *array, |
universe@850 | 88 | size_t elem_size, |
universe@850 | 89 | size_t elem_count, |
universe@850 | 90 | bool remove_keeps_order |
universe@850 | 91 | ) { |
universe@850 | 92 | CxMutIterator iter; |
universe@850 | 93 | |
universe@850 | 94 | iter.index = 0; |
universe@850 | 95 | iter.src_handle = array; |
universe@850 | 96 | iter.elem_handle = array; |
universe@850 | 97 | iter.elem_size = elem_size; |
universe@850 | 98 | iter.elem_count = array == NULL ? 0 : elem_count; |
universe@850 | 99 | iter.base.valid = cx_iter_valid; |
universe@850 | 100 | iter.base.current = cx_iter_current; |
universe@850 | 101 | iter.base.next = remove_keeps_order ? cx_iter_next_slow : cx_iter_next_fast; |
universe@850 | 102 | iter.base.remove = false; |
universe@850 | 103 | iter.base.mutating = true; |
universe@850 | 104 | |
universe@850 | 105 | return iter; |
universe@850 | 106 | } |
universe@851 | 107 | |
universe@851 | 108 | CxIterator cxIterator( |
universe@851 | 109 | void const *array, |
universe@851 | 110 | size_t elem_size, |
universe@851 | 111 | size_t elem_count |
universe@851 | 112 | ) { |
universe@851 | 113 | CxMutIterator iter = cxMutIterator((void*)array, elem_size, elem_count, false); |
universe@851 | 114 | iter.base.mutating = false; |
universe@851 | 115 | |
universe@851 | 116 | // we know the iterators share the same memory layout |
universe@851 | 117 | CxIterator ret; |
universe@851 | 118 | memcpy(&ret, &iter, sizeof(CxIterator)); |
universe@851 | 119 | return ret; |
universe@851 | 120 | } |