Re: Creating fixed array on stack
On Friday, 11 January 2019 at 14:46:36 UTC, Andrey wrote: Hi, In C++ you can create a fixed array on stack: int count = getCount(); int myarray[count]; In D the "count" is part of type and must be known at CT but in example it is RT. How to do such thing in D? Without using of heap. You could try alloca: import core.stdc.stdlib: alloca; pragma(inline, true) auto stack(T, alias len)(void* p = alloca(T.sizeof * len)) { return (cast(T*) p)[0 .. len] = T.init; } void main() { import std.stdio: writeln; int size = 42; auto a = stack!(int, size); writeln(a); a[] = 2; writeln(a); }
Re: D'ish similar_text?
On Thursday, 10 May 2018 at 20:38:12 UTC, Vladimir Panteleev wrote: On Thursday, 10 May 2018 at 20:32:11 UTC, Dgame wrote: immutable size_t len = s1.length + s2.length; percent = (len - distance) * 100.0 / len; Note that this formula will give you only 50% similarity for "abc" and "def", i.e. two completely different strings. I suggest to divide by max(s1.length, s2.length) instead. Hm, that does not work either. ABC and AZB have a different outcome with both. How can I calculate the percentage with levenshtein? It's rather simple with similar_text since it returns the amount of similar chars.
Re: D'ish similar_text?
On Thursday, 10 May 2018 at 20:13:49 UTC, Vladimir Panteleev wrote: On Thursday, 10 May 2018 at 20:08:04 UTC, Dgame wrote: void similar_text_similar_str(char* txt1, size_t len1, char* That looks like an implementation of Levenshtein distance. We have one in Phobos: https://dlang.org/library/std/algorithm/comparison/levenshtein_distance.html Oh, that could to work, thank you. I've just tested it quickly, but that code seems to produce the same results: size_t similar_text2(in string s1, in string s2, out double percent) { import std.algorithm: levenshteinDistance; immutable size_t distance = s1.levenshteinDistance(s2); immutable size_t len = s1.length + s2.length; percent = (len - distance) * 100.0 / len; return distance; }
D'ish similar_text?
I'm in need for some sort of string similarity comparision. I've found soundex but that didn't solved my needs. After some search I found a C implementation of similar_text, but that is quite ugly... I was able to let it work in D but it's still somewhat messy. Is there any D implementation of similar_text? Here's my current code which makes heavy use of pointer-arithmetic which makes it hard to understand. void similar_text_similar_str(char* txt1, size_t len1, char* txt2, size_t len2, size_t* pos1, size_t* pos2, size_t* max) { char* p, q; char* end1 = txt1 + len1; char* end2 = txt2 + len2; size_t l; *max = 0; for (p = txt1; p < end1; p++) { for (q = txt2; q < end2; q++) { for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); l++) { } if (l > *max) { *max = l; *pos1 = p - txt1; *pos2 = q - txt2; } } } } size_t similar_text_similar_char(char* txt1, size_t len1, char* txt2, size_t len2) { size_t sum; size_t pos1, pos2, max; similar_text_similar_str(txt1, len1, txt2, len2, , , ); if ((sum = max) != 0) { if (pos1 && pos2) { sum += similar_text_similar_char(txt1, pos1, txt2, pos2); } if ((pos1 + max < len1) && (pos2 + max < len2)) { sum += similar_text_similar_char(txt1 + pos1 + max, len1 - pos1 - max, txt2 + pos2 + max, len2 - pos2 - max); } } return sum; } size_t similar_text(in string s1, in string s2, out double percent) { immutable size_t sim = similar_text_similar_char(s1.dup.ptr, s1.length, s2.dup.ptr, s2.length); percent = sim * 200.0 / (s1.length + s2.length); return sim; }
Re: Static Array with negative index results in a strange error-message
It's really fun playing around: char[int.max - 1] c; results in Internal error: dmd/backend/cgcod.c 634 with DMD 2.079. Guess I or somebody else should report this.
Re: Static Array with negative index results in a strange error-message
On Monday, 23 April 2018 at 13:48:07 UTC, Steven Schveighoffer wrote: On 4/23/18 9:32 AM, Dgame wrote: char[-1] c; results in Error: char[18446744073709551615LU] size 1 * 18446744073709551615 exceeds 0x7fff size limit for static array Should we fix that? A negative index should be IMO detected sooner/with a cleaner error message. Hm.. at least it's detected. I actually don't think the message is wrong: -1 is a valid size_t literal, and results in that number. if you did: enum size_t x = -1; char[x] c; You would get the same result, and I don't know how we would fix that. -Steve C's error message is error: 'c' declared as an array with a negative size char c[-1]; That is more understandable.
Static Array with negative index results in a strange error-message
char[-1] c; results in Error: char[18446744073709551615LU] size 1 * 18446744073709551615 exceeds 0x7fff size limit for static array Should we fix that? A negative index should be IMO detected sooner/with a cleaner error message.
Re: Assoc. Array and struct with immutable member
On Tuesday, 17 April 2018 at 11:38:17 UTC, Jonathan M Davis wrote: On Sunday, April 15, 2018 17:59:01 Dgame via Digitalmars-d-learn wrote: How am I supposed to insert a struct with immutable members into an assoc. array? Reduced example: struct A { immutable string name; } A[string] as; as["a"] = A("a"); // Does not work I would point out that in general, having const or immutable fields in a struct is a terrible idea. It causes all kinds of problems because stuff like assignment doesn't work. If you want the field to be read-only, you'll have far fewer problems if you simply use a function to access it instead of making the member public. e.g. struct A { public: @property string name() { return _name; } private: string _name; } The problem you're having here is just one example of the list of things that don't work if you have a struct with immutable members. It looks like the AA probably is doing something like initializing the entry with A.init and then assigning it the new value, and that's not going to work if the struct has immutable members - which is exactly what the error message says. - Jonathan M Davis That's how I solved it. But it is troublesome and annoying because it increases the amount of manually work I have to do.
Re: Ldc on Windows
On Tuesday, 17 April 2018 at 11:01:21 UTC, Nicholas Wilson wrote: On Tuesday, 17 April 2018 at 10:17:56 UTC, Dgame wrote: Ah, I found the msvcEnv.bat and I told me that I have to VSC installation. Solved! You should also be able to use -link-internally /LLMV's lld if you don't want to install MSVC What would be "/LLMV's lld" in this case?
Re: Ldc on Windows
On Tuesday, 17 April 2018 at 09:42:01 UTC, Dgame wrote: I'm trying to use Ldc on Windows, but I get these linker errors: OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html OPTLINK : Warning 9: Unknown Option : OUT OPTLINK : Warning 9: Unknown Option : LIBPATH OPTLINK : Warning 9: Unknown Option : D OPTLINK : Warning 9: Unknown Option : LDC OPTLINK : Warning 9: Unknown Option : .. OPTLINK : Warning 9: Unknown Option : LIB :REF.obj Error 2: File Not Found :REF.obj Error: D:\D\dmd2\windows\bin\link.exe failed with status: 1 How can I solve this? Ah, I found the msvcEnv.bat and I told me that I have to VSC installation. Solved!
Ldc on Windows
I'm trying to use Ldc on Windows, but I get these linker errors: OPTLINK (R) for Win32 Release 8.00.17 Copyright (C) Digital Mars 1989-2013 All rights reserved. http://www.digitalmars.com/ctg/optlink.html OPTLINK : Warning 9: Unknown Option : OUT OPTLINK : Warning 9: Unknown Option : LIBPATH OPTLINK : Warning 9: Unknown Option : D OPTLINK : Warning 9: Unknown Option : LDC OPTLINK : Warning 9: Unknown Option : .. OPTLINK : Warning 9: Unknown Option : LIB :REF.obj Error 2: File Not Found :REF.obj Error: D:\D\dmd2\windows\bin\link.exe failed with status: 1 How can I solve this?
Assoc. Array and struct with immutable member
How am I supposed to insert a struct with immutable members into an assoc. array? Reduced example: struct A { immutable string name; } A[string] as; as["a"] = A("a"); // Does not work
Re: Rvalue references
On Wednesday, 10 January 2018 at 14:41:21 UTC, Steven Schveighoffer wrote: On 1/10/18 3:08 AM, Dgame wrote: On Wednesday, 10 January 2018 at 01:56:02 UTC, Steven Schveighoffer wrote: But current auto ref is what we have, so I would recommend using it. I would recommend to ignore auto ref for rvalue references. It generates 2^N functions where N is the amount of auto ref parameters. That the most awful template bloat I've ever seen. It only generates 2^N functions if you call it 2^N different ways. Most of the time you call it the same way. -Steve If that would be true we wouldn't need auto ref at all.
Re: Rvalue references
On Wednesday, 10 January 2018 at 01:56:02 UTC, Steven Schveighoffer wrote: But current auto ref is what we have, so I would recommend using it. I would recommend to ignore auto ref for rvalue references. It generates 2^N functions where N is the amount of auto ref parameters. That the most awful template bloat I've ever seen.
Re: How do I pass a type as parameter in this method?
On Tuesday, 19 December 2017 at 15:19:53 UTC, Marc wrote: On Tuesday, 19 December 2017 at 00:01:00 UTC, Ali Çehreli wrote: On 12/18/2017 03:54 PM, Ali Çehreli wrote: On 12/18/2017 02:58 PM, Marc wrote: Here's another experiment: template FirstOf(T...) { template otherwise(D) { static if (T.length == 0) { enum otherwise = D.init; } else { enum otherwise = T[0]; } } } void main() { static assert (FirstOf!(1.5, "hello").otherwise!int == 1.5); static assert (FirstOf!("world", [1]).otherwise!int == "world"); static assert (FirstOf!().otherwise!int == 0); } Ali Thanks four answer. I'll be using this one. I was messing around and getting multiple arguments to template function too. I like the naming too, it made code clear, imo. It's possible to have overload where one take a type name and the other a variable (constant value actually)? something like this (also imaginary code): template FirstOf(TP...) { template otherwise(D) { static if(TP.length > 0) { enum otherwise = TP[0]; } else { enum otherwise = D.init; } } template otherwise(D value) { static if(TP.length > 0) { enum otherwise = TP[0]; } else { enum otherwise = value; } } } So I can use like this: int index = FirstOf!(myTuple).otherwise!int; int value = FirstOf(myTuple2).otherwise!(10); with my limited template knowledge, I know I can write it like this: template otherwise(D, D value) { static if(TP.length > 0) { enum otherwise = TP[0]; } else { enum otherwise = value; } } So the call would go like this: int value = FirstOf(myTuple2).otherwise!(int, 10); But for simplicity I'd like to infer the type from constant value passed in parameter, in that case, the integer value of 10. template FirstOf(T...) { template otherwise(D) { static if (T.length == 0) { enum otherwise = D.init; } else { enum otherwise = T[0]; } } template otherwise(alias value) { static if (T.length == 0) { enum otherwise = value; } else { enum otherwise = T[0]; } } } void main() { static assert (FirstOf!(1.5, "hello").otherwise!int == 1.5); static assert (FirstOf!("world", [1]).otherwise!int == "world"); static assert (FirstOf!().otherwise!int == 0); static assert (FirstOf!(1.5, "hello").otherwise!23 == 1.5); static assert (FirstOf!().otherwise!42 == 42); }
Re: Sort characters in string
On Wednesday, 6 December 2017 at 09:25:20 UTC, Biotronic wrote: On Wednesday, 6 December 2017 at 08:59:09 UTC, Fredrik Boulund wrote: string word = "longword"; writeln(sort(word)); But that doesn't work because I guess a string is not the type of range required for sort? Yeah, narrow (non-UTF-32) strings are not random-access, since characters like take up more than one code unit, and so ""[0] returns an invalid piece of a character instead of a full character. In addition, sort does in-place sorting, so the input range is changed. Since D strings are immutable(char)[], changing the elements is disallowed. So in total, you'll need to convert from a string (immutable(char)[]) to a dchar[]. std.conv.to to the rescue: import std.stdio : writeln; import std.conv : to; import std.algorithm.sorting : sort; string word = "longword"; writeln(sort(word.to!(dchar[]))); // dglnoorw -- Biotronic Or you simply do writeln("longword".array.sort);
Re: Temporary objects as function parameters or when-is-this-shit-going-to-end?
On Friday, 13 October 2017 at 12:08:00 UTC, Jack Applegame wrote: On Friday, 13 October 2017 at 12:03:55 UTC, Dgame wrote: Interesting. If you remove the CTor in Foo it works again. If you remove DTor it works again too. :) That's one of these times where it would be helpful to see the generated AST.
Re: Temporary objects as function parameters or when-is-this-shit-going-to-end?
On Friday, 13 October 2017 at 10:35:56 UTC, Jack Applegame wrote: If you don't want to get the great PITA, never create temporary objects in function parameters. I recently spent a whole day digging through my reference counted containers library. But nasty bug was not there, but in the compiler. Look at this: https://glot.io/snippets/eui2l8ov0r Result: Bar.this(int): 7FFD3D60CD38 fun: 7FFD3D60CD20 Bar.~this(): 7FFD3D60CD20 Compiler creates struct on the stack and silently (without postblitting and destruction old object) moves it to another address. Is it normal? I don't think so. But that's not the most fun. Look at this: https://glot.io/snippets/eui2pjrwvi Result: Bar.this(int): 7FFF87DD2D31 fun: 7FFF87DD2CE0 Bar.~this(): 7FFF87DD2CE0 Bar.~this(): 7FFF87DD2D31 WAT??? Compiler creates struct on the stack copies it without postblitting and destructs both objects. But if you create the structure before calling the function, then all will be well: https://glot.io/snippets/eui2vn2bu1 All this greatly angered me because there are several issues related wrong struct construction and destruction in the bugtracker. Because of these bugs my low level libraries full of strange hacks. I want to ask. How you guys are going to create a reliable RC library, if such fundamental bugs hang in the issue tracker for months and years. And instead of fixing them, you develop new minor bells and whistles. See: https://issues.dlang.org/buglist.cgi?quicksearch=destructor Since I myself can't fix such bugs (my knowledge in this area are extremely small), I have a question to Andrei Alexandrescu: Can I donate to the D Foundation and that my donations would be aimed at fixing exactly these bugs? Interesting. If you remove the CTor in Foo it works again.
Re: No polymorphism?
On Monday, 24 July 2017 at 18:15:20 UTC, Steven Schveighoffer wrote: On 7/24/17 1:29 PM, Dgame wrote: Why isn't the compiler able to deduce S[] => I[]? Or is it just me? I've tried dmd 2.075 I know you got the explanation already, but just in case you actually need to call something like test1 but only have an S[]: test1(ss.map!((I i) => i).array) -Steve Thanks.
Re: No polymorphism?
On Monday, 24 July 2017 at 17:33:48 UTC, Adam D. Ruppe wrote: On Monday, 24 July 2017 at 17:29:55 UTC, Dgame wrote: S[] ss = [new S()]; test1(ss); // Fails Why isn't the compiler able to deduce S[] => I[]? Or is it just me? This is exactly because of polymorphism. Consider the following: ``` S[] ss = [new S()]; I[] i = ss; // pass it to the function or whatever for implicit conversion class OtherDerived : I {} i[0] = new OtherDerived(); // looks OK, otherDerived is also interface I ``` But now, ss[0], the same array as i, no longer points to an S! You broke the type system. So, tired it is. Thanks a lot.
No polymorphism?
I may be just tired, but could somebody explain this behaviour to me? It seems odd to me: interface I { } class S : I { } void test1(I[]) { } void test2(I) { } void main() { test1([new S()]); // Works test2(new S()); // Works I i = new S(); test2(i); // Works S s = new S(); test2(s); // Works I[] si = [new S()]; test1(si); // Works S[] ss = [new S()]; test1(ss); // Fails } Compiler output: test.d(32): Error: function test1 (I[] _param_0) is not callable using argument types (S[]) Why isn't the compiler able to deduce S[] => I[]? Or is it just me? I've tried dmd 2.075
Re: Static array with parameter based size?
On Tuesday, 11 July 2017 at 08:23:02 UTC, Miguel L wrote: I need to create a non-dynamic array like this void f(int x) { int[x] my_array; ... this does not compile as x value needs to be known at compile time. The closest to this I can get is: void f(int x) { int[] my_array; my_array.length=x; but I don't really need a dynamic array as length is not going to change inside f. What is the best way to do this? Thanks in advance Another approach would be to use a template function: void f(int x)() { int[x] array; } void main() { f!42; }