kernel/util: Fixes in [MinMax]Heap implementation

This commit is contained in:
Pawel Dziepak 2013-10-22 23:56:31 +02:00
parent 8d471bc3d9
commit 4c4994435d
2 changed files with 14 additions and 4 deletions

View File

@ -230,6 +230,7 @@ HEAP_CLASS_NAME::RemoveRoot()
#if KDEBUG #if KDEBUG
Element* element = PeekRoot(); Element* element = PeekRoot();
HeapLink<Element, Key>* link = sGetLink(element); HeapLink<Element, Key>* link = sGetLink(element);
ASSERT(link->fIndex != -1);
link->fIndex = -1; link->fIndex = -1;
#endif #endif
@ -257,6 +258,8 @@ HEAP_CLASS_NAME::Insert(Element* element, Key key)
HeapLink<Element, Key>* link = sGetLink(element); HeapLink<Element, Key>* link = sGetLink(element);
ASSERT(link->fIndex == -1);
fElements[fLastElement] = element; fElements[fLastElement] = element;
link->fIndex = fLastElement++; link->fIndex = fLastElement++;
link->fKey = key; link->fKey = key;
@ -270,7 +273,6 @@ HEAP_TEMPLATE_LIST
status_t status_t
HEAP_CLASS_NAME::_GrowHeap(int minimalSize) HEAP_CLASS_NAME::_GrowHeap(int minimalSize)
{ {
minimalSize = minimalSize % 2 ? minimalSize : minimalSize + 1;
int newSize = max_c(max_c(fSize * 2, 4), minimalSize); int newSize = max_c(max_c(fSize * 2, 4), minimalSize);
size_t arraySize = newSize * sizeof(Element*); size_t arraySize = newSize * sizeof(Element*);

View File

@ -258,6 +258,7 @@ MIN_MAX_HEAP_CLASS_NAME::RemoveMinimum()
#if KDEBUG #if KDEBUG
Element* element = PeekMinimum(); Element* element = PeekMinimum();
MinMaxHeapLink<Element, Key>* link = sGetLink(element); MinMaxHeapLink<Element, Key>* link = sGetLink(element);
ASSERT(link->fIndex != -1);
link->fIndex = -1; link->fIndex = -1;
#endif #endif
@ -278,6 +279,7 @@ MIN_MAX_HEAP_CLASS_NAME::RemoveMaximum()
#if KDEBUG #if KDEBUG
Element* element = PeekMaximum(); Element* element = PeekMaximum();
MinMaxHeapLink<Element, Key>* link = sGetLink(element); MinMaxHeapLink<Element, Key>* link = sGetLink(element);
ASSERT(link->fIndex != -1);
link->fIndex = -1; link->fIndex = -1;
#endif #endif
@ -296,10 +298,12 @@ MIN_MAX_HEAP_CLASS_NAME::Insert(Element* element, Key key)
return result; return result;
} }
ASSERT(fMinLastElement != fSize || fMaxLastElement != fSize); ASSERT(fMinLastElement < fSize || fMaxLastElement < fSize);
MinMaxHeapLink<Element, Key>* link = sGetLink(element); MinMaxHeapLink<Element, Key>* link = sGetLink(element);
ASSERT(link->fIndex == -1);
link->fMinTree = fMinLastElement < fMaxLastElement; link->fMinTree = fMinLastElement < fMaxLastElement;
int& lastElement = link->fMinTree ? fMinLastElement : fMaxLastElement; int& lastElement = link->fMinTree ? fMinLastElement : fMaxLastElement;
@ -320,7 +324,7 @@ MIN_MAX_HEAP_TEMPLATE_LIST
status_t status_t
MIN_MAX_HEAP_CLASS_NAME::_GrowHeap(int minimalSize) MIN_MAX_HEAP_CLASS_NAME::_GrowHeap(int minimalSize)
{ {
minimalSize = minimalSize % 2 ? minimalSize : minimalSize + 1; minimalSize = minimalSize % 2 == 0 ? minimalSize : minimalSize + 1;
int newSize = max_c(max_c(fSize * 4, 4), minimalSize); int newSize = max_c(max_c(fSize * 4, 4), minimalSize);
size_t arraySize = newSize * sizeof(Element*); size_t arraySize = newSize * sizeof(Element*);
@ -329,7 +333,11 @@ MIN_MAX_HEAP_CLASS_NAME::_GrowHeap(int minimalSize)
if (newBuffer == NULL) if (newBuffer == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
fMinElements = newBuffer; fMinElements = newBuffer;
fMaxElements = newBuffer + (newSize / 2);
newBuffer += newSize / 2;
if (fMaxElements != NULL)
memcpy(newBuffer, fMaxElements, fSize * sizeof(Element*));
fMaxElements = newBuffer;
fSize = newSize / 2; fSize = newSize / 2;
return B_OK; return B_OK;