Re: Bug in usage of associative array: dynamic array with string as a key

2023-07-01 Thread Ali Çehreli via Digitalmars-d-learn
de inside a 'void main()' function 2) Pasting the code from the top of that page and it works: void main() { int[string] aa; // Associative array of ints that are // indexed by string keys. // The KeyType is string. aa["hello"] = 3; // set value associated with key "h

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn
ld printfs it seems to be the case that the array is being populated (elsewhere) with the expected correct values. Taking out the if doesn’t seem to help either. I don’t have a way of examining the contents of the dynamic array directly to check that they are actually being stored as expected, other t

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn
On Friday, 30 June 2023 at 21:25:23 UTC, H. S. Teoh wrote: On Fri, Jun 30, 2023 at 07:05:23PM +, Cecil Ward via Digitalmars-d-learn wrote: [...] It would help if you could post the complete code that reproduces the problem. Or, if you do not wish to reveal your code, reduce it to a

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread mw via Digitalmars-d-learn
https://forum.dlang.org/thread/duetqujuoceancqtj...@forum.dlang.org Try HashMap see if it is still a problem. If no, then it's another example of the built in AA problem.

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Jun 30, 2023 at 07:05:23PM +, Cecil Ward via Digitalmars-d-learn wrote: [...] It would help if you could post the complete code that reproduces the problem. Or, if you do not wish to reveal your code, reduce it to a minimal case that still exhibits the same problem, so that we can

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Ali Çehreli via Digitalmars-d-learn
On 6/30/23 13:16, Cecil Ward wrote: On Friday, 30 June 2023 at 19:58:39 UTC, FeepingCreature wrote: Note that you can do `uint ordinal = Decls.ordinals.get(str, -1);`. Is the second argument an ‘else’ then, my friend? Yes, .get and friends appear in this table:

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn
On Friday, 30 June 2023 at 19:58:39 UTC, FeepingCreature wrote: On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote: I have code roughly like the following: dstring str = "name"d; uint ordinal = (( str in Decls.ordinals ) !is null) ? Decls.ordinals[ str ] : -1; struct Decls

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn
On Friday, 30 June 2023 at 20:12:08 UTC, Ali Çehreli wrote: On 6/30/23 12:05, Cecil Ward wrote: > I have code roughly like the following: > > dstring str = "name"d; Aside: One almost never needs dstring. > uint ordinal = (( str in Decls.ordinals ) !is null) ? > Decls.ordinals[ str ]

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Ali Çehreli via Digitalmars-d-learn
On 6/30/23 12:05, Cecil Ward wrote: > I have code roughly like the following: > > dstring str = "name"d; Aside: One almost never needs dstring. > uint ordinal = (( str in Decls.ordinals ) !is null) ? > Decls.ordinals[ str ] : -1; > > struct Decls > { > uint[ dstring]

Re: Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread FeepingCreature via Digitalmars-d-learn
On Friday, 30 June 2023 at 19:05:23 UTC, Cecil Ward wrote: I have code roughly like the following: dstring str = "name"d; uint ordinal = (( str in Decls.ordinals ) !is null) ? Decls.ordinals[ str ] : -1; struct Decls { uint[ dstring] ordinals; } //and

Bug in usage of associative array: dynamic array with string as a key

2023-06-30 Thread Cecil Ward via Digitalmars-d-learn
it always returns ordinal== -1 from the expression. Can you sort me out? I took this from the example given in the language reference under arrays, testing for membership (or similar, I forget the subssection title). From good old printfs it seems to be the case that the array is being

Re: Dynamic array + AA array

2019-09-17 Thread Brett via Digitalmars-d-learn
On Tuesday, 17 September 2019 at 15:51:34 UTC, Andrea Fontana wrote: On Tuesday, 17 September 2019 at 14:33:30 UTC, Brett wrote: The idea is to basically use a dynamic array for most of the items, then an array to get the rest. T[] Base; T[int] Rest; Then if Base has a max size(usually

Re: Dynamic array + AA array

2019-09-17 Thread Andrea Fontana via Digitalmars-d-learn
On Tuesday, 17 September 2019 at 14:33:30 UTC, Brett wrote: The idea is to basically use a dynamic array for most of the items, then an array to get the rest. T[] Base; T[int] Rest; Then if Base has a max size(usually it might be fixed due to some algorithm) the Rest AA can pick up any

Re: Dynamic array + AA array

2019-09-17 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 17 September 2019 at 14:33:30 UTC, Brett wrote: The idea is to basically use a dynamic array for most of the items, then an array to get the rest. T[] Base; T[int] Rest; Then if Base has a max size(usually it might be fixed due to some algorithm) the Rest AA can pick up any

Dynamic array + AA array

2019-09-17 Thread Brett via Digitalmars-d-learn
The idea is to basically use a dynamic array for most of the items, then an array to get the rest. T[] Base; T[int] Rest; Then if Base has a max size(usually it might be fixed due to some algorithm) the Rest AA can pick up any outside values easily. The idea here is to be able to combine

Re: How to use an associative array with array values.

2018-03-21 Thread Jesse Phillips via Digitalmars-d-learn
On Wednesday, 21 March 2018 at 18:31:29 UTC, tipdbmp wrote: I see. I guess the other would be: { int[8192] bar; int[8192][string] foo; foo["a"] = bar; foo["a"][8191] = -1; } https://run.dlang.io/is/AK2X2t Are you looking to use static arrays or dynamic? You can't use the new

Re: How to use an associative array with array values.

2018-03-21 Thread tipdbmp via Digitalmars-d-learn
I see. I guess the other would be: { int[8192] bar; int[8192][string] foo; foo["a"] = bar; foo["a"][8191] = -1; }

Re: How to use an associative array with array values.

2018-03-21 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 21 March 2018 at 15:53:32 UTC, tipdbmp wrote: int[10][string] foo; One option is to initialize like this --- void main() { int[10][string] foo; if("a" !in foo) foo["a"] = [0,0,0,0,0,0,0,0,0,0]; // set all to zero to create the key foo["a"][4] = 4; // now valid to

How to use an associative array with array values.

2018-03-21 Thread tipdbmp via Digitalmars-d-learn
// foo is an associative array/hashtable with // key type: string // value type: int[10] // int[10][string] foo; // foo["a"] = new int[10]; // Range violation at runtime // foo["a"][0] = 1; // Range violation at runtime

Re: Initialise dynamic array in array of structures

2016-06-22 Thread ketmar via Digitalmars-d-learn
On Wednesday, 22 June 2016 at 09:27:01 UTC, cym13 wrote: what i meant is that "{}" should be fully equivalent to "Struct()" ctor in terms of calling postblits, and it isn't.

Re: Initialise dynamic array in array of structures

2016-06-22 Thread ketmar via Digitalmars-d-learn
On Wednesday, 22 June 2016 at 09:27:01 UTC, cym13 wrote: On the other hand I don't see why you'd expect {} to call postblit at all. 'cause it essentially makes a copy. i gave the sample in bugreport. it worth me a hour of debugging to find why my refcounted struct keep crashing with invalid

Re: Initialise dynamic array in array of structures

2016-06-22 Thread cym13 via Digitalmars-d-learn
On Wednesday, 22 June 2016 at 08:06:26 UTC, ketmar wrote: On Wednesday, 22 June 2016 at 06:43:12 UTC, Paul wrote: Why is initialisation via {} bad (in simple terms please :D)? first, it is buggy. i.e. it doesn't always call postblit[1]. second, it's syntax is the same as the syntax of

Re: Initialise dynamic array in array of structures

2016-06-22 Thread ketmar via Digitalmars-d-learn
On Wednesday, 22 June 2016 at 06:43:12 UTC, Paul wrote: Why is initialisation via {} bad (in simple terms please :D)? first, it is buggy. i.e. it doesn't always call postblit[1]. second, it's syntax is the same as the syntax of argument-less lambda, which makes it context-dependent -- so

Re: Initialise dynamic array in array of structures

2016-06-22 Thread Paul via Digitalmars-d-learn
likely I need a better way to store the information. @ketmar: Why is initialisation via {} bad (in simple terms please :D)? I noticed while working on this bit of code that I could initialise simple struct variables with {} but couldn't do the same with an array of structs. I guess

Re: Initialise dynamic array in array of structures

2016-06-21 Thread ketmar via Digitalmars-d-learn
On Tuesday, 21 June 2016 at 19:33:31 UTC, cym13 wrote: i would want him to figure that by himself, tbh. just to remember that "{}" struct initialization is BAD. ;-)

Re: Initialise dynamic array in array of structures

2016-06-21 Thread cym13 via Digitalmars-d-learn
On Tuesday, 21 June 2016 at 19:15:56 UTC, Paul wrote: Given these structures and declaration: struct CoordList{ int x, y; } struct Part{ int x, y; CoordList[] coords; int nextNode, prevNode; string nextEnter, prevEnter; } Part[10] trackTemplates; Can

Re: Initialise dynamic array in array of structures

2016-06-21 Thread ketmar via Digitalmars-d-learn
trackTemplates[0].coords = [ CoordList(0, 9), CoordList(1, 1), CoordList(3, 6) ];

Initialise dynamic array in array of structures

2016-06-21 Thread Paul via Digitalmars-d-learn
Given these structures and declaration: struct CoordList{ int x, y; } struct Part{ int x, y; CoordList[] coords; int nextNode, prevNode; string nextEnter, prevEnter; } Part[10] trackTemplates; Can someone please tell me why I can't initialise

Re: Making associatvie array from array of pairs

2013-12-25 Thread Dfr
On Tuesday, 24 December 2013 at 14:39:16 UTC, Timon Gehr wrote: On 12/24/2013 12:36 PM, Dfr wrote: Let's say i have array of kind: auto a = [[1,FF], [2, 00FF00], ...]; Is there simple way to turn it into associative array of kind: string[string] b = [1: FF, 2: 00FF00, ...]; void

Re: Making associatvie array from array of pairs

2013-12-25 Thread bearophile
Dfr: This example looks cleanest, but not compile with error: Error: no property 'assocArray' for type 'MapResult!(__lambda9, immutable(char[][])[])' It compiles for me. Bye, bearophile

Re: Making associatvie array from array of pairs

2013-12-25 Thread Dfr
On Wednesday, 25 December 2013 at 14:44:57 UTC, bearophile wrote: Dfr: This example looks cleanest, but not compile with error: Error: no property 'assocArray' for type 'MapResult!(__lambda9, immutable(char[][])[])' It compiles for me. Bye, bearophile Sorry, just forgot to import

Making associatvie array from array of pairs

2013-12-24 Thread Dfr
Let's say i have array of kind: auto a = [[1,FF], [2, 00FF00], ...]; Is there simple way to turn it into associative array of kind: string[string] b = [1: FF, 2: 00FF00, ...];

Re: Making associatvie array from array of pairs

2013-12-24 Thread Gary Willoughby
On Tuesday, 24 December 2013 at 11:36:23 UTC, Dfr wrote: Let's say i have array of kind: auto a = [[1,FF], [2, 00FF00], ...]; Is there simple way to turn it into associative array of kind: string[string] b = [1: FF, 2: 00FF00, ...]; You can if the initial array was comprised

Re: Making associatvie array from array of pairs

2013-12-24 Thread Philippe Sigaud
On Tue, Dec 24, 2013 at 12:36 PM, Dfr defle...@yandex.ru wrote: Let's say i have array of kind: auto a = [[1,FF], [2, 00FF00], ...]; Is there simple way to turn it into associative array of kind: string[string] b = [1: FF, 2: 00FF00, ...]; To build a value (your b) from a range

Re: Making associatvie array from array of pairs

2013-12-24 Thread bearophile
Philippe Sigaud: auto a = [[1,FF], [2, 00FF00]]; import std.algorithm: reduce; string[string] b; b = reduce!((aa, pair) { aa[pair[0]] = pair[1]; return aa;})(b,a); writeln(b); While this code seems correct (and I think it's kind of common in Scala), I consider it an

Re: Making associatvie array from array of pairs

2013-12-24 Thread Philippe Sigaud
On Tue, Dec 24, 2013 at 1:12 PM, bearophile bearophileh...@lycos.com wrote: Philippe Sigaud: auto a = [[1,FF], [2, 00FF00]]; import std.algorithm: reduce; string[string] b; b = reduce!((aa, pair) { aa[pair[0]] = pair[1]; return aa;})(b,a); writeln(b); While this

Re: Making associatvie array from array of pairs

2013-12-24 Thread monarch_dodra
On Tuesday, 24 December 2013 at 12:33:13 UTC, Philippe Sigaud wrote: I don't know. I really consider `reduce` a nice way to collapse a structure down to a value. I find it in many different places. Sure, map and filter are more usual, but reduce is not far behind. you use map and filter to

Re: Making associatvie array from array of pairs

2013-12-24 Thread Timon Gehr
On 12/24/2013 12:36 PM, Dfr wrote: Let's say i have array of kind: auto a = [[1,FF], [2, 00FF00], ...]; Is there simple way to turn it into associative array of kind: string[string] b = [1: FF, 2: 00FF00, ...]; void main(){ import std.array, std.algorithm, std.typecons

Re: Making associatvie array from array of pairs

2013-12-24 Thread Philippe Sigaud
On Tue, Dec 24, 2013 at 3:07 PM, monarch_dodra monarchdo...@gmail.com wrote: I don't know. I really consider `reduce` a nice way to collapse a structure down to a value. I find it in many different places. Sure, map and filter are more usual, but reduce is not far behind. you use map and

Re: Hole of new? (Re: Array of array)

2012-01-07 Thread Steven Schveighoffer
On Mon, 02 Jan 2012 18:30:52 -0500, Timon Gehr timon.g...@gmx.ch wrote: On 01/03/2012 12:02 AM, Mafi wrote: Am 02.01.2012 23:33, schrieb Timon Gehr: On 01/02/2012 11:21 PM, RenatoL wrote: Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T

Re: Hole of new? (Re: Array of array)

2012-01-07 Thread bearophile
Steven Schveighoffer: Interesting trivia, the compiler actually transforms the following: struct S { int x; } auto s = new S; into this: auto s = (new S[1]).ptr; Found that out when revamping the array allocation code. It's one thing that still bugs me because this means

Re: Hole of new? (Re: Array of array)

2012-01-07 Thread Steven Schveighoffer
On Sat, 07 Jan 2012 13:29:25 -0500, bearophile bearophileh...@lycos.com wrote: Steven Schveighoffer: Interesting trivia, the compiler actually transforms the following: struct S { int x; } auto s = new S; into this: auto s = (new S[1]).ptr; Found that out when revamping the array

Re: Hole of new? (Re: Array of array)

2012-01-07 Thread bearophile
Steven Schveighoffer: Wasted space. An appendable array requires extra space at the end of the block to store the 'used' length. If the disadvantages are significant is this improvement in Bugzilla as enhancement request? It's not in bugzilla, but it's strictly a performance thing

Re: Hole of new? (Re: Array of array)

2012-01-07 Thread Steven Schveighoffer
On Sat, 07 Jan 2012 14:24:19 -0500, bearophile bearophileh...@lycos.com wrote: Steven Schveighoffer: Wasted space. An appendable array requires extra space at the end of the block to store the 'used' length. If the disadvantages are significant is this improvement in Bugzilla

Re: Hole of new? (Re: Array of array)

2012-01-07 Thread bearophile
Steven Schveighoffer: http://d.puremagic.com/issues/show_bug.cgi?id=7243 Thank you :-) I think in Bugzilla there is space for few performance-related enhancement requests too, if they are about very common/important parts of the language :-) Bye, bearophile

Array of array

2012-01-02 Thread RenatoL
auto r = new int[][5]; this is ok auto r = new int[][]; this is not ok Error: new can only create structs, dynamic arrays or class objects , not int[][]'s why?

Re: Array of array

2012-01-02 Thread Timon Gehr
Array(T){this(size_t length){...}} auto r = new Array!(Array!int); // error: missing constructor argument

Re: Array of array

2012-01-02 Thread RenatoL
Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why?

Re: Array of array

2012-01-02 Thread Timon Gehr
On 01/02/2012 11:21 PM, RenatoL wrote: Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why? It does work. Why do you think it does not? T[] a; // ok T[][] b

Hole of new? (Re: Array of array)

2012-01-02 Thread Mafi
Am 02.01.2012 23:33, schrieb Timon Gehr: On 01/02/2012 11:21 PM, RenatoL wrote: Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why? It does work. Why do you think it does not? T

Re: Array of array

2012-01-02 Thread RenatoL
I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok.

Re: Array of array

2012-01-02 Thread Timon Gehr
On 01/03/2012 12:03 AM, RenatoL wrote: I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok. new int[][3] is an alternate form of new int[][](3); new int[][3] allocates an int[][] with 3

Re: Hole of new? (Re: Array of array)

2012-01-02 Thread Timon Gehr
On 01/03/2012 12:02 AM, Mafi wrote: Am 02.01.2012 23:33, schrieb Timon Gehr: On 01/02/2012 11:21 PM, RenatoL wrote: Just curious... the answer of the compiler it's a bit unclear to me... T[] is a dynamic array of type T. T[][] is a dynamic array of T[]. But this doesn't work. Why? It does

Re: Array of array

2012-01-02 Thread Matej Nanut
On 3 January 2012 00:27, Timon Gehr timon.g...@gmx.ch wrote: On 01/03/2012 12:03 AM, RenatoL wrote: I have: auto r = new int[][]; Error: new can only create structs, dynamic arrays or class objects , not int[][]'s while auto r = new int[][3]; is ok. new int[][3] is an alternate

Re: Array of array

2012-01-02 Thread Manfred Nowak
RenatoL wrote: Error: new can only create structs, dynamic arrays or class objects, not int[][]'s There is an error in the error message: new can only create _static_ arrays. -manfred

Re: Array of array

2012-01-02 Thread Jonathan M Davis
On Monday, January 02, 2012 23:49:36 Manfred Nowak wrote: RenatoL wrote: Error: new can only create structs, dynamic arrays or class objects, not int[][]'s There is an error in the error message: new can only create _static_ arrays. Um no. new is used for creating _dynamic_ arrays, not

Re: Array of array

2012-01-02 Thread Timon Gehr
' and 'length'. 'ptr' is a pointer to the first element of the array, and 'length' indicates how many elements are in the array. int[] does not carry any data on its own: it is only a reference to the data. int[5] on the other hand is a data structure that contains 5 integers with the indices

Re: Array of array

2012-01-02 Thread Manfred Nowak
Jonathan M Davis wrote: new is used for creating _dynamic_ arrays, not static arrays. Correct, my fault. I meant something like statically initialized dynamic, because `new' currently needs an `uint' number to allocate some space for the elements of the outermost array. The posting shows

Re: When is array-to-array cast legal, and what does actually happen?

2010-02-24 Thread bearophile
Daniel Keep: [1] Except for int-float. Oh, and objects. Really, this is one thing I could just about strangle KR for: conflating value-preserving, non-value-preserving *AND* unsafe conversions all into a single construct. Walter, gets slapped with a fish for not putting a bullet in cast's

Re: When is array-to-array cast legal, and what does actually happen?

2010-02-23 Thread Lars T. Kyllingstad
negotiations. cast is specifically *designed* to circumvent the type system's protections [1]. If you want to do a value conversion, *do a value conversion*. Allocate a new array and convert each member. cast doesn't call the constructor or the postblit because it's doing a pointer conversion. Your code

Re: When is array-to-array cast legal, and what does actually happen?

2010-02-23 Thread div0
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Ali Çehreli wrote: Daniel Keep wrote: ... snip Well I forget the details, but it's been pointed out before that D's cast is fundamentally broken. You get one cast operator that hides the full set of c++ static, dynamic, const reinterpret casts.

When is array-to-array cast legal, and what does actually happen?

2010-02-22 Thread Ali Çehreli
Is the following cast legal with dmd 2.040? struct MyChar { dchar d; this(dchar param) in { assert(false); // not called } body { d = param; assert(d != param); // not called } this(this) in { assert(false); //

Re: When is array-to-array cast legal, and what does actually happen?

2010-02-22 Thread Daniel Keep
is specifically *designed* to circumvent the type system's protections [1]. If you want to do a value conversion, *do a value conversion*. Allocate a new array and convert each member. cast doesn't call the constructor or the postblit because it's doing a pointer conversion. Your code

Re: why the array bounds array

2008-12-08 Thread Sergey Gromov
Mon, 8 Dec 2008 06:59:40 + (UTC), BCS wrote: Reply to Bill, On Mon, Dec 8, 2008 at 2:57 PM, BCS [EMAIL PROTECTED] wrote: Reply to Michael P., rand() TYPES_OF_TILES never use rand like that (the low order bit on many rands toggles every single time) better (I think): rand() /

Re: why the array bounds array

2008-12-08 Thread BCS
Reply to Sergey, Mon, 8 Dec 2008 06:59:40 + (UTC), BCS wrote: better (I think): rand() / (RAND_MAX / TYPES_OF_TILES) Don't use rand() like this, ever. ;) RAND_MAX / TYPES_OF_TILES is an integer expression, it rounds down, therefore your formula gives a slightly greater range of

Re: why the array bounds array

2008-12-08 Thread Zarathustra
Michael P. Wrote: Okay, I'm getting an array bounds error, and I have no clue why. Here is the code that affect it: //Constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int TILE_WIDTH = 20; const int TILE_HEIGHT = 20; //how big one tile is, in pixels const int

why the array bounds array

2008-12-07 Thread Michael P.
Okay, I'm getting an array bounds error, and I have no clue why. Here is the code that affect it: //Constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int TILE_WIDTH = 20; const int TILE_HEIGHT = 20; //how big one tile is, in pixels const int NUMBER_OF_TILES_WIDTH

Re: why the array bounds array

2008-12-07 Thread BCS
Reply to Michael P., rand() TYPES_OF_TILES never use rand like that (the low order bit on many rands toggles every single time) better (I think): rand() / (TYPES_OF_TILES / RAND_MAX)

Re: why the array bounds array

2008-12-07 Thread Bill Baxter
On Mon, Dec 8, 2008 at 2:57 PM, BCS [EMAIL PROTECTED] wrote: Reply to Michael P., rand() TYPES_OF_TILES never use rand like that (the low order bit on many rands toggles every single time) better (I think): rand() / (TYPES_OF_TILES / RAND_MAX) That's a divide by zero, so I don't think

Re: why the array bounds array

2008-12-07 Thread BCS
Reply to Bill, On Mon, Dec 8, 2008 at 2:57 PM, BCS [EMAIL PROTECTED] wrote: Reply to Michael P., rand() TYPES_OF_TILES never use rand like that (the low order bit on many rands toggles every single time) better (I think): rand() / (TYPES_OF_TILES / RAND_MAX) That's a divide by zero, so I