theap.h 1.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"
dengyihao's avatar
dengyihao 已提交
20 21 22 23 24 25 26 27

#ifdef __cplusplus
extern "C" {
#endif

struct HeapNode;

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

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 已提交
42 43 44
  HeapNode*     min;
  size_t        nelts;
  HeapCompareFn compFn;
dengyihao's avatar
dengyihao 已提交
45 46 47 48
} Heap;

Heap* heapCreate(HeapCompareFn fn);

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

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 已提交
59
size_t heapSize(Heap* heap);
dengyihao's avatar
dengyihao 已提交
60 61 62 63 64

#ifdef __cplusplus
}
#endif

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