1994-11-04 16:29:24 +00:00
|
|
|
/* Implementation for Objective-C Heap object
|
1996-02-22 15:18:57 +00:00
|
|
|
Copyright (C) 1993, 1994, 1996 Free Software Foundation, Inc.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
1996-02-22 15:18:57 +00:00
|
|
|
Created: May 1993
|
1994-11-04 16:29:24 +00:00
|
|
|
|
1996-04-17 19:55:26 +00:00
|
|
|
This file is part of the Gnustep Base Library.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library 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. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
/* This class could be improved by somehow making is a subclass of
|
|
|
|
IndexedCollection, but not OrderedCollection. */
|
|
|
|
|
1996-04-17 15:23:00 +00:00
|
|
|
#include <gnustep/base/Heap.h>
|
|
|
|
#include <gnustep/base/ArrayPrivate.h>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
#define HEAP_PARENT(i) (i/2)
|
|
|
|
#define HEAP_LEFT(i) (2 * i)
|
|
|
|
#define HEAP_RIGHT(i) ((2 * i) + 1)
|
|
|
|
|
|
|
|
@implementation Heap
|
|
|
|
|
|
|
|
/* We could take out the recursive call to make it a little more efficient */
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) heapifyFromIndex: (unsigned)index
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
|
|
|
unsigned right, left, largest;
|
1996-02-22 15:18:57 +00:00
|
|
|
id tmp;
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
right = HEAP_RIGHT(index);
|
|
|
|
left = HEAP_LEFT(index);
|
|
|
|
if (left <= _count
|
1996-04-16 21:29:00 +00:00
|
|
|
&& [_contents_array[index] compare: _contents_array[left]] > 0)
|
1994-11-04 16:29:24 +00:00
|
|
|
largest = left;
|
|
|
|
else
|
|
|
|
largest = index;
|
|
|
|
if (right <= _count
|
1996-04-16 21:29:00 +00:00
|
|
|
&& [_contents_array[largest] compare: _contents_array[right]] > 0)
|
1994-11-04 16:29:24 +00:00
|
|
|
largest = right;
|
|
|
|
if (largest != index)
|
|
|
|
{
|
|
|
|
tmp = _contents_array[index];
|
|
|
|
_contents_array[index] = _contents_array[largest];
|
|
|
|
_contents_array[largest] = tmp;
|
|
|
|
[self heapifyFromIndex:largest];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) heapify
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// could use objc_msg_lookup here;
|
|
|
|
for (i = _count / 2; i >= 1; i--)
|
|
|
|
[self heapifyFromIndex:i];
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) removeFirstObject
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
|
|
|
if (_count == 0)
|
1996-02-22 15:18:57 +00:00
|
|
|
return;
|
|
|
|
[_contents_array[0] release];
|
1994-11-04 16:29:24 +00:00
|
|
|
_contents_array[0] = _contents_array[_count-1];
|
|
|
|
decrementCount(self);
|
|
|
|
[self heapifyFromIndex:0];
|
|
|
|
}
|
|
|
|
|
1996-02-22 15:18:57 +00:00
|
|
|
- (void) addObject: newObject
|
1994-11-04 16:29:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
incrementCount(self);
|
1996-02-22 15:18:57 +00:00
|
|
|
[newObject retain];
|
1994-11-04 16:29:24 +00:00
|
|
|
for (i = _count-1;
|
|
|
|
i > 0
|
1996-04-16 21:29:00 +00:00
|
|
|
&& [newObject compare: _contents_array[HEAP_PARENT(i)]] < 0;
|
1994-11-04 16:29:24 +00:00
|
|
|
i = HEAP_PARENT(i))
|
|
|
|
{
|
|
|
|
_contents_array[i] = _contents_array[HEAP_PARENT(i)];
|
|
|
|
}
|
1996-02-22 15:18:57 +00:00
|
|
|
_contents_array[i] = newObject;
|
1994-11-04 16:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|