theap.h 2.6 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
S
hash  
Shengliang Guan 已提交
15 16 17 18 19

#ifndef _TD_UTIL_HEAP_H_
#define _TD_UTIL_HEAP_H_

#include "os.h"
20
#include "tarray.h"
dengyihao's avatar
dengyihao 已提交
21 22 23 24 25 26 27 28

#ifdef __cplusplus
extern "C" {
#endif

struct HeapNode;

/* Return non-zero if a < b. */
S
hash  
Shengliang Guan 已提交
29
typedef int32_t (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b);
dengyihao's avatar
dengyihao 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42

typedef struct HeapNode {
  struct HeapNode* left;
  struct HeapNode* right;
  struct HeapNode* parent;
} HeapNode;

/* A binary min heap.  The usual properties hold: the root is the lowest
 * element in the set, the height of the tree is at most log2(nodes) and
 * it's always a complete binary tree.
 *
 */
typedef struct {
S
hash  
Shengliang Guan 已提交
43 44 45
  HeapNode*     min;
  size_t        nelts;
  HeapCompareFn compFn;
dengyihao's avatar
dengyihao 已提交
46 47 48 49
} Heap;

Heap* heapCreate(HeapCompareFn fn);

S
hash  
Shengliang Guan 已提交
50
void heapDestroy(Heap* heap);
dengyihao's avatar
dengyihao 已提交
51 52 53 54 55 56 57 58 59

HeapNode* heapMin(const Heap* heap);

void heapInsert(Heap* heap, HeapNode* node);

void heapRemove(Heap* heap, struct HeapNode* node);

void heapDequeue(Heap* heap);

S
hash  
Shengliang Guan 已提交
60
size_t heapSize(Heap* heap);
dengyihao's avatar
dengyihao 已提交
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
typedef bool (*pq_comp_fn)(void* l, void* r, void* param);

typedef struct PriorityQueueNode {
  void* data;
} PriorityQueueNode;

typedef struct PriorityQueue PriorityQueue;

PriorityQueue* createPriorityQueue(pq_comp_fn fn, FDelete deleteFn, void* param);

void taosPQSetFn(PriorityQueue* pq, pq_comp_fn fn);

void destroyPriorityQueue(PriorityQueue* pq);

PriorityQueueNode* taosPQTop(PriorityQueue* pq);

size_t taosPQSize(PriorityQueue* pq);

void taosPQPush(PriorityQueue* pq, const PriorityQueueNode* node);

void taosPQPop(PriorityQueue* pq);

typedef struct BoundedQueue BoundedQueue;

BoundedQueue* createBoundedQueue(uint32_t maxSize, pq_comp_fn fn, FDelete deleteFn, void* param);

void taosBQSetFn(BoundedQueue* q, pq_comp_fn fn);

void destroyBoundedQueue(BoundedQueue* q);

void taosBQPush(BoundedQueue* q, PriorityQueueNode* n);

PriorityQueueNode* taosBQTop(BoundedQueue* q);

size_t taosBQSize(BoundedQueue* q);

size_t taosBQMaxSize(BoundedQueue* q);

void taosBQBuildHeap(BoundedQueue* q);

void taosBQPop(BoundedQueue* q);

dengyihao's avatar
dengyihao 已提交
104 105 106 107
#ifdef __cplusplus
}
#endif

S
hash  
Shengliang Guan 已提交
108
#endif /*_TD_UTIL_HEAP_H_*/