Category talk:Wren-array: Difference between revisions

→‎Source code: Numerous changes to BitArray and ByteArray classes including new methods.
(Added ByteArray class.)
(→‎Source code: Numerous changes to BitArray and ByteArray classes including new methods.)
Line 161:
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 is around 4 times slower to index. Also, unlike List<Bool>, BitArray is not a Sequence.
*/
class BitArray is Sequence {
// 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.
Line 171:
_len = (size / 32).ceil
_a = List.filled(_len, v ? 4294967295 : 0)
_rng = 0..._len * 32
}
 
Line 182 ⟶ 183:
copy() {
var c = BitArray.new(count, false)
for (i in 0...count_rng) c[i] = this[i]
return c
}
Line 226 ⟶ 227:
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.
toList {
var bools = List.filled(count, false)
for (i in 0...count_rng) bools[i] = this[i]
return bools
}
Line 237 ⟶ 262:
toArray {
var bools = Array.new(count, false)
for (i in 0...count_rng) bools[i] = this[i]
return bools
}
Line 244 ⟶ 269:
toString {
var bytes = List.filled(count, 0)
for (i in 0...count_rng) if (this[i]) bytes[i] = 1
return bytes.join()
}
Line 253 ⟶ 278:
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>
but is around 4 times slower to index. Also, unlike List<Byte>, ByteArray is not a Sequence.
*/
class ByteArray is Sequence {
// 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.
Line 265 ⟶ 290:
v = (v == 0) ? 0 : v | v << 8 | v << 16 | v << 24
_a = List.filled(_len, v)
_rng = 0...4 *_len
}
 
Line 293 ⟶ 319:
}
}
_rng = 0...4 *_len
}
 
Line 331 ⟶ 358:
copy() {
var c = ByteArray.new(count, 0)
for (i in 0...count_rng) c[i] = this[i]
return c
}
Line 375 ⟶ 402:
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.
toList {
var bytes = List.filled(count, 0)
for (i in 0...count_rng) bytes[i] = this[i]
return bytes
}
Line 386 ⟶ 450:
toArray {
var bytes = Array.new(count, 0)
for (i in 0...count_rng) bytes[i] = this[i]
return bytes
}
9,476

edits