mirror of https://github.com/F-Stack/f-stack.git
442 lines
8.4 KiB
C
442 lines
8.4 KiB
C
|
|
|||
|
/**
|
|||
|
* Tencent is pleased to support the open source community by making MSEC available.
|
|||
|
*
|
|||
|
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
|
|||
|
*
|
|||
|
* Licensed under the GNU General Public License, Version 2.0 (the "License");
|
|||
|
* you may not use this file except in compliance with the License. You may
|
|||
|
* obtain a copy of the License at
|
|||
|
*
|
|||
|
* https://opensource.org/licenses/GPL-2.0
|
|||
|
*
|
|||
|
* Unless required by applicable law or agreed to in writing, software distributed under the
|
|||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|||
|
* either express or implied. See the License for the specific language governing permissions
|
|||
|
* and limitations under the License.
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @filename heap.h
|
|||
|
* @info <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>std::make_heap
|
|||
|
* @time 2013-06-11
|
|||
|
*/
|
|||
|
|
|||
|
#ifndef __HEAP_ENTRY_FILE__
|
|||
|
#define __HEAP_ENTRY_FILE__
|
|||
|
|
|||
|
#include <stdlib.h>
|
|||
|
#include <stdio.h>
|
|||
|
#include <string.h>
|
|||
|
#include <assert.h>
|
|||
|
|
|||
|
#define heap_assert(statement)
|
|||
|
//#define heap_assert(statement) assert(statement)
|
|||
|
|
|||
|
namespace NS_MICRO_THREAD {
|
|||
|
|
|||
|
class HeapEntry; // <20><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>, <20>̳<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>չ
|
|||
|
class HeapList; // <20>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD>, ͨ<><CDA8>
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>С<EFBFBD>ѵĶ<EFBFBD>Ԫ<EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>õĶ<EFBFBD>, <EFBFBD>̳и<EFBFBD>Ԫ<EFBFBD>ؼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ
|
|||
|
*/
|
|||
|
class HeapEntry
|
|||
|
{
|
|||
|
private:
|
|||
|
int _index; ///< <20><>Ԫ<EFBFBD><D4AA><EFBFBD>±<EFBFBD>, <20><><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
public:
|
|||
|
friend class HeapList;
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
HeapEntry():_index(0){};
|
|||
|
virtual ~HeapEntry(){};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ȡֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>Ƚ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
*/
|
|||
|
virtual unsigned long long HeapValue() = 0;
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ѱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>ѡʵ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
virtual void HeapIterate() {
|
|||
|
return;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ԫ<EFBFBD>ز<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param list <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>; -2 <EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline int InsertIntoHeap(HeapList* list);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ԫ<EFBFBD>شӶ<EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
|
|||
|
* @param list <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD>ѿ<EFBFBD>; -2 <EFBFBD>ظ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline int DeleteFromHeap(HeapList* list);
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>ȡ, <EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
|
|||
|
* @return <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
*/
|
|||
|
inline int GetIndex() {
|
|||
|
return _index;
|
|||
|
};
|
|||
|
|
|||
|
private:
|
|||
|
|
|||
|
inline int HeapValueCmp(HeapEntry* rhs) {
|
|||
|
if (this->HeapValue() == rhs->HeapValue()) {
|
|||
|
return 0;
|
|||
|
} else if (this->HeapValue() > rhs->HeapValue()) {
|
|||
|
return 1;
|
|||
|
} else {
|
|||
|
return -1;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
inline void SetIndex(int index) {
|
|||
|
_index = index;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>С<EFBFBD>Ѷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class HeapList
|
|||
|
{
|
|||
|
private:
|
|||
|
HeapEntry** _list; // <20><>Ԫ<EFBFBD>ص<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, Ŀǰ<C4BF><C7B0><EFBFBD><EFBFBD>
|
|||
|
int _max; // <20>ѿɹ<D1BF><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
|||
|
int _count; // <20><><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>
|
|||
|
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
explicit HeapList(int max = 100000) {
|
|||
|
_max = (max > 0) ? max : 100000;
|
|||
|
_list = (HeapEntry**)malloc (sizeof(HeapEntry*) * (_max+1));
|
|||
|
heap_assert(_list);
|
|||
|
memset(_list, 0, sizeof(HeapEntry*) * (_max+1));
|
|||
|
_count = 0;
|
|||
|
};
|
|||
|
virtual ~HeapList() {
|
|||
|
if (_list) {
|
|||
|
free(_list);
|
|||
|
_list = NULL;
|
|||
|
}
|
|||
|
_max = 0;
|
|||
|
_count = 0;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>չheap<EFBFBD>Ĵ<EFBFBD>С, <EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param size <EFBFBD>µĶ<EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; -1 ʧ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
int HeapResize(int size) {
|
|||
|
if (_max >= size) {
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
HeapEntry** new_list = (HeapEntry**)malloc(sizeof(HeapEntry*) * (size+1));
|
|||
|
if (NULL == new_list) {
|
|||
|
return -1;
|
|||
|
}
|
|||
|
memset(new_list, 0, sizeof(HeapEntry*) * (size+1));
|
|||
|
memcpy(new_list, _list, sizeof(HeapEntry*) * (_max+1));
|
|||
|
free(_list);
|
|||
|
_list = new_list;
|
|||
|
_max = size;
|
|||
|
|
|||
|
return 0;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @param entry <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>; -2 <EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
int HeapPush(HeapEntry* entry);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief ȡ<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @return <EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>, NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
HeapEntry* HeapPop();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @param entry <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD>ѿ<EFBFBD>; -2 <EFBFBD>ظ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
int HeapDelete(HeapEntry* entry);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Խӿ<EFBFBD>, <EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD>ѷ<EFBFBD>ʽ<EFBFBD><EFBFBD>ӡԪ<EFBFBD><EFBFBD>, ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿԪ<EFBFBD>صĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
*/
|
|||
|
void HeapForeach();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD>ѵ<EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
|
|||
|
*/
|
|||
|
int HeapSize() {
|
|||
|
return _count;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief ȡ<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @return <EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>, NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
HeapEntry* HeapTop() {
|
|||
|
return (_count > 0) ? _list[1] : NULL;
|
|||
|
};
|
|||
|
|
|||
|
private:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return true <EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
bool HeapFull() {
|
|||
|
return (_count >= _max);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @return true <EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
bool HeapEmpty() {
|
|||
|
return (_count == 0);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ȽϺ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void HeapUp();
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ȽϺ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void HeapDown(int index);
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ȽϺ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline void HeapList::HeapUp()
|
|||
|
{
|
|||
|
for (int pos = _count; pos > 0; pos = pos/2)
|
|||
|
{
|
|||
|
if (pos/2 < 1) // pos == 1 <20>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>, 0 <20><><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD>
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (_list[pos]->HeapValueCmp(_list[pos/2]) < 0)
|
|||
|
{
|
|||
|
HeapEntry* tmp = _list[pos/2];
|
|||
|
_list[pos/2] = _list[pos];
|
|||
|
_list[pos] = tmp;
|
|||
|
|
|||
|
_list[pos]->SetIndex(pos);
|
|||
|
_list[pos/2]->SetIndex(pos/2);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ȽϺ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @param index <EFBFBD>Ӹ<EFBFBD>λ<EFBFBD>ÿ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline void HeapList::HeapDown(int index)
|
|||
|
{
|
|||
|
int min_son;
|
|||
|
for (int pos = index; pos <= _count; pos = min_son)
|
|||
|
{
|
|||
|
if (pos*2 > _count) // pos<6F><73>Ҷ<EFBFBD>ӽڵ<D3BD><DAB5><EFBFBD>
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
else if (pos*2 == _count)
|
|||
|
{
|
|||
|
min_son = pos*2;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (_list[pos*2+1]->HeapValueCmp(_list[pos*2]) < 0)
|
|||
|
{
|
|||
|
min_son = pos*2+1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
min_son = pos*2;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (_list[pos]->HeapValueCmp(_list[min_son]) > 0)
|
|||
|
{
|
|||
|
HeapEntry* tmp = _list[min_son];
|
|||
|
_list[min_son] = _list[pos];
|
|||
|
_list[pos] = tmp;
|
|||
|
|
|||
|
_list[pos]->SetIndex(pos);
|
|||
|
_list[min_son]->SetIndex(min_son);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @param entry <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>; -2 <EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline int HeapList::HeapPush(HeapEntry* item)
|
|||
|
{
|
|||
|
if (HeapFull()) {
|
|||
|
heap_assert(0); // <20><>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF>ܵ<EFBFBD>, ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD>̫<EFBFBD><CCAB><EFBFBD>ܹ<EFBFBD>10W
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
if (item->GetIndex() != 0) {
|
|||
|
heap_assert(0); // <20>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
return -2;
|
|||
|
}
|
|||
|
|
|||
|
_count++;
|
|||
|
_list[_count] = item;
|
|||
|
item->SetIndex(_count);
|
|||
|
|
|||
|
HeapUp();
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief ȡ<EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @return <EFBFBD>Ѷ<EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>, NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline HeapEntry* HeapList::HeapPop()
|
|||
|
{
|
|||
|
if (HeapEmpty()) {
|
|||
|
return NULL;
|
|||
|
}
|
|||
|
|
|||
|
HeapEntry* top = _list[1]; // 0 <20><><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
_list[1] = _list[_count];
|
|||
|
_list[1]->SetIndex(1);
|
|||
|
_list[_count] = 0;
|
|||
|
|
|||
|
_count--;
|
|||
|
HeapDown(1);
|
|||
|
|
|||
|
heap_assert(top->GetIndex() == 1);
|
|||
|
top->SetIndex(0);
|
|||
|
return top;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
|
|||
|
* @param entry <EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD>ѿ<EFBFBD>; -2 <EFBFBD>ظ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline int HeapList::HeapDelete(HeapEntry* item)
|
|||
|
{
|
|||
|
if (HeapEmpty()) {
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
int pos = item->GetIndex() ;
|
|||
|
if ((pos > _count) ||(pos <= 0))
|
|||
|
{
|
|||
|
heap_assert(0); // <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB>ظ<EFBFBD>ɾ<EFBFBD><C9BE>
|
|||
|
return -2;
|
|||
|
}
|
|||
|
|
|||
|
HeapEntry* del = _list[pos];
|
|||
|
_list[pos] = _list[_count];
|
|||
|
_list[pos]->SetIndex(pos);
|
|||
|
|
|||
|
_list[_count] = 0;
|
|||
|
_count--;
|
|||
|
|
|||
|
HeapDown(pos);
|
|||
|
heap_assert(pos == del->GetIndex());
|
|||
|
del->SetIndex(0);
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>Խӿ<EFBFBD>, <EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD>ѷ<EFBFBD>ʽ<EFBFBD><EFBFBD>ӡԪ<EFBFBD><EFBFBD>, ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿԪ<EFBFBD>صĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
*/
|
|||
|
inline void HeapList::HeapForeach()
|
|||
|
{
|
|||
|
int per = 1;
|
|||
|
for (int i = 1; i <= _count; i++)
|
|||
|
{
|
|||
|
if (i >= per*2)
|
|||
|
{
|
|||
|
printf("\n");
|
|||
|
per *=2;
|
|||
|
}
|
|||
|
printf("%llu ", _list[i]->HeapValue());
|
|||
|
|
|||
|
_list[i]->HeapIterate();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ԫ<EFBFBD>ز<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param list <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>; -2 <EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline int HeapEntry::InsertIntoHeap(HeapList* list) {
|
|||
|
return list->HeapPush(this);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ԫ<EFBFBD>شӶ<EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD>
|
|||
|
* @param list <EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
* @return 0 <EFBFBD>ɹ<EFBFBD>; <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD> -1 <EFBFBD>ѿ<EFBFBD>; -2 <EFBFBD>ظ<EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
inline int HeapEntry::DeleteFromHeap(HeapList* list) {
|
|||
|
return list->HeapDelete(this);
|
|||
|
};
|
|||
|
|
|||
|
} // namespace end
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
|