Category talk:Wren-array: Difference between revisions
Content added Content deleted
(Added ByteArray class.) |
(→Source code: Numerous changes to BitArray and ByteArray classes including new methods.) |
||
Line 161: | Line 161: | ||
BitArray represents a List<Bool> whose size cannot be changed after it has been constructed |
BitArray represents a List<Bool> whose size cannot be changed after it has been constructed |
||
but whose elements can be changed. It uses only 1/32nd as much memory as a 'normal' List<Bool> |
but whose elements can be changed. It uses only 1/32nd as much memory as a 'normal' List<Bool> |
||
but is around 4 times slower to index |
but is around 4 times slower to index. |
||
*/ |
*/ |
||
class BitArray { |
class BitArray is Sequence { |
||
// Constructs a new BitArray of a given size and sets all elements to the same value 'v'. |
// Constructs a new BitArray of a given size and sets all elements to the same value 'v'. |
||
// 'size' is rounded to the higher multiple of 32 where necessary. |
// 'size' is rounded to the higher multiple of 32 where necessary. |
||
Line 171: | Line 171: | ||
_len = (size / 32).ceil |
_len = (size / 32).ceil |
||
_a = List.filled(_len, v ? 4294967295 : 0) |
_a = List.filled(_len, v ? 4294967295 : 0) |
||
_rng = 0..._len * 32 |
|||
} |
} |
||
Line 182: | Line 183: | ||
copy() { |
copy() { |
||
var c = BitArray.new(count, false) |
var c = BitArray.new(count, false) |
||
for (i in |
for (i in _rng) c[i] = this[i] |
||
return c |
return c |
||
} |
} |
||
Line 226: | Line 227: | ||
this[index] = v |
this[index] = v |
||
} |
} |
||
// Returns the index of 'v' in the current instance or -1 if 'v' is not found. |
|||
indexOf(v) { |
|||
for (i in _rng) if (this[i] == v) return i |
|||
return -1 |
|||
} |
|||
// Returns the index of the last occurrence of 'v' in the current instance |
|||
// or -1 if 'v' is not found. |
|||
lastIndexOf(v) { |
|||
for (i in count-1..0) if (this[i] == v) return i |
|||
return -1 |
|||
} |
|||
// Swaps the elements at index1 and index2 within the BitArray. |
|||
swap(index1, index2) { |
|||
var t = this[index1] |
|||
this[index1] = this[index2] |
|||
this[index2] = t |
|||
} |
|||
// Iterator protocol methods. |
|||
iterate(iterator) { _rng.iterate(iterator) } |
|||
iteratorValue(iterator) { this[iterator] } |
|||
// Returns a List<Bool> using the normal 8 bytes for each element. |
// Returns a List<Bool> using the normal 8 bytes for each element. |
||
toList { |
toList { |
||
var bools = List.filled(count, false) |
var bools = List.filled(count, false) |
||
for (i in |
for (i in _rng) bools[i] = this[i] |
||
return bools |
return bools |
||
} |
} |
||
Line 237: | Line 262: | ||
toArray { |
toArray { |
||
var bools = Array.new(count, false) |
var bools = Array.new(count, false) |
||
for (i in |
for (i in _rng) bools[i] = this[i] |
||
return bools |
return bools |
||
} |
} |
||
Line 244: | Line 269: | ||
toString { |
toString { |
||
var bytes = List.filled(count, 0) |
var bytes = List.filled(count, 0) |
||
for (i in |
for (i in _rng) if (this[i]) bytes[i] = 1 |
||
return bytes.join() |
return bytes.join() |
||
} |
} |
||
Line 253: | Line 278: | ||
but whose elements can be changed. A 'Byte' for this purpose is an integral Num with a value |
but whose elements can be changed. A 'Byte' for this purpose is an integral Num with a value |
||
between 0 and 255 inclusive. It uses only a quarter as much memory as a 'normal' List<Byte> |
between 0 and 255 inclusive. It uses only a quarter as much memory as a 'normal' List<Byte> |
||
but is around 4 times slower to index |
but is around 4 times slower to index. |
||
*/ |
*/ |
||
class ByteArray { |
class ByteArray is Sequence { |
||
// Constructs a new ByteArray of a given size and sets all elements to the same value 'v'. |
// Constructs a new ByteArray of a given size and sets all elements to the same value 'v'. |
||
// 'size' is rounded to the higher multiple of 4 where necessary. |
// 'size' is rounded to the higher multiple of 4 where necessary. |
||
Line 265: | Line 290: | ||
v = (v == 0) ? 0 : v | v << 8 | v << 16 | v << 24 |
v = (v == 0) ? 0 : v | v << 8 | v << 16 | v << 24 |
||
_a = List.filled(_len, v) |
_a = List.filled(_len, v) |
||
_rng = 0...4 *_len |
|||
} |
} |
||
Line 293: | Line 319: | ||
} |
} |
||
} |
} |
||
_rng = 0...4 *_len |
|||
} |
} |
||
Line 331: | Line 358: | ||
copy() { |
copy() { |
||
var c = ByteArray.new(count, 0) |
var c = ByteArray.new(count, 0) |
||
for (i in |
for (i in _rng) c[i] = this[i] |
||
return c |
return c |
||
} |
} |
||
Line 375: | Line 402: | ||
this[index] = v |
this[index] = v |
||
} |
} |
||
// Returns the index of 'v' in the current instance or -1 if 'v' is not found. |
|||
indexOf(v) { |
|||
for (i in _rng) if (this[i] == v) return i |
|||
return -1 |
|||
} |
|||
// Returns the index of the last occurrence of 'v' in the current instance |
|||
// or -1 if 'v' is not found. |
|||
lastIndexOf(v) { |
|||
for (i in count-1..0) if (this[i] == v) return i |
|||
return -1 |
|||
} |
|||
// Replaces all occurrences of 'old' by 'new' in the current instance |
|||
// and returns ['old', 'new']. |
|||
replace(old, new) { |
|||
for (i in _rng) if (this[i] == old) this[i] = new |
|||
return [old, new] |
|||
} |
|||
// Swaps the elements at index1 and index2 within the ByteArray. |
|||
swap(index1, index2) { |
|||
var t = this[index1] |
|||
this[index1] = this[index2] |
|||
this[index2] = t |
|||
} |
|||
// Applies a function to each element of the ByteArray. |
|||
apply(fn) { |
|||
Check.func("fn", fn, 1) |
|||
for (i in 0..._rng) this[i] = fn.call(this[i]) |
|||
} |
|||
// Iterator protocol methods. |
|||
iterate(iterator) { _rng.iterate(iterator) } |
|||
iteratorValue(iterator) { this[iterator] } |
|||
// Returns a List<Byte> using the normal 8 bytes for each element. |
// Returns a List<Byte> using the normal 8 bytes for each element. |
||
toList { |
toList { |
||
var bytes = List.filled(count, 0) |
var bytes = List.filled(count, 0) |
||
for (i in |
for (i in _rng) bytes[i] = this[i] |
||
return bytes |
return bytes |
||
} |
} |
||
Line 386: | Line 450: | ||
toArray { |
toArray { |
||
var bytes = Array.new(count, 0) |
var bytes = Array.new(count, 0) |
||
for (i in |
for (i in _rng) bytes[i] = this[i] |
||
return bytes |
return bytes |
||
} |
} |