Memory layout of a data structure: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: Fix comment: Perl 6 --> Raku) |
(Added Wren) |
||
Line 1,208: | Line 1,208: | ||
if {$res ne $expected} {puts "$test -> $res, expected $expected"} |
if {$res ne $expected} {puts "$test -> $res, expected $expected"} |
||
}</lang> |
}</lang> |
||
=={{header|Wren}}== |
|||
{{libheader|Wren-seq}} |
|||
{{libheader|Wren-fmt}} |
|||
In Wren instances of user defined data structures ('classes') are always allocated on the heap and (AFAIK) their fields are laid out contiguously and require 8 bytes each. Nums, Bools and Nulls are stored directly and fields of other types store a pointer to their actual storage location. |
|||
It is not possible to vary this memory layout though, if lack of memory is a problem, one may be able to use bit arithmetic to pack more values into a Num. |
|||
As far as this task is concerned, below is a possible implementation for a 9-pin R-232 plug which allows access by pin name or number and provides a comprehensive print out of the current pin state. |
|||
<lang ecmascript>import "/seq" for Lst |
|||
import "/fmt" for Fmt |
|||
var ON = true |
|||
var OFF = false |
|||
// Converts "ON"/"OFF" string to true/false. |
|||
var AsBool = Fn.new { |s| s == "ON" } |
|||
class RS232_9 { |
|||
static names { ["CD", "RD", "TD", "DTR", "SG", "DSR", "RTS", "CTS", "RI"] } |
|||
construct new() { _settings = [OFF] * 9 } // all pins OFF |
|||
// get pin setting as an ON/OFF string by pin name or number; returns null if invalid |
|||
[p] { |
|||
if (p is String) { |
|||
var ix = Lst.indexOf(RS232_9.names, p) |
|||
return (ix >= 0 && ix < 9) ? (_settings[ix] ? "ON" : "OFF") : null |
|||
} |
|||
if (p is Num) { |
|||
return (p.isInteger && p >= 1 && p <= 9) ? (_settings[p-1] ? "ON" : "OFF") : null |
|||
} |
|||
return null |
|||
} |
|||
// set pin by pin name or number; does nothing if invalid |
|||
[p] = (v) { |
|||
if (v.type == String && (v == "ON" || v == "OFF")) v = AsBool.call(v) |
|||
if (v.type != Bool) return |
|||
if (p is String) { |
|||
var ix = Lst.indexOf(RS232_9.names, p) |
|||
if (ix >= 0 && ix < 9) _settings[ix] = v |
|||
} |
|||
if (p is Num && p.isInteger && p >= 1 && p <= 9) _settings[p-1] = v |
|||
} |
|||
// prints all pin settings |
|||
toString { (1..9).map { |i| "%(i) %(Fmt.s(-3, RS232_9.names[i-1])) = %(this[i])" }.join("\n") } |
|||
} |
|||
var plug = RS232_9.new() |
|||
plug["CD"] = ON // set pin 1 by name |
|||
plug[3] = ON // set pin 3 by number |
|||
plug["DSR"] = "ON" // set pin 6 by name and using a string |
|||
System.print(plug) // print the state of the pins</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 CD = ON |
|||
2 RD = OFF |
|||
3 TD = ON |
|||
4 DTR = OFF |
|||
5 SG = OFF |
|||
6 DSR = ON |
|||
7 RTS = OFF |
|||
8 CTS = OFF |
|||
9 RI = OFF |
|||
</pre> |
|||
{{omit from|ACL2}} |
{{omit from|ACL2}} |