home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "basHeap"
- '=====================================================================
- 'HEAP.BAS by Frank Font 1995
- '
- 'This VB4 file implements the interface to heap functions for the
- 'MakeTeam program.
- '
- 'Internally, the heap functionality is only simulated. A real heap
- 'implementation would enhance the performance of the program very
- 'significantly.
- '
- 'The heap is simulated by an array of type memrec_t. The memrec_t
- 'type is defined elsewhere.
- '
- '*********************************************************************
- 'NOTE: This program is for entertainment purposes ONLY.
- '=====================================================================
- Option Explicit
-
- '---------------------------------------------------------------------
- 'Initialize the heap so that it is empty.
- '---------------------------------------------------------------------
- Sub InitHeap()
-
- NodeHeapSize = ((JobsInProject * EmpsInPool) / 2) + 1024
- ReDim nodeHeap(NodeHeapSize + 1)
- Dim i As Long
-
- For i = 0 To NodeHeapSize
- nodeHeap(i) = -1
- Next i
- End Sub
-
-
- '---------------------------------------------------------------------
- 'Returns true if heap is empty.
- '---------------------------------------------------------------------
- Public Function EmptyHeap() As Boolean
- Dim i As Long
- For i = 0 To NodeHeapSize
- 'Find one non-empty node and we have non-empty heap.
- If nodeHeap(i) >= 0 Then
- EmptyHeap = False
- Exit Function
- End If
- Next i
- EmptyHeap = True
- End Function
-
-
-
- '---------------------------------------------------------------------
- 'Returns index to smallest node of heap and deletes it.
- '---------------------------------------------------------------------
- Public Function DeleteMin() As Long
- Dim i As Long
- Dim si As Long
- Dim smallest As Double
-
- 'Initialize the temporary variables.
- si = -1 'Initialize to flag value.
- smallest = MAX_Double 'Initialize to huge number.
-
- 'Search the fake heap.
- For i = 0 To NodeHeapSize
- If nodeHeap(i) >= 0 Then
- If memPool(nodeHeap(i)).cost <= smallest Then
- si = i 'Index to smallest so far.
- smallest = memPool(nodeHeap(i)).cost
- End If
- End If
- Next i
-
- i = nodeHeap(si) 'Record the pointer value.
- nodeHeap(si) = -1 'Delete the node from the heap.
- DeleteMin = i 'Return the node of the lowest cost.
-
- End Function
-
-
-
- '---------------------------------------------------------------------
- 'Pass in node pointer (np) of node to put into the heap.
- '---------------------------------------------------------------------
- Public Sub AddHeap(np As Long)
- Dim i As Long
- Dim h As mejrec_t
-
- 'Keep looping until we find a node.
- While True
-
- 'This is not a real heap.
- For i = 0 To NodeHeapSize
- If nodeHeap(i) < 0 Then
- nodeHeap(i) = np
- Exit Sub
- End If
- Next i
-
- 'We ran out of space - allocate some more.
- On Error GoTo addHeap_NoMemory
- NodeHeapSize = NodeHeapSize + 256
- ReDim Preserve nodeHeap(NodeHeapSize)
- Dim j 'Here for speed.
- For j = i To NodeHeapSize
- nodeHeap(j) = -1
- Next j
- On Error GoTo 0
- DoEvents
-
- Wend
-
- addHeap_NoMemory:
- Beep
- MsgBox "Fatal Error - Not cannot allocate more internal heap space!", 16, gProgramTitle
- End
- End Sub
-
-
-