www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Dynamic / resizable array type, and a crash problem

reply "ivoras" <ivoras gmail.com> writes:
What is the recommended dynamic array type in D? So far I found 
Array (specifically Array!string, as I need a dynamic array of 
strings), but my example program crashes:

https://gist.github.com/ivoras/2d7737c214c3dc937c28

The crash is at line 20:

core.exception.AssertError /usr/include/dmd/phobos/std/container/array.d(334): 
Assertion failure
----------------
./markov() [0x80fbfd1]
./markov(uint 
std.container.array.Array!(immutable(char)[]).Array.Payload.insertBack!(immutable(char)[]).insertBack(immu
able(char)[])+0x6f) 
[0x80fa997]
./markov(uint 
std.container.array.Array!(immutable(char)[]).Array.insertBack!(immutable(char)[]).insertBack(immu
able(char)[])+0x36) 
[0x80fa866]
./markov(_Dmain+0x167) [0x80d7ce7]

This is on DMD32 D Compiler v2.067.1
May 14 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
I would just use a regular `string[]` array...
May 14 2015
parent reply "ivoras" <ivoras gmail.com> writes:
On Thursday, 14 May 2015 at 12:46:48 UTC, Adam D. Ruppe wrote:
 I would just use a regular `string[]` array...
Is it resizable? Somehow I didn't get that impression from the docs. Apparently it doesn't even have an "insert" method: http://dlang.org/phobos/std_array.html .
May 14 2015
next sibling parent "Namespace" <rswhite4 gmail.com> writes:
On Thursday, 14 May 2015 at 13:26:27 UTC, ivoras wrote:
 On Thursday, 14 May 2015 at 12:46:48 UTC, Adam D. Ruppe wrote:
 I would just use a regular `string[]` array...
Is it resizable? Somehow I didn't get that impression from the docs. Apparently it doesn't even have an "insert" method: http://dlang.org/phobos/std_array.html .
---- string[] arr; arr ~= "Foo"; arr ~= "Bar"; writeln(arr, ':', arr.length); ---- It's all built in. ;) A nice article: http://dlang.org/d-array-article.html and the language reference: http://dlang.org/arrays.html
May 14 2015
prev sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 14 May 2015 at 13:26:27 UTC, ivoras wrote:
 Is it resizable?
You can append with the ~= operator and size down by slicing it.
 Apparently it doesn't even have an "insert" method: 
 http://dlang.org/phobos/std_array.html .
http://dlang.org/phobos/std_array.html#insertInPlace is the one you'd use for that.
May 14 2015
parent reply "ivoras" <ivoras gmail.com> writes:
On Thursday, 14 May 2015 at 13:50:17 UTC, Adam D. Ruppe wrote:
 On Thursday, 14 May 2015 at 13:26:27 UTC, ivoras wrote:
 Is it resizable?
You can append with the ~= operator and size down by slicing it.
 Apparently it doesn't even have an "insert" method: 
 http://dlang.org/phobos/std_array.html .
http://dlang.org/phobos/std_array.html#insertInPlace is the one you'd use for that.
Ok, string[] and ~= work and it doesn't crash now. Where would I look for documentation on the "~=" operator? It's not described in http://dlang.org/phobos/std_array.html (though it is mentioned so you need to know what you are looking for before you find it...). What would be the difference between Array!string and string[] ?
May 14 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 14 May 2015 at 20:03:16 UTC, ivoras wrote:
 Where would I look for documentation on the "~=" operator?
http://dlang.org/arrays.html under the heading "array concatenation"
 What would be the difference between Array!string and string[] ?
Array!string takes ownership of its own memory and frees it when no longer used making it appropriate in cases where you want that optmization. string[] is a slice - pointer plus length combination - into externally managed memory.
May 14 2015
prev sibling parent reply "rumbu" <rumbu rumbu.ro> writes:
On Thursday, 14 May 2015 at 20:03:16 UTC, ivoras wrote:
 What would be the difference between Array!string and string[] ?
std.array is used to manipulate or create built-in arrays from various sources (ranges). For basic needs, you can safely use built-in arrays: http://dlang.org/arrays.html Concatenation operator (~) is documented here: http://dlang.org/arrays.html#array-concatenation
May 14 2015
parent reply "ivoras" <ivoras gmail.com> writes:
On Thursday, 14 May 2015 at 20:32:28 UTC, rumbu wrote:
 On Thursday, 14 May 2015 at 20:03:16 UTC, ivoras wrote:
 What would be the difference between Array!string and string[] 
 ?
std.array is used to manipulate or create built-in arrays from various sources (ranges). For basic needs, you can safely use built-in arrays: http://dlang.org/arrays.html Concatenation operator (~) is documented here: http://dlang.org/arrays.html#array-concatenation
Thanks, everyone! I'm experimenting to get a feel for the language. Do you have a suggestion about this example code: https://goo.gl/F7LCAg to make it more "D-like", idiomatic?
May 14 2015
parent "anonymous" <anonymous example.com> writes:
On Thursday, 14 May 2015 at 20:50:05 UTC, ivoras wrote:
 I'm experimenting to get a feel for the language. Do you have a 
 suggestion about this example code: https://goo.gl/F7LCAg to 
 make it more "D-like", idiomatic?
Quoting from the code:
  for (int i = 0; i < count; i++) {
foreach(i; 0 .. count)
    try {
      auto choices = markov[current];
[...]
    } catch (RangeError e) {
      break;
    }
Don't catch Errors. Use the `in` operator to check if `current` is in `markov`: if(current !in markov) break; Or avoiding double lookup: string[]* choicesp = current in markov; if(choicesp is null) break; auto choices = *choicesp;
 int main(string[] args)
You cam omit the return type and `args` if you're not going to use them: void main()
  foreach (c_line; stdin.byLine()) {
    auto line = to!string(c_line);
Could use byLineCopy: foreach(line; stdin.byLineCopy)
      if (prev_token in markov) {
        markov[prev_token] ~= token;
      } else {
        markov[prev_token] = [token];
      }
I think you can just go with appending here: markov[prev_token] ~= token; But I'm not 100% sure about the implications.
May 15 2015
prev sibling parent "anonymous" <anonymous example.com> writes:
On Thursday, 14 May 2015 at 12:42:01 UTC, ivoras wrote:
 https://gist.github.com/ivoras/2d7737c214c3dc937c28

 The crash is at line 20:

 core.exception.AssertError /usr/include/dmd/phobos/std/container/array.d(334):
[...]
 This is on DMD32 D Compiler v2.067.1
Seems to be fixed in git head.
May 14 2015