src/iterator.c

Thu, 23 May 2024 19:29:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 May 2024 19:29:14 +0200
changeset 853
d4baf4dd55c3
parent 851
adb4e0737c33
child 854
fe0d69d72bcd
permissions
-rw-r--r--

simplify iterator structures

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@853 44 struct cx_iterator_s *iter = it;
universe@853 45 if (iter->remove) {
universe@853 46 iter->remove = false;
universe@850 47 iter->elem_count--;
universe@850 48 // only move the last element when we are not currently aiming
universe@850 49 // at the last element already
universe@850 50 if (iter->index < iter->elem_count) {
universe@853 51 void *last = ((char *) iter->src_handle.m)
universe@850 52 + iter->elem_count * iter->elem_size;
universe@850 53 memcpy(iter->elem_handle, last, iter->elem_size);
universe@850 54 }
universe@850 55 } else {
universe@850 56 iter->index++;
universe@850 57 iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size;
universe@850 58 }
universe@850 59 }
universe@850 60
universe@850 61 static void cx_iter_next_slow(void *it) {
universe@853 62 struct cx_iterator_s *iter = it;
universe@853 63 if (iter->remove) {
universe@853 64 iter->remove = false;
universe@850 65 iter->elem_count--;
universe@850 66
universe@850 67 // number of elements to move
universe@850 68 size_t remaining = iter->elem_count - iter->index;
universe@850 69 if (remaining > 0) {
universe@850 70 memmove(
universe@850 71 iter->elem_handle,
universe@850 72 ((char *) iter->elem_handle) + iter->elem_size,
universe@850 73 remaining * iter->elem_size
universe@850 74 );
universe@850 75 }
universe@850 76 } else {
universe@850 77 iter->index++;
universe@850 78 iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size;
universe@850 79 }
universe@850 80 }
universe@850 81
universe@853 82 CxIterator cxMutIterator(
universe@850 83 void *array,
universe@850 84 size_t elem_size,
universe@850 85 size_t elem_count,
universe@850 86 bool remove_keeps_order
universe@850 87 ) {
universe@853 88 CxIterator iter;
universe@850 89
universe@850 90 iter.index = 0;
universe@853 91 iter.src_handle.m = array;
universe@850 92 iter.elem_handle = array;
universe@850 93 iter.elem_size = elem_size;
universe@850 94 iter.elem_count = array == NULL ? 0 : elem_count;
universe@853 95 iter.valid = cx_iter_valid;
universe@853 96 iter.current = cx_iter_current;
universe@853 97 iter.next = remove_keeps_order ? cx_iter_next_slow : cx_iter_next_fast;
universe@853 98 iter.remove = false;
universe@853 99 iter.mutating = true;
universe@850 100
universe@850 101 return iter;
universe@850 102 }
universe@851 103
universe@851 104 CxIterator cxIterator(
universe@851 105 void const *array,
universe@851 106 size_t elem_size,
universe@851 107 size_t elem_count
universe@851 108 ) {
universe@853 109 CxIterator iter = cxMutIterator((void*)array, elem_size, elem_count, false);
universe@853 110 iter.mutating = false;
universe@853 111 return iter;
universe@851 112 }

mercurial