Line data Source code
1 : /* File: universal_array_index_sorter.inl; Copyright and License: see below */ 2 : 3 : #include "u8/u8_log.h" 4 : #include <assert.h> 5 : 6 2 : static inline void universal_array_index_sorter_init( universal_array_index_sorter_t *this_ ) 7 : { 8 2 : (*this_).entries_count = 0; 9 2 : } 10 : 11 1 : static inline void universal_array_index_sorter_reinit( universal_array_index_sorter_t *this_ ) 12 : { 13 1 : (*this_).entries_count = 0; 14 1 : } 15 : 16 2 : static inline void universal_array_index_sorter_destroy( universal_array_index_sorter_t *this_ ) 17 : { 18 2 : } 19 : 20 2053 : static inline u8_error_t universal_array_index_sorter_insert( universal_array_index_sorter_t *this_, 21 : uint32_t array_index, 22 : int64_t weight ) 23 : { 24 2053 : assert( (*this_).entries_count <= UNIVERSAL_ARRAY_INDEX_SORTER_MAX_ARRAY_SIZE ); 25 : u8_error_t result; 26 2053 : if ( (*this_).entries_count < UNIVERSAL_ARRAY_INDEX_SORTER_MAX_ARRAY_SIZE ) 27 : { 28 2052 : bool already_inserted = false; 29 2098185 : for ( uint32_t sorted_index = (*this_).entries_count; (sorted_index > 0) && ( ! already_inserted ); sorted_index -- ) 30 : { 31 2096133 : if ( weight < (*this_).weights[sorted_index-1] ) 32 : { 33 : /* shift */ 34 2096131 : (*this_).entries[sorted_index] = (*this_).entries[sorted_index-1]; 35 2096131 : (*this_).weights[sorted_index] = (*this_).weights[sorted_index-1]; 36 : } 37 : else 38 : { 39 : /* insert */ 40 2 : (*this_).entries[sorted_index] = array_index; 41 2 : (*this_).weights[sorted_index] = weight; 42 2 : already_inserted = true; 43 : } 44 : } 45 2052 : if ( ! already_inserted ) 46 : { 47 2050 : (*this_).entries[0] = array_index; 48 2050 : (*this_).weights[0] = weight; 49 : } 50 2052 : (*this_).entries_count ++; 51 2052 : result = U8_ERROR_NONE; 52 : } 53 : else 54 : { 55 1 : result = U8_ERROR_ARRAY_BUFFER_EXCEEDED; 56 : } 57 2053 : return result; 58 : } 59 : 60 2054 : static inline uint32_t universal_array_index_sorter_get_count( const universal_array_index_sorter_t *this_ ) 61 : { 62 2054 : return (*this_).entries_count; 63 : } 64 : 65 2054 : static inline uint32_t universal_array_index_sorter_get_array_index( const universal_array_index_sorter_t *this_, uint32_t sort_index ) 66 : { 67 2054 : assert( (*this_).entries_count <= UNIVERSAL_ARRAY_INDEX_SORTER_MAX_ARRAY_SIZE ); 68 2054 : assert( sort_index <= (*this_).entries_count ); 69 2054 : return (*this_).entries[sort_index]; 70 : } 71 : 72 : 73 : /* 74 : Copyright 2017-2024 Andreas Warnke 75 : 76 : Licensed under the Apache License, Version 2.0 (the "License"); 77 : you may not use this file except in compliance with the License. 78 : You may obtain a copy of the License at 79 : 80 : http://www.apache.org/licenses/LICENSE-2.0 81 : 82 : Unless required by applicable law or agreed to in writing, software 83 : distributed under the License is distributed on an "AS IS" BASIS, 84 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 85 : See the License for the specific language governing permissions and 86 : limitations under the License. 87 : */