d

D dynamic array initialization, stride and the index operation

半城伤御伤魂 提交于 2019-12-23 12:10:19
问题 Sorry, this became a 3-fold question regarding arrays I think (dynamic) arrays are truly powerful in D, but the following has been bothering me for a while: In C++ I could easily allocate an array with designated values, but in D I haven't found a way to do so. Surely the following is no problem: int[] a = new int[N]; a[] = a0; But it looks inefficient, since line one will initialize with 0 , and like 2 with a0 . Could something similar to the following be done in D? int[] a = new int(a0)[N];

Passing variadic arguments in one function to another function in D

醉酒当歌 提交于 2019-12-23 10:58:28
问题 I have a variadic D-style function foo(format, ...) , which is a wrapper around writefln . I'd like to do something like this: foo(format, <...>) { //... writefln(format, ...); } Essentially, passing on the ellipsis parameter(s) to writefln. I understand that this isn't easy/possible in C/C++, but is there a way to accomplish this in D? 回答1: This will do it for you: import std.stdio; void customWrite(Args...)(string format, Args args) { writefln(format, args); } 回答2: I had forgotten that

Tango future versus D1 discontinuation

别等时光非礼了梦想. 提交于 2019-12-23 09:46:58
问题 Knowing that D1 will be discontinued effective December 31, 2012 , is it still worth it to continue using Tango given that the official distribution is under D1? 回答1: If you're talking about whether it's worth continuing to use Tango with D1 once official support for D1 goes away, then I think that that's pretty much the same answer as whether it's worth continuing to use D1. The lack of D1 support for dmd really shouldn't have any effect on whether Tango for D1 is still usable. The real

Fast linear system solver for D? [closed]

流过昼夜 提交于 2019-12-23 09:18:23
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Where can I get a fast linear system solver written in D? It should be able to take a square matrix A and a vector b and solve the equation Ax = b for b and, ideally, also perform explicit inversion on A . I have one I wrote myself, but it's pretty slow, probably because it's completely cache-naive. However, for

is a static array a forward range?

偶尔善良 提交于 2019-12-23 07:49:05
问题 This works: int[] a = [ 1, 2, 3, 4 ]; fill(a, 5); but this doesn't: int[4] a = [ 1, 2, 3, 4 ]; fill(a, 5); and I get this error: Error: template std.algorithm.fill(Range,Value) if (isForwardRange!(Range) && is(typeof(range.front = filler))) does not match any function template declaration instead, I have to do this in order for it to work with static arrays: int[4] a = [ 1, 2, 3, 4 ]; fill(a[], 5); could any one explain this behavior please? 回答1: No. isForwardRange is false for static arrays,

D template specialization in different source file

懵懂的女人 提交于 2019-12-23 07:36:46
问题 I recently asked this question about how to simulate type classes in D and suggested a way to do this using template specialization. I discovered that D doesn´t recognize template specialization in a different source file. So I couldn´t just make a specialization in a file not included from the file where the generic function is defined. To illustrate, consider this example: //template.d import std.stdio; template Generic(A) { void sayHello() { writefln("Generic"); } } void testTemplate(A)()

Showing D Coverage Results as Overlays in Source Buffer

风格不统一 提交于 2019-12-23 03:32:22
问题 The D language compiler DMD outputs its coverage analysis in a file containing the original source as | inout(Ix)[] prefix() inout | { 2037| assert(!keys.empty); 2037| final switch (keys.length) | { 000000000| case 1: 000000000| return keys.at!0[]; 2037| case 2: | import std.algorithm.searching : commonPrefix; 2037| return commonPrefix(keys.at!0[], keys.at!1[]); | } | } that is, the original source where each line has been prefixed by a 10-character column containing the execution count (if

Disposing a class with an interface?

随声附和 提交于 2019-12-23 02:45:29
问题 interface Bar{ } class Foo: Bar{ int i; this(int _i){ i = _i; } } void main(){ import std.experimental.allocator.mallocator; import std.experimental.allocator; auto f = Mallocator.instance.make!Foo(42); Bar b = f; Mallocator.instance.dispose(b);// Bad } I am currently creating my own smart pointer and I am now extending them to also work with classes and polymorphism. The problem I am running into is subtyping, I am transferring a pointer from Box!Foo to Box!Bar if Foo implements Bar. But

STL __merge_without_buffer algorithm?

≯℡__Kan透↙ 提交于 2019-12-22 10:39:13
问题 Where can I get a decent high-level description of the algorithm used in __merge_without_buffer() in the C++ STL? I'm trying to reimplement this code in the D programming language, with some enhancements. I can't seem to grok what it's doing at the algorithmic level from just reading the STL source code because there are too many low-level details obscuring it. Also, I don't want to just blindly translate the code because then, if it didn't work I wouldn't know why, and I wouldn't be able to

Using std.algorithm.map with member functions in D2

心不动则不痛 提交于 2019-12-22 10:11:15
问题 I have: Foo foo = new Foo(); foreach (i; 0..10) { Bar bar = foo.getBar(i); ... } I want to be able to instead say (equivalently): foreach (bar; foo.getAllBars()) { ... } How do I go about implementing getAllBars() ? I figured something like this: class Foo { auto getAllBars() { return map!(getBar)(iota(10)); } } But you can't do that of course because getBar depends on the this parameter, which will go out of scope. The same applies if you try to create a local function or delegate . I also