universe@850: /* universe@850: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@850: * universe@850: * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. universe@850: * universe@850: * Redistribution and use in source and binary forms, with or without universe@850: * modification, are permitted provided that the following conditions are met: universe@850: * universe@850: * 1. Redistributions of source code must retain the above copyright universe@850: * notice, this list of conditions and the following disclaimer. universe@850: * universe@850: * 2. Redistributions in binary form must reproduce the above copyright universe@850: * notice, this list of conditions and the following disclaimer in the universe@850: * documentation and/or other materials provided with the distribution. universe@850: * universe@850: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@850: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@850: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@850: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@850: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@850: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@850: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@850: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@850: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@850: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@850: * POSSIBILITY OF SUCH DAMAGE. universe@850: */ universe@850: universe@850: #include "cx/iterator.h" universe@850: universe@850: #include universe@850: universe@850: static bool cx_iter_valid(void const *it) { universe@850: struct cx_iterator_s const *iter = it; universe@850: return iter->index < iter->elem_count; universe@850: } universe@850: universe@850: static void *cx_iter_current(void const *it) { universe@850: struct cx_iterator_s const *iter = it; universe@850: return iter->elem_handle; universe@850: } universe@850: universe@850: static void cx_iter_next_fast(void *it) { universe@850: struct cx_iterator_base_s *itbase = it; universe@850: if (itbase->remove) { universe@850: struct cx_mut_iterator_s *iter = it; universe@850: itbase->remove = false; universe@850: iter->elem_count--; universe@850: // only move the last element when we are not currently aiming universe@850: // at the last element already universe@850: if (iter->index < iter->elem_count) { universe@850: void *last = ((char *) iter->src_handle) universe@850: + iter->elem_count * iter->elem_size; universe@850: memcpy(iter->elem_handle, last, iter->elem_size); universe@850: } universe@850: } else { universe@850: struct cx_iterator_s *iter = it; universe@850: iter->index++; universe@850: iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; universe@850: } universe@850: } universe@850: universe@850: static void cx_iter_next_slow(void *it) { universe@850: struct cx_iterator_base_s *itbase = it; universe@850: if (itbase->remove) { universe@850: struct cx_mut_iterator_s *iter = it; universe@850: itbase->remove = false; universe@850: iter->elem_count--; universe@850: universe@850: // number of elements to move universe@850: size_t remaining = iter->elem_count - iter->index; universe@850: if (remaining > 0) { universe@850: memmove( universe@850: iter->elem_handle, universe@850: ((char *) iter->elem_handle) + iter->elem_size, universe@850: remaining * iter->elem_size universe@850: ); universe@850: } universe@850: } else { universe@850: struct cx_iterator_s *iter = it; universe@850: iter->index++; universe@850: iter->elem_handle = ((char *) iter->elem_handle) + iter->elem_size; universe@850: } universe@850: } universe@850: universe@850: CxMutIterator cxIterator( universe@850: void *array, universe@850: size_t elem_size, universe@850: size_t elem_count, universe@850: bool remove_keeps_order universe@850: ) { universe@850: CxMutIterator iter; universe@850: universe@850: iter.index = 0; universe@850: iter.src_handle = array; universe@850: iter.elem_handle = array; universe@850: iter.elem_size = elem_size; universe@850: iter.elem_count = array == NULL ? 0 : elem_count; universe@850: iter.base.valid = cx_iter_valid; universe@850: iter.base.current = cx_iter_current; universe@850: iter.base.next = remove_keeps_order ? cx_iter_next_slow : cx_iter_next_fast; universe@850: iter.base.remove = false; universe@850: iter.base.mutating = true; universe@850: universe@850: return iter; universe@850: }