📄 list2ranges.pro
字号:
; $Id: list2ranges.pro,v 1.2 2002/03/14 11:49:13 riccardi Exp $
;+
;
; LIST2RANGES
;
; This function returns a list of couples of numbers
; giving the ranges of consecutive numbers in the input list.
;
; ranges = LIST2RANGES(list, range_count)
;
;
; INPUTS
;
; list: byte, short-int or long-int vector. The list don't need to
; be ordered or having unique elements.
;
; OUTPUTS
;
; ranges: byte, short-int or long-int array. Same type as list.
; List of ranges of consecutive values
; in list. The array has 2 columns and range_count rows.
;
; range_count: named variable. This variable is set in output to the number of
; indipendent ranges found in the list vector.
;
;
; EXAMPLE
;
; list = [3, 14, 2, 13, 4, 1, 13, 11, -1, 5, 0]
; print, list2ranges(list, count)
;
; -1 5
; 11 11
; 13 14
;
; print, count
;
; 3
;
; The result means that the list can be considered as 3
; not overlapping groups of numbers, the first ranging from
; -1 to 5, the second from 11 to 11 and the last from 13 to 14.
;
;
; HISTORY:
;
; Nov 27 1999, Armando Riccardi
; <riccardi@riccardi.arcetri.astro.it>
;
;
;-
;
function list2ranges, list, n_first_last
on_error, 2
n_first_last = 0L
if test_type(list, /NOFLOATING, N_EL=n) then begin
message, 'list must be a vector of byte, int or long'
endif
the_list = list[sort(list)] ; an ordered list is needed
the_list = the_list[uniq(the_list)] ; a not redundant list is needed
n = n_elements(the_list)
if n eq 1 then begin
n_first_last = 1L
ranges = [the_list[0], the_list[0]]
endif else begin
last = long(shift(the_list, -1)) - long(the_list)
first = long(the_list) - long(shift(the_list, 1))
idx_first = where(first gt 1 or first lt 0, first_count)
idx_last = where(last gt 1 or last lt 0, last_count)
if first_count ne last_count or first_count eq 0 then begin
message, 'The algorithm for the first/last choice failed'
endif
n_first_last = first_count
ranges = [transpose(the_list[idx_first]), transpose(the_list[idx_last])]
endelse
return, ranges
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -