;finds the peak of the region in the previous frame to which the current peak belongs to
(bind-func find_previous_peak
(lambda (current_peak:i64 prev_peaks_array:i64* prev_lower_bound:i64* prev_upper_bound:i64* prev_size:i64)
(let ((n:i64 0)
(prev_peak:i64 0))
(cond ((= (is_empty prev_peaks_array prev_size) 0)
(while (= (and (<= (pref prev_lower_bound n) current_peak) (<= current_peak (pref prev_upper_bound n))) 0)
(set! n (+ n 1)))
(set! prev_peak (pref prev_peaks_array n)))
(else
(set! prev_peak -1)))
prev_peak)))