sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget%/translations/zh_CN/core-api/min_heapmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/zh_TW/core-api/min_heapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/it_IT/core-api/min_heapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/ja_JP/core-api/min_heapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/ko_KR/core-api/min_heapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hPortuguese (Brazilian)}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/pt_BR/core-api/min_heapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/sp_SP/core-api/min_heapmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhcomment)}(h SPDX-License-Identifier: GPL-2.0h]h SPDX-License-Identifier: GPL-2.0}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhh?/var/lib/git/docbuild/linux/Documentation/core-api/min_heap.rsthKubhsection)}(hhh](htitle)}(h Min Heap APIh]h Min Heap API}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh field_list)}(hhh]hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(h%Kuan-Wei Chiu h]h paragraph)}(h$Kuan-Wei Chiu h](hKuan-Wei Chiu <}(hhhhhNhNubh reference)}(hvisitorckw@gmail.comh]hvisitorckw@gmail.com}(hj hhhNhNubah}(h]h ]h"]h$]h&]refurimailto:visitorckw@gmail.comuh1jhhubh>}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h Introductionh]h Introduction}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj5hhhhhK ubh)}(hXThe Min Heap API provides a set of functions and macros for managing min-heaps in the Linux kernel. A min-heap is a binary tree structure where the value of each node is less than or equal to the values of its children, ensuring that the smallest element is always at the root.h]hXThe Min Heap API provides a set of functions and macros for managing min-heaps in the Linux kernel. A min-heap is a binary tree structure where the value of each node is less than or equal to the values of its children, ensuring that the smallest element is always at the root.}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hj5hhubh)}(hThis document provides a guide to the Min Heap API, detailing how to define and use min-heaps. Users should not directly call functions with **__min_heap_*()** prefixes, but should instead use the provided macro wrappers.h](hThis document provides a guide to the Min Heap API, detailing how to define and use min-heaps. Users should not directly call functions with }(hjThhhNhNubhstrong)}(h**__min_heap_*()**h]h__min_heap_*()}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjTubh> prefixes, but should instead use the provided macro wrappers.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj5hhubh)}(hXIn addition to the standard version of the functions, the API also includes a set of inline versions for performance-critical scenarios. These inline functions have the same names as their non-inline counterparts but include an **_inline** suffix. For example, **__min_heap_init_inline** and its corresponding macro wrapper **min_heap_init_inline**. The inline versions allow custom comparison and swap functions to be called directly, rather than through indirect function calls. This can significantly reduce overhead, especially when CONFIG_MITIGATION_RETPOLINE is enabled, as indirect function calls become more expensive. As with the non-inline versions, it is important to use the macro wrappers for inline functions instead of directly calling the functions themselves.h](hIn addition to the standard version of the functions, the API also includes a set of inline versions for performance-critical scenarios. These inline functions have the same names as their non-inline counterparts but include an }(hjvhhhNhNubj])}(h **_inline**h]h_inline}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjvubh suffix. For example, }(hjvhhhNhNubj])}(h**__min_heap_init_inline**h]h__min_heap_init_inline}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjvubh% and its corresponding macro wrapper }(hjvhhhNhNubj])}(h**min_heap_init_inline**h]hmin_heap_init_inline}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjvubhX. The inline versions allow custom comparison and swap functions to be called directly, rather than through indirect function calls. This can significantly reduce overhead, especially when CONFIG_MITIGATION_RETPOLINE is enabled, as indirect function calls become more expensive. As with the non-inline versions, it is important to use the macro wrappers for inline functions instead of directly calling the functions themselves.}(hjvhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj5hhubeh}(h] introductionah ]h"] introductionah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(hData Structuresh]hData Structures}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK"ubh)}(hhh](h)}(hMin-Heap Definitionh]hMin-Heap Definition}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK%ubh)}(hThe core data structure for representing a min-heap is defined using the **MIN_HEAP_PREALLOCATED** and **DEFINE_MIN_HEAP** macros. These macros allow you to define a min-heap with a preallocated buffer or dynamically allocated memory.h](hIThe core data structure for representing a min-heap is defined using the }(hjhhhNhNubj])}(h**MIN_HEAP_PREALLOCATED**h]hMIN_HEAP_PREALLOCATED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh and }(hjhhhNhNubj])}(h**DEFINE_MIN_HEAP**h]hDEFINE_MIN_HEAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubhp macros. These macros allow you to define a min-heap with a preallocated buffer or dynamically allocated memory.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK'hjhhubh)}(hExample:h]hExample:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hjhhubh literal_block)}(hX#define MIN_HEAP_PREALLOCATED(_type, _name, _nr) struct _name { size_t nr; /* Number of elements in the heap */ size_t size; /* Maximum number of elements that can be held */ _type *data; /* Pointer to the heap data */ _type preallocated[_nr]; /* Static preallocated array */ } #define DEFINE_MIN_HEAP(_type, _name) MIN_HEAP_PREALLOCATED(_type, _name, 0)h]hX#define MIN_HEAP_PREALLOCATED(_type, _name, _nr) struct _name { size_t nr; /* Number of elements in the heap */ size_t size; /* Maximum number of elements that can be held */ _type *data; /* Pointer to the heap data */ _type preallocated[_nr]; /* Static preallocated array */ } #define DEFINE_MIN_HEAP(_type, _name) MIN_HEAP_PREALLOCATED(_type, _name, 0)}hj&sbah}(h]h ]h"]h$]h&]hhƌforcelanguagechighlight_args}uh1j$hhhK.hjhhubh)}(hXA typical heap structure will include a counter for the number of elements (`nr`), the maximum capacity of the heap (`size`), and a pointer to an array of elements (`data`). Optionally, you can specify a static array for preallocated heap storage using **MIN_HEAP_PREALLOCATED**.h](hLA typical heap structure will include a counter for the number of elements (}(hj9hhhNhNubhtitle_reference)}(h`nr`h]hnr}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj9ubh%), the maximum capacity of the heap (}(hj9hhhNhNubjB)}(h`size`h]hsize}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj9ubh*), and a pointer to an array of elements (}(hj9hhhNhNubjB)}(h`data`h]hdata}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj9ubhR). Optionally, you can specify a static array for preallocated heap storage using }(hj9hhhNhNubj])}(h**MIN_HEAP_PREALLOCATED**h]hMIN_HEAP_PREALLOCATED}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj9ubh.}(hj9hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK:hjhhubeh}(h]min-heap-definitionah ]h"]min-heap definitionah$]h&]uh1hhjhhhhhK%ubh)}(hhh](h)}(hMin Heap Callbacksh]hMin Heap Callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK@ubh)}(hThe **struct min_heap_callbacks** provides customization options for ordering elements in the heap and swapping them. It contains two function pointers:h](hThe }(hjhhhNhNubj])}(h**struct min_heap_callbacks**h]hstruct min_heap_callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubhw provides customization options for ordering elements in the heap and swapping them. It contains two function pointers:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKBhjhhubj%)}(hstruct min_heap_callbacks { bool (*less)(const void *lhs, const void *rhs, void *args); void (*swp)(void *lhs, void *rhs, void *args); };h]hstruct min_heap_callbacks { bool (*less)(const void *lhs, const void *rhs, void *args); void (*swp)(void *lhs, void *rhs, void *args); };}hjsbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKEhjhhubh bullet_list)}(hhh](h list_item)}(hL**less** is the comparison function used to establish the order of elements.h]h)}(hjh](j])}(h**less**h]hless}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubhD is the comparison function used to establish the order of elements.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKLhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h**swp** is a function for swapping elements in the heap. If swp is set to NULL, the default swap function will be used, which swaps the elements based on their size h]h)}(h**swp** is a function for swapping elements in the heap. If swp is set to NULL, the default swap function will be used, which swaps the elements based on their sizeh](j])}(h**swp**h]hswp}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh is a function for swapping elements in the heap. If swp is set to NULL, the default swap function will be used, which swaps the elements based on their size}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]bullet-uh1jhhhKLhjhhubeh}(h]min-heap-callbacksah ]h"]min heap callbacksah$]h&]uh1hhjhhhhhK@ubeh}(h]data-structuresah ]h"]data structuresah$]h&]uh1hhhhhhhhK"ubh)}(hhh](h)}(hMacro Wrappersh]hMacro Wrappers}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjChhhhhKQubh)}(hThe following macro wrappers are provided for interacting with the heap in a user-friendly manner. Each macro corresponds to a function that operates on the heap, and they abstract away direct calls to internal functions.h]hThe following macro wrappers are provided for interacting with the heap in a user-friendly manner. Each macro corresponds to a function that operates on the heap, and they abstract away direct calls to internal functions.}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKShjChhubh)}(h>Each macro accepts various parameters that are detailed below.h]h>Each macro accepts various parameters that are detailed below.}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKWhjChhubh)}(hhh](h)}(hHeap Initializationh]hHeap Initialization}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhjphhhhhKZubj%)}(h min_heap_init(heap, data, size);h]h min_heap_init(heap, data, size);}hjsbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhK\hjphhubj)}(hhh](j)}(h@**heap**: A pointer to the min-heap structure to be initialized.h]h)}(hjh](j])}(h**heap**h]hheap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh8: A pointer to the min-heap structure to be initialized.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK`hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h**data**: A pointer to the buffer where the heap elements will be stored. If `NULL`, the preallocated buffer within the heap structure will be used.h]h)}(h**data**: A pointer to the buffer where the heap elements will be stored. If `NULL`, the preallocated buffer within the heap structure will be used.h](j])}(h**data**h]hdata}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubhE: A pointer to the buffer where the heap elements will be stored. If }(hjhhhNhNubjB)}(h`NULL`h]hNULL}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubhA, the preallocated buffer within the heap structure will be used.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKahjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h<**size**: The maximum number of elements the heap can hold. h]h)}(h;**size**: The maximum number of elements the heap can hold.h](j])}(h**size**h]hsize}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh3: The maximum number of elements the heap can hold.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKchjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhK`hjphhubh)}(hThis macro initializes the heap, setting its initial state. If `data` is `NULL`, the preallocated memory inside the heap structure will be used for storage. Otherwise, the user-provided buffer is used. The operation is **O(1)**.h](h?This macro initializes the heap, setting its initial state. If }(hjhhhNhNubjB)}(h`data`h]hdata}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh is }(hjhhhNhNubjB)}(h`NULL`h]hNULL}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh, the preallocated memory inside the heap structure will be used for storage. Otherwise, the user-provided buffer is used. The operation is }(hjhhhNhNubj])}(h**O(1)**h]hO(1)}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKehjphhubh)}(h:**Inline Version:** min_heap_init_inline(heap, data, size)h](j])}(h**Inline Version:**h]hInline Version:}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj`ubh' min_heap_init_inline(heap, data, size)}(hj`hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKihjphhubeh}(h]heap-initializationah ]h"]heap initializationah$]h&]uh1hhjChhhhhKZubh)}(hhh](h)}(hAccessing the Top Elementh]hAccessing the Top Element}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKlubj%)}(helement = min_heap_peek(heap);h]helement = min_heap_peek(heap);}hjsbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKnhjhhubj)}(hhh]j)}(hQ**heap**: A pointer to the min-heap from which to retrieve the smallest element. h]h)}(hP**heap**: A pointer to the min-heap from which to retrieve the smallest element.h](j])}(h**heap**h]hheap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubhH: A pointer to the min-heap from which to retrieve the smallest element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKrhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubah}(h]h ]h"]h$]h&]j1j2uh1jhhhKrhjhhubh)}(hThis macro returns a pointer to the smallest element (the root) of the heap, or `NULL` if the heap is empty. The operation is **O(1)**.h](hPThis macro returns a pointer to the smallest element (the root) of the heap, or }(hjhhhNhNubjB)}(h`NULL`h]hNULL}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh( if the heap is empty. The operation is }(hjhhhNhNubj])}(h**O(1)**h]hO(1)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKuhjhhubh)}(h.**Inline Version:** min_heap_peek_inline(heap)h](j])}(h**Inline Version:**h]hInline Version:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh min_heap_peek_inline(heap)}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKxhjhhubeh}(h]accessing-the-top-elementah ]h"]accessing the top elementah$]h&]uh1hhjChhhhhKlubh)}(hhh](h)}(hHeap Insertionh]hHeap Insertion}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj)hhhhhK{ubj%)}(h8success = min_heap_push(heap, element, callbacks, args);h]h8success = min_heap_push(heap, element, callbacks, args);}hj:sbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhK}hj)hhubj)}(hhh](j)}(hN**heap**: A pointer to the min-heap into which the element should be inserted.h]h)}(hjNh](j])}(h**heap**h]hheap}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjPubhF: A pointer to the min-heap into which the element should be inserted.}(hjPhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjLubah}(h]h ]h"]h$]h&]uh1jhjIhhhhhNubj)}(hC**element**: A pointer to the element to be inserted into the heap.h]h)}(hjsh](j])}(h **element**h]helement}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjuubh8: A pointer to the element to be inserted into the heap.}(hjuhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjqubah}(h]h ]h"]h$]h&]uh1jhjIhhhhhNubj)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h]h)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h](j])}(h **callbacks**h]h callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh: A pointer to a }(hjhhhNhNubjB)}(h`struct min_heap_callbacks`h]hstruct min_heap_callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh providing the }(hjhhhNhNubjB)}(h`less`h]hless}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh and }(hjhhhNhNubjB)}(h`swp`h]hswp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh functions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjIhhhhhNubj)}(hG**args**: Optional arguments passed to the `less` and `swp` functions. h]h)}(hF**args**: Optional arguments passed to the `less` and `swp` functions.h](j])}(h**args**h]hargs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh#: Optional arguments passed to the }(hjhhhNhNubjB)}(h`less`h]hless}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh and }(hjhhhNhNubjB)}(h`swp`h]hswp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh functions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjIhhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhKhj)hhubh)}(hThis macro inserts an element into the heap. It returns `true` if the insertion was successful and `false` if the heap is full. The operation is **O(log n)**.h](h8This macro inserts an element into the heap. It returns }(hjBhhhNhNubjB)}(h`true`h]htrue}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjBubh% if the insertion was successful and }(hjBhhhNhNubjB)}(h`false`h]hfalse}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjBubh' if the heap is full. The operation is }(hjBhhhNhNubj])}(h **O(log n)**h]hO(log n)}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjBubh.}(hjBhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj)hhubh)}(hH**Inline Version:** min_heap_push_inline(heap, element, callbacks, args)h](j])}(h**Inline Version:**h]hInline Version:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh5 min_heap_push_inline(heap, element, callbacks, args)}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj)hhubeh}(h]heap-insertionah ]h"]heap insertionah$]h&]uh1hhjChhhhhK{ubh)}(hhh](h)}(h Heap Removalh]h Heap Removal}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubj%)}(h.success = min_heap_pop(heap, callbacks, args);h]h.success = min_heap_pop(heap, callbacks, args);}hjsbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhjhhubj)}(hhh](j)}(hN**heap**: A pointer to the min-heap from which to remove the smallest element.h]h)}(hjh](j])}(h**heap**h]hheap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubhF: A pointer to the min-heap from which to remove the smallest element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h]h)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h](j])}(h **callbacks**h]h callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh: A pointer to a }(hjhhhNhNubjB)}(h`struct min_heap_callbacks`h]hstruct min_heap_callbacks}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh providing the }(hjhhhNhNubjB)}(h`less`h]hless}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh and }(hjhhhNhNubjB)}(h`swp`h]hswp}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh functions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hG**args**: Optional arguments passed to the `less` and `swp` functions. h]h)}(hF**args**: Optional arguments passed to the `less` and `swp` functions.h](j])}(h**args**h]hargs}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjRubh#: Optional arguments passed to the }(hjRhhhNhNubjB)}(h`less`h]hless}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjRubh and }(hjRhhhNhNubjB)}(h`swp`h]hswp}(hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjRubh functions.}(hjRhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjNubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjhhubh)}(hThis macro removes the smallest element (the root) from the heap. It returns `true` if the element was successfully removed, or `false` if the heap is empty. The operation is **O(log n)**.h](hMThis macro removes the smallest element (the root) from the heap. It returns }(hjhhhNhNubjB)}(h`true`h]htrue}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh- if the element was successfully removed, or }(hjhhhNhNubjB)}(h`false`h]hfalse}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh( if the heap is empty. The operation is }(hjhhhNhNubj])}(h **O(log n)**h]hO(log n)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h>**Inline Version:** min_heap_pop_inline(heap, callbacks, args)h](j])}(h**Inline Version:**h]hInline Version:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh+ min_heap_pop_inline(heap, callbacks, args)}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h] heap-removalah ]h"] heap removalah$]h&]uh1hhjChhhhhKubh)}(hhh](h)}(hHeap Maintenanceh]hHeap Maintenance}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hBYou can use the following macros to maintain the heap's structure:h]hDYou can use the following macros to maintain the heap’s structure:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj%)}(h/min_heap_sift_down(heap, pos, callbacks, args);h]h/min_heap_sift_down(heap, pos, callbacks, args);}hj%sbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhjhhubj)}(hhh](j)}(h$**heap**: A pointer to the min-heap.h]h)}(hj9h](j])}(h**heap**h]hheap}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj;ubh: A pointer to the min-heap.}(hj;hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj7ubah}(h]h ]h"]h$]h&]uh1jhj4hhhhhNubj)}(h4**pos**: The index from which to start sifting down.h]h)}(hj^h](j])}(h**pos**h]hpos}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj`ubh-: The index from which to start sifting down.}(hj`hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj\ubah}(h]h ]h"]h$]h&]uh1jhj4hhhhhNubj)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h]h)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h](j])}(h **callbacks**h]h callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh: A pointer to a }(hjhhhNhNubjB)}(h`struct min_heap_callbacks`h]hstruct min_heap_callbacks}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh providing the }(hjhhhNhNubjB)}(h`less`h]hless}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh and }(hjhhhNhNubjB)}(h`swp`h]hswp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh functions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhj4hhhhhNubj)}(hG**args**: Optional arguments passed to the `less` and `swp` functions. h]h)}(hF**args**: Optional arguments passed to the `less` and `swp` functions.h](j])}(h**args**h]hargs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh#: Optional arguments passed to the }(hjhhhNhNubjB)}(h`less`h]hless}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh and }(hjhhhNhNubjB)}(h`swp`h]hswp}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh functions.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhj4hhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjhhubh)}(hThis macro restores the heap property by moving the element at the specified index (`pos`) down the heap until it is in the correct position. The operation is **O(log n)**.h](hTThis macro restores the heap property by moving the element at the specified index (}(hj- hhhNhNubjB)}(h`pos`h]hpos}(hj5 hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj- ubhF) down the heap until it is in the correct position. The operation is }(hj- hhhNhNubj])}(h **O(log n)**h]hO(log n)}(hjG hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj- ubh.}(hj- hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hI**Inline Version:** min_heap_sift_down_inline(heap, pos, callbacks, args)h](j])}(h**Inline Version:**h]hInline Version:}(hjc hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj_ ubh6 min_heap_sift_down_inline(heap, pos, callbacks, args)}(hj_ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj%)}(h-min_heap_sift_up(heap, idx, callbacks, args);h]h-min_heap_sift_up(heap, idx, callbacks, args);}hj{ sbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhjhhubj)}(hhh](j)}(h$**heap**: A pointer to the min-heap.h]h)}(hj h](j])}(h**heap**h]hheap}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh: A pointer to the min-heap.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(h-**idx**: The index of the element to sift up.h]h)}(hj h](j])}(h**idx**h]hidx}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh&: The index of the element to sift up.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h]h)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h](j])}(h **callbacks**h]h callbacks}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh: A pointer to a }(hj hhhNhNubjB)}(h`struct min_heap_callbacks`h]hstruct min_heap_callbacks}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh providing the }(hj hhhNhNubjB)}(h`less`h]hless}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh and }(hj hhhNhNubjB)}(h`swp`h]hswp}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh functions.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hG**args**: Optional arguments passed to the `less` and `swp` functions. h]h)}(hF**args**: Optional arguments passed to the `less` and `swp` functions.h](j])}(h**args**h]hargs}(hj; hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj7 ubh#: Optional arguments passed to the }(hj7 hhhNhNubjB)}(h`less`h]hless}(hjM hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj7 ubh and }(hj7 hhhNhNubjB)}(h`swp`h]hswp}(hj_ hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj7 ubh functions.}(hj7 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj3 ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjhhubh)}(hThis macro restores the heap property by moving the element at the specified index (`idx`) up the heap. The operation is **O(log n)**.h](hTThis macro restores the heap property by moving the element at the specified index (}(hj hhhNhNubjB)}(h`idx`h]hidx}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh ) up the heap. The operation is }(hj hhhNhNubj])}(h **O(log n)**h]hO(log n)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hG**Inline Version:** min_heap_sift_up_inline(heap, idx, callbacks, args)h](j])}(h**Inline Version:**h]hInline Version:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh4 min_heap_sift_up_inline(heap, idx, callbacks, args)}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj%)}(h'min_heapify_all(heap, callbacks, args);h]h'min_heapify_all(heap, callbacks, args);}hj sbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhjhhubj)}(hhh](j)}(h$**heap**: A pointer to the min-heap.h]h)}(hj h](j])}(h**heap**h]hheap}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh: A pointer to the min-heap.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h]h)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h](j])}(h **callbacks**h]h callbacks}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh: A pointer to a }(hj hhhNhNubjB)}(h`struct min_heap_callbacks`h]hstruct min_heap_callbacks}(hj" hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh providing the }(hj hhhNhNubjB)}(h`less`h]hless}(hj4 hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh and }(hj hhhNhNubjB)}(h`swp`h]hswp}(hjF hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh functions.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hG**args**: Optional arguments passed to the `less` and `swp` functions. h]h)}(hF**args**: Optional arguments passed to the `less` and `swp` functions.h](j])}(h**args**h]hargs}(hjl hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjh ubh#: Optional arguments passed to the }(hjh hhhNhNubjB)}(h`less`h]hless}(hj~ hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjh ubh and }(hjh hhhNhNubjB)}(h`swp`h]hswp}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjh ubh functions.}(hjh hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjd ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjhhubh)}(hThis macro ensures that the entire heap satisfies the heap property. It is called when the heap is built from scratch or after many modifications. The operation is **O(n)**.h](hThis macro ensures that the entire heap satisfies the heap property. It is called when the heap is built from scratch or after many modifications. The operation is }(hj hhhNhNubj])}(h**O(n)**h]hO(n)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hA**Inline Version:** min_heapify_all_inline(heap, callbacks, args)h](j])}(h**Inline Version:**h]hInline Version:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh. min_heapify_all_inline(heap, callbacks, args)}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]heap-maintenanceah ]h"]heap maintenanceah$]h&]uh1hhjChhhhhKubh)}(hhh](h)}(hRemoving Specific Elementsh]hRemoving Specific Elements}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubj%)}(h3success = min_heap_del(heap, idx, callbacks, args);h]h3success = min_heap_del(heap, idx, callbacks, args);}hj sbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhj hhubj)}(hhh](j)}(h$**heap**: A pointer to the min-heap.h]h)}(hj h](j])}(h**heap**h]hheap}(hj" hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh: A pointer to the min-heap.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(h,**idx**: The index of the element to delete.h]h)}(hjB h](j])}(h**idx**h]hidx}(hjG hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjD ubh%: The index of the element to delete.}(hjD hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj@ ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h]h)}(hc**callbacks**: A pointer to a `struct min_heap_callbacks` providing the `less` and `swp` functions.h](j])}(h **callbacks**h]h callbacks}(hjm hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hji ubh: A pointer to a }(hji hhhNhNubjB)}(h`struct min_heap_callbacks`h]hstruct min_heap_callbacks}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhji ubh providing the }(hji hhhNhNubjB)}(h`less`h]hless}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhji ubh and }(hji hhhNhNubjB)}(h`swp`h]hswp}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhji ubh functions.}(hji hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhje ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubj)}(hG**args**: Optional arguments passed to the `less` and `swp` functions. h]h)}(hF**args**: Optional arguments passed to the `less` and `swp` functions.h](j])}(h**args**h]hargs}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh#: Optional arguments passed to the }(hj hhhNhNubjB)}(h`less`h]hless}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh and }(hj hhhNhNubjB)}(h`swp`h]hswp}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh functions.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubeh}(h]h ]h"]h$]h&]j1j2uh1jhhhKhj hhubh)}(hThis macro removes an element at the specified index (`idx`) from the heap and restores the heap property. The operation is **O(log n)**.h](h6This macro removes an element at the specified index (}(hj hhhNhNubjB)}(h`idx`h]hidx}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubhA) from the heap and restores the heap property. The operation is }(hj hhhNhNubj])}(h **O(log n)**h]hO(log n)}(hj+ hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hC**Inline Version:** min_heap_del_inline(heap, idx, callbacks, args)h](j])}(h**Inline Version:**h]hInline Version:}(hjG hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjC ubh0 min_heap_del_inline(heap, idx, callbacks, args)}(hjC hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]removing-specific-elementsah ]h"]removing specific elementsah$]h&]uh1hhjChhhhhKubeh}(h]macro-wrappersah ]h"]macro wrappersah$]h&]uh1hhhhhhhhKQubh)}(hhh](h)}(hOther Utilitiesh]hOther Utilities}(hjr hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo hhhhhKubj)}(hhh]j)}(hP**min_heap_full(heap)**: Checks whether the heap is full. Complexity: **O(1)**. h]h)}(hO**min_heap_full(heap)**: Checks whether the heap is full. Complexity: **O(1)**.h](j])}(h**min_heap_full(heap)**h]hmin_heap_full(heap)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh/: Checks whether the heap is full. Complexity: }(hj hhhNhNubj])}(h**O(1)**h]hO(1)}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubah}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjo hhubj%)}(h bool full = min_heap_full(heap);h]h bool full = min_heap_full(heap);}hj sbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhjo hhubj)}(hhh]j)}(h,`heap`: A pointer to the min-heap to check. h]h)}(h+`heap`: A pointer to the min-heap to check.h](jB)}(h`heap`h]hheap}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh%: A pointer to the min-heap to check.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj hhhhhNubah}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjo hhubh)}(hAThis macro returns `true` if the heap is full, otherwise `false`.h](hThis macro returns }(hj hhhNhNubjB)}(h`true`h]htrue}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh if the heap is full, otherwise }(hj hhhNhNubjB)}(h`false`h]hfalse}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjo hhubh)}(h.**Inline Version:** min_heap_full_inline(heap)h](j])}(h**Inline Version:**h]hInline Version:}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1j\hj1ubh min_heap_full_inline(heap)}(hj1hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjo hhubj)}(hhh]j)}(hR**min_heap_empty(heap)**: Checks whether the heap is empty. Complexity: **O(1)**. h]h)}(hQ**min_heap_empty(heap)**: Checks whether the heap is empty. Complexity: **O(1)**.h](j])}(h**min_heap_empty(heap)**h]hmin_heap_empty(heap)}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjTubh0: Checks whether the heap is empty. Complexity: }(hjThhhNhNubj])}(h**O(1)**h]hO(1)}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjTubh.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjPubah}(h]h ]h"]h$]h&]uh1jhjMhhhhhNubah}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjo hhubj%)}(h"bool empty = min_heap_empty(heap);h]h"bool empty = min_heap_empty(heap);}hjsbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhKhjo hhubj)}(hhh]j)}(h,`heap`: A pointer to the min-heap to check. h]h)}(h+`heap`: A pointer to the min-heap to check.h](jB)}(h`heap`h]hheap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh%: A pointer to the min-heap to check.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubah}(h]h ]h"]h$]h&]j1j2uh1jhhhKhjo hhubh)}(hBThis macro returns `true` if the heap is empty, otherwise `false`.h](hThis macro returns }(hjhhhNhNubjB)}(h`true`h]htrue}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh! if the heap is empty, otherwise }(hjhhhNhNubjB)}(h`false`h]hfalse}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jAhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjo hhubh)}(h/**Inline Version:** min_heap_empty_inline(heap)h](j])}(h**Inline Version:**h]hInline Version:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j\hjubh min_heap_empty_inline(heap)}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjo hhubeh}(h]other-utilitiesah ]h"]other utilitiesah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h Example Usageh]h Example Usage}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj"hhhhhMubh)}(hAn example usage of the min-heap API would involve defining a heap structure, initializing it, and inserting and removing elements as needed.h]hAn example usage of the min-heap API would involve defining a heap structure, initializing it, and inserting and removing elements as needed.}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubj%)}(hX(#include int my_less_function(const void *lhs, const void *rhs, void *args) { return (*(int *)lhs < *(int *)rhs); } struct min_heap_callbacks heap_cb = { .less = my_less_function, /* Comparison function for heap order */ .swp = NULL, /* Use default swap function */ }; void example_usage(void) { /* Pre-populate the buffer with elements */ int buffer[5] = {5, 2, 8, 1, 3}; /* Declare a min-heap */ DEFINE_MIN_HEAP(int, my_heap); /* Initialize the heap with preallocated buffer and size */ min_heap_init(&my_heap, buffer, 5); /* Build the heap using min_heapify_all */ my_heap.nr = 5; /* Set the number of elements in the heap */ min_heapify_all(&my_heap, &heap_cb, NULL); /* Peek at the top element (should be 1 in this case) */ int *top = min_heap_peek(&my_heap); pr_info("Top element: %d\n", *top); /* Pop the top element (1) and get the new top (2) */ min_heap_pop(&my_heap, &heap_cb, NULL); top = min_heap_peek(&my_heap); pr_info("New top element: %d\n", *top); /* Insert a new element (0) and recheck the top */ int new_element = 0; min_heap_push(&my_heap, &new_element, &heap_cb, NULL); top = min_heap_peek(&my_heap); pr_info("Top element after insertion: %d\n", *top); }h]hX(#include int my_less_function(const void *lhs, const void *rhs, void *args) { return (*(int *)lhs < *(int *)rhs); } struct min_heap_callbacks heap_cb = { .less = my_less_function, /* Comparison function for heap order */ .swp = NULL, /* Use default swap function */ }; void example_usage(void) { /* Pre-populate the buffer with elements */ int buffer[5] = {5, 2, 8, 1, 3}; /* Declare a min-heap */ DEFINE_MIN_HEAP(int, my_heap); /* Initialize the heap with preallocated buffer and size */ min_heap_init(&my_heap, buffer, 5); /* Build the heap using min_heapify_all */ my_heap.nr = 5; /* Set the number of elements in the heap */ min_heapify_all(&my_heap, &heap_cb, NULL); /* Peek at the top element (should be 1 in this case) */ int *top = min_heap_peek(&my_heap); pr_info("Top element: %d\n", *top); /* Pop the top element (1) and get the new top (2) */ min_heap_pop(&my_heap, &heap_cb, NULL); top = min_heap_peek(&my_heap); pr_info("New top element: %d\n", *top); /* Insert a new element (0) and recheck the top */ int new_element = 0; min_heap_push(&my_heap, &new_element, &heap_cb, NULL); top = min_heap_peek(&my_heap); pr_info("Top element after insertion: %d\n", *top); }}hjAsbah}(h]h ]h"]h$]h&]hhj4j5j6j7}uh1j$hhhMhj"hhubeh}(h] example-usageah ]h"] example usageah$]h&]uh1hhhhhhhhMubeh}(h] min-heap-apiah ]h"] min heap apiah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourcehnj _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}(j]jZjjj@j=jjj8j5jl ji jj~j&j#jjjjj j jd ja jjjUjRu nametypes}(j]jj@jj8jl jj&jjj jd jjUuh}(jZhjj5j=jjjj5jji jCj~jpj#jjj)jjj jja j jjo jRj"u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]transform_messages] transformerN include_log] decorationNhhub.