Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gadig
four
Commits
30954f53
Commit
30954f53
authored
Oct 20, 2016
by
Cody A Burchett
Browse files
-m Heap for zac
parent
7d25f6be
Changes
2
Hide whitespace changes
Inline
Side-by-side
Assets/script/Heap.cs
0 → 100644
View file @
30954f53
using
UnityEngine
;
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
public
class
Heap
<
Tile
>
where
Tile
:
HeapItem
<
Tile
>{
Tile
[]
heapTiles
;
int
itemCount
;
//add heapIndex, compareTo to tile class
//compare fCost of node then hCost
public
Heap
(
int
maxSize
)
{
heapTiles
=
new
Tile
[
maxSize
];
}
public
void
Add
(
Tile
item
)
{
item
.
heapIndex
=
itemCount
;
heapTiles
[
itemCount
]
=
item
;
SortUp
(
item
);
itemCount
++;
}
public
Tile
RemoveFirst
()
{
Tile
first
=
heapTiles
[
0
];
itemCount
--;
heapTiles
[
0
]
=
heapTiles
[
itemCount
];
heapTiles
[
0
].
heapIndex
=
0
;
SortDown
(
heapTiles
[
0
]);
return
first
;
}
public
void
UpdateItem
(
Tile
item
)
{
SortUp
(
item
);
}
public
int
Count
{
get
{
return
itemCount
;
}
}
public
bool
Contains
(
Tile
item
)
{
return
Equals
(
heapTiles
[
item
.
heapIndex
],
item
);
}
void
SortDown
(
Tile
item
)
{
while
(
true
)
{
int
leftChildIndex
=
item
.
heapIndex
*
2
+
1
;
int
rightChildIndex
=
(
item
.
heapIndex
*
2
)
+
2
;
int
swapIndex
=
0
;
if
(
leftChildIndex
<
itemCount
)
{
swapIndex
=
leftChildIndex
;
if
(
rightChildIndex
<
itemCount
)
{
if
(
heapTiles
[
leftChildIndex
].
CompareTo
(
heapTiles
[
rightChildIndex
])
<
0
)
{
swapIndex
=
rightChildIndex
;
}
}
if
(
item
.
CompareTo
(
heapTiles
[
swapIndex
])
<
0
)
{
Swap
(
item
,
heapTiles
[
swapIndex
]);
}
else
{
return
;
}
}
else
{
return
;
}
}
}
void
SortUp
(
Tile
item
)
{
int
indexOfParent
=
(
item
.
heapIndex
-
1
)
/
2
;
while
(
true
)
{
Tile
parent
=
heapTiles
[
indexOfParent
];
if
(
item
.
CompareTo
(
parent
)
>
0
)
{
Swap
(
item
,
parent
);
}
else
{
break
;
}
indexOfParent
=
(
item
.
heapIndex
-
1
)
/
2
;
}
}
void
Swap
(
Tile
item1
,
Tile
item2
)
{
heapTiles
[
item1
.
heapIndex
]
=
item2
;
heapTiles
[
item2
.
heapIndex
]
=
item1
;
int
tempIndex
=
item1
.
heapIndex
;
item1
.
heapIndex
=
item2
.
heapIndex
;
item2
.
heapIndex
=
tempIndex
;
}
}
public
interface
HeapItem
<
Tile
>
:
IComparable
<
Tile
>
{
int
heapIndex
{
get
;
set
;
}
}
\ No newline at end of file
Assets/script/Heap.cs.meta
0 → 100644
View file @
30954f53
fileFormatVersion: 2
guid: 243bb794b9f808b409bcd50557270e5b
timeCreated: 1477007304
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment