> http://en.wikipedia.org/wiki/Tagged_union
>
> Oh Go Gods, was something like this considered for Go?
http://golang.org/doc/go_faq.html#variant_types
Ian
> For Go I can:
> (a) Store four pointers and two flag bits and waste space.
> (b) Store two pointers and flag bits and use Unsafe to recast
> (c) ? what else... this is my question. is there small/fast solution that
> keeps both pointers valid?
Another possibility is to make each node an interface value, and store
pointers in each interface value. But this does do more memory
allocation than the C solution.
Ian
I think that solution might be liable to horrible failure when the GC gets
better.
You could use exactly four pointers, as Ian says. If the size of O
fits within a pointer, the overhead won't be too bad.
I was thinking of the case where you store a struct with two pointers
into the interface. That doesn't fit in the interface, so there is an
extra allocation there.
But, you're right, you could structure it differently.
Ian
On Apr 21, 2012 1:55 AM, "Michael Jones" <m...@google.com> wrote:
> Thanks! Maybe I'll just use unsafe and the LSB pointer hack then and consider that idiomatic.
Using unsafe, while sometimes necessary, is never idiomatic.
Dave.
I don't know how to code crit-bit trees gracefully in Go.
In the present C version, to save space, pointer type is encoded in each pointer's LSB.
Given my background, this is the natural, graceful expression. ;-)
I don't know how to code crit-bit trees gracefully in Go.
My first thought was to propose an extension for Go 2.
My second thought was to appeal for advice in Go 1.
Goal:
I want to organize a collection of objects of type O
I need ia tree structure of nodes (Type T) each with a little data and two pointers.
Each pointer points to either a subordinate tree node (type T) or object (type O), or be nil.
In the present C version, to save space, pointer type is encoded in each pointer's LSB.
Given my background, this is the natural, graceful expression. ;-)
For Go I can:
(a) Store four pointers and two flag bits and waste space.
(b) Store two pointers and flag bits and use Unsafe to recast
(c) ? what else... this is my question. is there small/fast solution that keeps both pointers valid?
type childKind = (node, string);shape = recordpayload : integer;case leftKind : childKind ofnode : ...string : ...case rightKind : childKind ofnode : ...string : ...end;