Category talk:Wren-seq: Difference between revisions
Content added Content deleted
(→Source code: Added indexOf method to FrozenList class.) |
(→Source code: Added 'indexOfSlice' and 'isSliceOf' methods to Lst class.) |
||
Line 120: | Line 120: | ||
static indexOf(a, value) { indexOf(a, value, 0) } |
static indexOf(a, value) { indexOf(a, value, 0) } |
||
static indexOfAny(a, values) { indexOfAny(a, values, 0) } |
static indexOfAny(a, values) { indexOfAny(a, values, 0) } |
||
// Returns the index at which the slice 's' is first found in 'a' or -1 if it is not present. |
|||
static indexOfSlice(a, s) { |
|||
isList_(a) |
|||
isList_(s) |
|||
var ac = a.count |
|||
var sc = s.count |
|||
if (ac == 0 || sc > ac) return -1 |
|||
if (sc == 0) return 0 |
|||
if (ac == 1) return (a[0] == s[0]) ? 0 : -1 |
|||
if (sc == 1) return a.indexOf(s[0]) |
|||
for (i in 0..ac-sc) { |
|||
if (a[i] == s[0]) { |
|||
var ok = true |
|||
for (j in i+1...i + sc) { |
|||
if (a[j] != s[j - i]) { |
|||
ok = false |
|||
break |
|||
} |
|||
} |
|||
if (ok) return i |
|||
} |
|||
} |
|||
return -1 |
|||
} |
|||
// Returns true if 's' is a slice of 'a' or false otherwise. |
|||
static isSliceOf(a, s) { indexOfSlice(a, s) >= 0 } |
|||
// Exchanges the elements at indices 'i' and 'j' of 'a'. |
// Exchanges the elements at indices 'i' and 'j' of 'a'. |