sc/heap/sc_heap.h

107 lines
2.7 KiB
C
Raw Normal View History

2020-11-11 01:19:49 +03:00
/*
* MIT License
*
2021-02-05 20:46:59 +03:00
* Copyright (c) 2021 Ozan Tezcan
2020-11-11 01:19:49 +03:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SC_HEAP_H
#define SC_HEAP_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
2021-02-14 16:19:26 +03:00
#define SC_HEAP_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
#define sc_heap_malloc malloc
#define sc_heap_realloc realloc
#define sc_heap_free free
#endif
struct sc_heap_data {
int64_t key;
void *data;
2020-11-11 01:19:49 +03:00
};
struct sc_heap {
size_t cap;
size_t size;
struct sc_heap_data *elems;
2020-11-11 01:19:49 +03:00
};
/**
* @param h heap
* @param cap initial capacity, pass '0' for no initial memory allocation
* @return 'true' on success, 'false' on out of memory
2020-11-11 01:19:49 +03:00
*/
bool sc_heap_init(struct sc_heap *h, size_t cap);
2020-11-11 01:19:49 +03:00
/**
* Destroys heap, frees memory
* @param h heap
2020-11-11 01:19:49 +03:00
*/
void sc_heap_term(struct sc_heap *h);
2020-11-11 01:19:49 +03:00
/**
* @param h heap
* @return element count
2020-11-11 01:19:49 +03:00
*/
size_t sc_heap_size(struct sc_heap *h);
2020-11-11 01:19:49 +03:00
/**
* Clears elements from the queue, does not free the allocated memory.
* @param h heap
2020-11-11 01:19:49 +03:00
*/
void sc_heap_clear(struct sc_heap *h);
2020-11-11 01:19:49 +03:00
/**
* @param h heap
2021-02-03 08:09:50 +03:00
* @param key key
* @param data data
2020-11-11 01:19:49 +03:00
* @return 'false' on out of memory.
*/
bool sc_heap_add(struct sc_heap *h, int64_t key, void *data);
2020-11-11 01:19:49 +03:00
/**
* Read top element without removing from the heap.
*
* @param h heap
2020-11-11 01:19:49 +03:00
* @param key [out] key
* @param data [out] data
* @return 'false' if there is no element in the heap.
*/
bool sc_heap_peek(struct sc_heap *h, int64_t *key, void **data);
2020-11-11 01:19:49 +03:00
/**
* Read top element and remove it from the heap.
*
* @param h heap
2020-11-11 01:19:49 +03:00
* @param key [out] key
* @param data [out] data
* @return 'false' if there is no element in the heap.
*/
bool sc_heap_pop(struct sc_heap *h, int64_t *key, void **data);
2020-11-11 01:19:49 +03:00
#endif