Line data Source code
1 : /* File: ctrl_undo_redo_iterator.inl; Copyright and License: see below */ 2 : 3 20 : static inline void ctrl_undo_redo_iterator_init ( ctrl_undo_redo_iterator_t *this_, 4 : const ctrl_undo_redo_entry_t (*ring_buf)[], 5 : uint32_t ring_buf_size, 6 : bool iterate_upwards, 7 : uint32_t current, 8 : uint32_t length ) 9 : { 10 20 : assert( ring_buf != NULL ); 11 20 : assert( ring_buf_size > 0 ); 12 20 : assert( current < ring_buf_size ); 13 20 : assert( length <= ring_buf_size ); 14 20 : (*this_).ring_buf = ring_buf; 15 20 : (*this_).ring_buf_size = ring_buf_size; 16 20 : (*this_).iterate_upwards = iterate_upwards; 17 20 : (*this_).current = current; 18 20 : (*this_).length = length; 19 20 : } 20 : 21 9 : static inline void ctrl_undo_redo_iterator_reinit ( ctrl_undo_redo_iterator_t *this_, 22 : const ctrl_undo_redo_entry_t (*ring_buf)[], 23 : uint32_t ring_buf_size, 24 : bool iterate_upwards, 25 : uint32_t current, 26 : uint32_t length ) 27 : { 28 9 : ctrl_undo_redo_iterator_init( this_, ring_buf, ring_buf_size, iterate_upwards, current, length ); 29 9 : } 30 : 31 9 : static inline void ctrl_undo_redo_iterator_init_empty ( ctrl_undo_redo_iterator_t *this_ ) 32 : { 33 9 : (*this_).ring_buf = NULL; 34 9 : (*this_).ring_buf_size = 1; 35 9 : (*this_).iterate_upwards = true; 36 9 : (*this_).current = 0; 37 9 : (*this_).length = 0; 38 9 : } 39 : 40 20 : static inline void ctrl_undo_redo_iterator_destroy ( ctrl_undo_redo_iterator_t *this_ ) 41 : { 42 20 : (*this_).ring_buf = NULL; 43 20 : } 44 : 45 80 : static inline bool ctrl_undo_redo_iterator_has_next ( const ctrl_undo_redo_iterator_t *this_ ) 46 : { 47 80 : return ( (*this_).length != 0 ); 48 : } 49 : 50 59 : static inline const ctrl_undo_redo_entry_t * ctrl_undo_redo_iterator_next ( ctrl_undo_redo_iterator_t *this_ ) 51 : { 52 59 : assert( (*this_).length > 0 ); 53 : 54 59 : const ctrl_undo_redo_entry_t * result = NULL; 55 59 : if ( (*this_).length > 0 ) 56 : { 57 59 : result = &((*(*this_).ring_buf)[(*this_).current]); 58 59 : if ( (*this_).iterate_upwards ) 59 : { 60 3 : (*this_).current = ( (*this_).current + 1 ) % (*this_).ring_buf_size; 61 : } 62 : else 63 : { 64 56 : (*this_).current = ( (*this_).current + (*this_).ring_buf_size - 1 ) % (*this_).ring_buf_size; 65 : } 66 59 : (*this_).length --; 67 : } 68 : 69 59 : return result; 70 : } 71 : 72 9 : static inline void ctrl_undo_redo_iterator_collect_statistics ( ctrl_undo_redo_iterator_t *this_, 73 : bool undo, 74 : data_stat_t *io_stat ) 75 : { 76 9 : assert( io_stat != NULL ); 77 33 : while ( ctrl_undo_redo_iterator_has_next( this_ ) ) 78 : { 79 24 : const ctrl_undo_redo_entry_t *const current_entry = ctrl_undo_redo_iterator_next( this_ ); 80 24 : ctrl_undo_redo_entry_to_statistics ( current_entry, undo /*=undo*/, false /*=err*/, io_stat ); 81 : } 82 9 : } 83 : 84 : 85 : /* 86 : Copyright 2024-2024 Andreas Warnke 87 : 88 : Licensed under the Apache License, Version 2.0 (the "License"); 89 : you may not use this file except in compliance with the License. 90 : You may obtain a copy of the License at 91 : 92 : http://www.apache.org/licenses/LICENSE-2.0 93 : 94 : Unless required by applicable law or agreed to in writing, software 95 : distributed under the License is distributed on an "AS IS" BASIS, 96 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 97 : See the License for the specific language governing permissions and 98 : limitations under the License. 99 : */