fix infinite-handling in mcp and precision issues in heap

This commit is contained in:
Zachary Pincus
2011-01-07 08:01:21 -05:00
parent dc79f81824
commit 171bd18f80
3 changed files with 20 additions and 13 deletions
+15 -8
View File
@@ -476,19 +476,26 @@ cdef class MCP:
# ignore this point
if flat_cumulative_costs[new_index] != inf:
continue
# If the cost at this point is negative or infinite, ignore it
new_cost = flat_costs[new_index]
if new_cost < 0 or new_cost == inf:
continue
# Now we ask the heap to append or update the cost to
# this new point, but only if that point isn't already
# in the heap, or it is but the new cost is lower.
travel_cost = self._travel_cost(flat_costs[index],
flat_costs[new_index],
new_cost,
offset_lengths[i])
costs_heap.push_if_lower_fast(cost + travel_cost, new_index)
# If we did perform an append or update, we should
# record the offset from the predecessor to this new
# point
if costs_heap._pushed:
traceback_offsets[new_index] = i
# don't push infs into the heap though!
new_cost = cost + travel_cost
if new_cost != inf:
costs_heap.push_if_lower_fast(new_cost, new_index)
# If we did perform an append or update, we should
# record the offset from the predecessor to this new
# point
if costs_heap._pushed:
traceback_offsets[new_index] = i
# Un-flatten the costs and traceback arrays for human consumption.
cumulative_costs = flat_cumulative_costs.reshape(self.costs_shape,
+2 -2
View File
@@ -22,7 +22,7 @@ cdef class BinaryHeap:
cdef void _remove(self, int i)
cdef int push_fast(self, double value, int reference)
cdef float pop_fast(self)
cdef VALUE_T pop_fast(self)
cdef class FastUpdateBinaryHeap(BinaryHeap):
cdef readonly int max_reference
@@ -30,6 +30,6 @@ cdef class FastUpdateBinaryHeap(BinaryHeap):
cdef int _invalid_ref
cdef int _pushed
cdef float value_of_fast(self, int reference)
cdef VALUE_T value_of_fast(self, int reference)
cdef int push_if_lower_fast(self, double value, int reference)
+3 -3
View File
@@ -339,7 +339,7 @@ cdef class BinaryHeap:
return count
cdef float pop_fast(self):
cdef VALUE_T pop_fast(self):
"""The c-method for fast popping.
Returns the minimum value. The reference is put in self._popped_ref"""
@@ -366,7 +366,7 @@ cdef class BinaryHeap:
# get values
cdef int ir = i - ((1 << levels) - 1) #(2**self.levels-1) # LevelStart
cdef float value = values[i]
cdef VALUE_T value = values[i]
self._popped_ref = self._references[ir]
# remove it
@@ -645,7 +645,7 @@ cdef class FastUpdateBinaryHeap(BinaryHeap):
return ir
cdef float value_of_fast(self, int reference):
cdef VALUE_T value_of_fast(self, int reference):
"""Return the value corresponding to the given reference. If inf
is returned, the reference may be invalid: check the _invaild_ref
field in this case."""