Re: Dynamically Sized Structs

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On Fri, 18 Apr 2014 00:05:03 -0400, Kagamin s...@here.lot wrote: Well, it's proof of concept of bound checked variable-size struct, I wrote it in a minute. It even compiles and runs. Please take no offense :) I just was pointing out a difference between a hand-managed and hand-written

Re: Dynamically Sized Structs

2014-04-18 Thread Kagamin via Digitalmars-d-learn
Oh, and I don't believe, that a variable-size struct can be emplaced inside fixed-size struct or class. Only as a smart pointer from the example.

Re: Dynamically Sized Structs

2014-04-18 Thread Kagamin via Digitalmars-d-learn
I mean, it doesn't cover all scenarios, but can be extended to support them. The indexes array does just that: it's emplaced without indirection, but still is bound checked.

Re: Dynamically Sized Structs

2014-04-18 Thread Kagamin via Digitalmars-d-learn
On Friday, 18 April 2014 at 13:10:28 UTC, Steven Schveighoffer wrote: Note, you could probably, with mixin magic, make a version that could be emplaced inside a struct or class without an extra indirection. Speaking about mixin magic, you probably suggest to do it overly generically, though

Re: Dynamically Sized Structs

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On Fri, 18 Apr 2014 12:59:35 -0400, Kagamin s...@here.lot wrote: Oh, and I don't believe, that a variable-size struct can be emplaced inside fixed-size struct or class. Only as a smart pointer from the example. It goes at the end. Then you need to allocate the whole thing with that in

Re: Dynamically Sized Structs

2014-04-17 Thread monarch_dodra
On Thursday, 17 April 2014 at 00:55:19 UTC, Dicebot wrote: Just in case, the key line to pay attention to in that example is this one: CellIndex[0] c_; It is a commonly used C idiom for dynamically sized structures that D also supports. Absolutely. However, from a technical point of view,

Re: Dynamically Sized Structs

2014-04-17 Thread bearophile
Dicebot: Just in case, the key line to pay attention to in that example is this one: CellIndex[0] c_; When you define dynamically sized structs it's also a good idea to wrap the items access with some kind of get/set functions, to make the code less bug-prone (and sometimes there is also

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin
Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin
On Thursday, 17 April 2014 at 17:15:15 UTC, Kagamin wrote: Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3 Well, indexes getter could take cellCount into account...

Re: Dynamically Sized Structs

2014-04-17 Thread bearophile
Kagamin: Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3 I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels. Bye, bearophile

Re: Dynamically Sized Structs

2014-04-17 Thread Dicebot
On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote: Kagamin: Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3 I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels.

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin
On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote: Kagamin: Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3 I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels.

Re: Dynamically Sized Structs

2014-04-17 Thread Dicebot
On Thursday, 17 April 2014 at 17:45:22 UTC, Kagamin wrote: On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote: Kagamin: Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3 I think you are missing one of the main points of a variable sized struct,

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin
State* pointer in sokoban example is a perfect 1 indirection.

Re: Dynamically Sized Structs

2014-04-17 Thread Dicebot
On Thursday, 17 April 2014 at 18:17:59 UTC, Kagamin wrote: State* pointer in sokoban example is a perfect 1 indirection. It is not related to actual dynamic struct thing, which is why I have highlighted the line to look at. Minimal example is this: struct Dynamic { size_t length;

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin
How is this different from my example?

Re: Dynamically Sized Structs

2014-04-17 Thread Dicebot via Digitalmars-d-learn
On Thursday, 17 April 2014 at 18:29:21 UTC, Kagamin wrote: How is this different from my example? b = new byte[StateImpl.sizeof + CellIndex.sizeof*cellCount]; this line creates heap indirection

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin via Digitalmars-d-learn
So you assert that variable length structs can't be allocated on heap and sokoban example is a wrong example of variable length struct usage? And how heap indirection is different from stack indirection? It's still indirection.

Re: Dynamically Sized Structs

2014-04-17 Thread John Colvin via Digitalmars-d-learn
On Thursday, 17 April 2014 at 17:50:30 UTC, Dicebot wrote: On Thursday, 17 April 2014 at 17:45:22 UTC, Kagamin wrote: On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote: Kagamin: Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3 I think you are

Re: Dynamically Sized Structs

2014-04-17 Thread John Colvin via Digitalmars-d-learn
On Thursday, 17 April 2014 at 18:40:27 UTC, Kagamin wrote: And how heap indirection is different from stack indirection? It's still indirection. Locality. The stack is (within reason) readily available in cache.

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin via Digitalmars-d-learn
Well, cache locality can be optimized without reducing number of indirections, as long as the data is likely to be in cache. I agree with bearophile that variable size structs reduce number of indirections and have no direct relation to cache locality. One may have no time or no desire to

Re: Dynamically Sized Structs

2014-04-17 Thread Steven Schveighoffer via Digitalmars-d-learn
On Thu, 17 Apr 2014 14:40:25 -0400, Kagamin s...@here.lot wrote: So you assert that variable length structs can't be allocated on heap and sokoban example is a wrong example of variable length struct usage? And how heap indirection is different from stack indirection? It's still

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin via Digitalmars-d-learn
Well, it's proof of concept of bound checked variable-size struct, I wrote it in a minute. It even compiles and runs.

Re: Dynamically Sized Structs

2014-04-17 Thread Kagamin via Digitalmars-d-learn
On Wednesday, 16 April 2014 at 23:36:05 UTC, bearophile wrote: Jeroen Bollen: Is it possible to have a structure with a dynamic size? See an usage example I have written here: http://rosettacode.org/wiki/Sokoban#Faster_Version This can illustrate 1. fairly straightforward translation of

Dynamically Sized Structs

2014-04-16 Thread Jeroen Bollen
Is it possible to have a structure with a dynamic size? The structure would contain an array. I know I can use templates, but the size won't be known at compile time. I also know I could just put a dynamic array into it, but that way it would just be a pointer. I know there would be major

Re: Dynamically Sized Structs

2014-04-16 Thread Justin Whear
On Wed, 16 Apr 2014 23:15:40 +, Jeroen Bollen wrote: Is it possible to have a structure with a dynamic size? The structure would contain an array. I know I can use templates, but the size won't be known at compile time. I also know I could just put a dynamic array into it, but that way

Re: Dynamically Sized Structs

2014-04-16 Thread bearophile
Jeroen Bollen: Is it possible to have a structure with a dynamic size? See an usage example I have written here: http://rosettacode.org/wiki/Sokoban#Faster_Version But that code requires a very updated compiler. Otherwise you will need a little different code. Bye, bearophile

Re: Dynamically Sized Structs

2014-04-16 Thread Rene Zwanenburg
On Wednesday, 16 April 2014 at 23:15:43 UTC, Jeroen Bollen wrote: Is it possible to have a structure with a dynamic size? The structure would contain an array. I know I can use templates, but the size won't be known at compile time. I also know I could just put a dynamic array into it, but

Re: Dynamically Sized Structs

2014-04-16 Thread Dicebot
On Wednesday, 16 April 2014 at 23:36:05 UTC, bearophile wrote: Jeroen Bollen: Is it possible to have a structure with a dynamic size? See an usage example I have written here: http://rosettacode.org/wiki/Sokoban#Faster_Version But that code requires a very updated compiler. Otherwise you