Re: Create D portable binary
On Friday, 8 December 2017 at 06:37:36 UTC, Adam D. Ruppe wrote: On Friday, 8 December 2017 at 05:16:22 UTC, Fra Mecca wrote: Is there a way to compile a project and deploying it as a single statically linked binary? A default build of a D program is *reasonably* compatible. All its dependencies are core operating system components like libc. Now, there can certainly be libc version incompatibilities, but there's a decent chance it will just work. I'm pretty sure this is the exact same situation Go is in; the default Go and D builds link the same way. If you want to eliminate the potential C lib incompatibility too, you can do it basically the same way as in C, passing options to gcc with dmd's -L thing like `-L-static -L-nodefaultlib -L-lsome_alternate_clib` On the same note, has https://wiki.dlang.org/DIP59 been dropped?
Re: Binary serialization of a struct
On Saturday, 16 September 2017 at 13:15:54 UTC, Azi Hassan wrote: On Saturday, 16 September 2017 at 03:30:51 UTC, Joseph wrote: Are there any simple direct serialization libraries where I can mark elements of a class or struct that I want serialized with an attribute and it will take care of all the rest(including recursive structures, arrays, etc) then deserialize back in to the structs? I want something straight forward without allot of plumbing on my end. Have you checked Cerealed ? From the looks of it, it supports a @NoCereal attribute which does the opposite of what you're looking for. Not sure how it handles nested structs, but there are examples in the test/directory : https://github.com/atilaneves/cerealed/ Cerealed is definitely my favourite library out there for binary serialization. High quality.
Re: Create D portable binary
On Friday, 8 December 2017 at 06:37:36 UTC, Adam D. Ruppe wrote: On Friday, 8 December 2017 at 05:16:22 UTC, Fra Mecca wrote: Is there a way to compile a project and deploying it as a single statically linked binary? A default build of a D program is *reasonably* compatible. All its dependencies are core operating system components like libc. Now, there can certainly be libc version incompatibilities, but there's a decent chance it will just work. I'm pretty sure this is the exact same situation Go is in; the default Go and D builds link the same way. I was curious so I just tried. Building a hello world with "go build" (I just litterally did what was on https://gobyexample.com/hello-world) produces a file that is not a dynamic executable. There is no dependency. For comparison building a similar hello world with dmd and no options gives the followig dependencies (thanks ldd): linux-vdso.so.1 (0x7fffd02aa000) libpthread.so.0 => /usr/lib/libpthread.so.0 (0x7f45e5e69000) libm.so.6 => /usr/lib/libm.so.6 (0x7f45e5b1d000) librt.so.1 => /usr/lib/librt.so.1 (0x7f45e5915000) libdl.so.2 => /usr/lib/libdl.so.2 (0x7f45e5711000) libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7f45e54fa000) libc.so.6 => /usr/lib/libc.so.6 (0x7f45e5142000) /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x7f45e6087000) (And using -L-static produces an error because of an incompatibility with -pie... I'm sure it's workable I just didn't take the time to look it up yet)
Re: Parallel reads on std.container.array.Array
On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran wrote: 2. I'm on an 8 CPU box and I don't seem to hit 800% CPU with D version (max 720%). However I can get 800% CPU usage with the C++ version. Please ignore, this is because of the write.
Re: Create D portable binary
On 2017-12-08 06:16, Fra Mecca wrote: Is there a way to compile a project and deploying it as a single statically linked binary? My main target would be something like a self contained jar (like .war files), but something that is in the style of go binaries and portable to another Linux distribution without any hassle would be enough. From what I understand the main problem in achieving this is the dependency to curl (all the warnings related to gethostbyname during linking phase) and glibc that makes it hard to static link. What if musl is used as libc? Currently the musl library cannot be used because the runtime relies on some GNU extension to standard libc. Right now it seems that the only viable option is to distribute object files and make the end user link them You can statically link a D binary using the -static flag and LDC, if you don't rely on any of the functions that the linker warns about, like gethostbyname. DMD cannot be used because of its TLS implementation. For example, this project I've created is 100% statically linked on Linux. You can have a look in the Dub configuration [2]. If you need some resource files you can have a look at the Import Expression [3] as well. [1] https://github.com/jacob-carlborg/remarkify [2] https://github.com/jacob-carlborg/remarkify/blob/master/dub.sdl#L7 [3] https://dlang.org/spec/expression.html#import_expressions -- /Jacob Carlborg
Check whether a file is empty.
Hi All, Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes. From, Vino.B
Re: Parallel reads on std.container.array.Array
On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran wrote: I was wondering if std.container.array.Array supports threadsafe parallel reads similar to std::vector. I've created a small program for demonstration https://github.com/carun/parallel-read-tester It works fine with just couple of problems though: 1. D version takes way too long compared to C++ version. My mistake (IO bottleneck, std.stdio.write is probably flushing?)! The timings are now close enough, in the order of milliseconds. This is not just with one run, but multiple runs. (I should probably test this on a Xeon server). === Starting CPP version === Took 3.79253 to load 200 items. Gonna search in parallel... 4 4 1 4 3 4 2 4 6 4 7 4 5 4 0 4 Took 6.28018 to search === Starting D version === Took 1 sec, 474 ms, 869 μs, and 4 hnsecs to load 200 items. Gonna search in parallel... 0 4 1 4 2 4 7 4 6 4 4 4 3 4 5 4 Took 6 secs, 472 ms, 467 μs, and 8 hnsecs to search. The one that puzzles me is, what's wrong with the CPP version? :) Why is it slow loading the gallery (more than twice as slow as the D counterpart)? I thought std::vector::emplace_back should do a decent job. RVO in D? 2. Introducing a string in the struct Data results in "std.container.Array.reserve failed to allocate memory", whereas adding a similar std::string in the C++ struct seems to work fine. Couldn't find the reason! Am I missing anything obvious here? Also why doesn't std.container.array support an equivalent of std::vector::erase?
Re: Parallel reads on std.container.array.Array
On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran wrote: I was wondering if std.container.array.Array supports threadsafe parallel reads similar to std::vector. No, your code can also fail on a system with inconsistent cache because data written by writing thread can remain in its cache and not reach shared memory in time or reading threads can read from their stale cache.
Re: Binary serialization of a struct
You should use size_t instead of ulong, but on 32bit you would have still problem because you are trying assign 2^32 which is too big to hold in 32bit On Thu, Dec 7, 2017 at 11:42 PM, kdevel via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > On Tuesday, 19 September 2017 at 06:32:52 UTC, Nordlöw wrote: > >> I want something straight forward without allot of plumbing on my end. >>> >> >> https://github.com/msgpack/msgpack-d >> > > I can't unittest my 32-bit code: > > $ MODEL=32 make -f posix.mak unittest > [...] > src/msgpack/packer.d(1139): Error: function core.stdc.stdlib.malloc (uint > size) is not callable using argument types (ulong) > > >
Re: Parallel reads on std.container.array.Array
On Friday, 8 December 2017 at 10:01:14 UTC, Kagamin wrote: On Friday, 8 December 2017 at 07:34:53 UTC, Arun Chandrasekaran wrote: I was wondering if std.container.array.Array supports threadsafe parallel reads similar to std::vector. No, your code can also fail on a system with inconsistent cache because data written by writing thread can remain in its cache and not reach shared memory in time or reading threads can read from their stale cache. I'm OK with some delay between the writes and the reads. The same applies to the writes and reads across processes. At least between threads the impact/delay is minimum whereas between processes it's even worse as the page will have to be reflected in all the mapped processes.
Re: Check whether a file is empty.
Other functions that can be used for this are GetFileInformationByHandle, GetFileSizeEx, SetFilePointerEx or File.size in phobos.
Re: Check whether a file is empty.
On Friday, 8 December 2017 at 09:40:18 UTC, Vino wrote: Hi All, Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes. From, Vino.B Was it the same file on Windows 7 and Windows 2003? Maybe the file on Windows 2003 had a byte order mark or carriage return + newline characters.
Re: std.range.interfaces : InputRange moveFront
On 12/03/2017 12:42 AM, Johan Engelen wrote: On Friday, 1 December 2017 at 18:33:09 UTC, Ali Çehreli wrote: On 12/01/2017 07:21 AM, Steven Schveighoffer wrote: > On 12/1/17 4:29 AM, Johan Engelen wrote: >> (Also, I would expect "popFront" to return the element popped, but it >> doesn't, OK... > > pop removes the front element, but if getting the front element is > expensive (say if it's a map with a complex lambda function), you don't > want to execute that just so you can return it to someone who doesn't > care. This is why front and popFront are separate. Yet, we're told that compilers are pretty good at eliminating that unused copy especially for function templates where all code is visible. I assume that Steven means "copying the front element" when he wrote "getting the front element"? There is no need for a copy, because the element will be removed from the range, so we can move (whose cost only depends on the size of the element, internal pointers being disallowed by the language). If it is expensive to actually get _to_ the front/back element (i.e. find its memory location), then having to do the operation twice is a disadvantage. Ali: the compiler can only elide copying/moving of an unused return value when inlining the function. (the duty of making the return value move/copy is on the callee, not the caller) Note that because front/back() and popFront/Back() are separate, a copy *is* needed when one wants to "pop an element off". Thus moveFront/Back() and popFront/Back() should be used. OK. The fact that "pop" does something different from other programming languages is something important to remember when teaching people about D. And I think should be made clear in the documentation; let's add an example of how one is supposed to use all this in an efficient manner? Back on topic: let's change the documentation of moveFront such that it is clear that it does _not_ reduce the number of elements in the range? So, even though exception safety is not a common topic of D community, the real reason for why popFront() does not return the element is for strong exception safety guarantee. Interesting point. Argh why do we allow the user to throw in move? Regardless, separating front() from popFront() is preferable due to cohesion: fewer responsibilities per function, especially such low level ones. This doesn't make much sense ;-) popFrontN has more responsibility, and one gains better performance than simply calling popFront N times. It's similar here. Thanks Ali for asking me to comment in this thread. The matter of fact is moveFront was needed for different purposes. First off, moving in D cannot throw; all objects are moveable by means of bitwise move. The main reason for moveFront's existence is supporting ranges that have front() return an rvalue. For those, there would otherwise be no efficient means to move data out of the range to its user. Now, why does popFront return void instead of the popped element? We need front() anyway as a non-destructive way to look at the current element of the range, so having popFront return that element is redundant. Plus, it's difficult to optimize away particularly in separately compiled code. Andrei
Re: Check whether a file is empty.
On Friday, 8 December 2017 at 12:25:19 UTC, FreeSlave wrote: On Friday, 8 December 2017 at 09:40:18 UTC, Vino wrote: Hi All, Request your help on how to check whether a given file is empty, I tried the getSize from std.file but no luck as in windows 7 is the file is empty the size of the file is 0 bytes but in Windows 2003 if the file is empty the size of the file show as 2 bytes. From, Vino.B Was it the same file on Windows 7 and Windows 2003? Maybe the file on Windows 2003 had a byte order mark or carriage return + newline characters. Hi, The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty. From, Vino.B
Re: Directory Size
On Thursday, 7 December 2017 at 12:19:00 UTC, Vino wrote: On Thursday, 7 December 2017 at 09:04:19 UTC, Vino wrote: [...] Hi Andrea, Was able to find a solution to the above issue by adding the replace function as below, the the code is working as expected, is there any chance of using parallel function as the file system contains about 500+ folders and the size of each folder ranges from 5GB - 500 GB so the run time of the above code is about an 2 hours. .map!(a => tuple(a[0].replace(",\\?\", ""), ((a[1].to!real)/ SGb ; From, Vino.B Hi All, Any update on the above request. From, Vino.B
Re: GUI program on Mac OS in D?
On Thursday, 7 December 2017 at 09:39:45 UTC, Jacob Carlborg wrote: The latest DMD compiler only supports what's in the official documentation, i.e. [1]. What's documented in DIP43 [2] (except anything marked with "unimplemented") is what's been implemented in one of my forks. I'm working on adding what's in my fork piece by piece to upstream. Ok thanks for clearing that up! Your work looks really appealing. Hope it gets into the official compiler soon.
Re: What is "stringImportPaths"
On Thursday, 7 December 2017 at 09:47:31 UTC, Jacob Carlborg wrote: On 2017-12-06 20:05, mrphobby wrote: There are two kinds of language constructs that uses the "import" keyword. One is the "Import Declaration" [1] which is the most common one and is used to import other symbols. The other language construct is the "Import Expression" [2], which is used to read a file at compile time and put its content into a string literal in your source code. Anything specified to the "stringImportPaths" build setting will be sent to the compiler with the -J flag. [1] https://dlang.org/spec/module.html#ImportDeclaration [2] https://dlang.org/spec/expression.html#import_expressions Ok thanks! I couldn't find that in the docs so kudos for pointing me to it. I still think using the word "import" is confusing. Would rather have it called "load" or something. But at least I understand it now :)
Re: GUI program on Mac OS in D?
On Thursday, 7 December 2017 at 12:27:36 UTC, Guillaume Piolat wrote: On Thursday, 7 December 2017 at 12:18:21 UTC, Guillaume Piolat wrote: You can easily make a DUB frontend to do that, for example https://github.com/AuburnSounds/Dplug/tree/master/tools/dplug-build And it might be cleaner to do this as a post-build step. Thanks, I'll have a look at that.
Re: What is "stringImportPaths"
On 12/08/2017 12:10 PM, mrphobby wrote: > I still think using the word "import" is confusing. Would rather have it > called "load" or something. But at least I understand it now :) We don't want any more keywords. :) (D's keywords are context-independent.) An unfortunate example was the "body", which is on its way out of being a keyword. Instead, the "do" keyword is overloaded to take "body"s responsibility: int foo(int i) in { assert(i < 100); } out(result) { assert(result > i); } do { auto body = i + 7;// YAY! :) return body; } void main() { foo(42); } Of course the same can be said about any other keyword but life is not fair. :) Ali
Problem with digest Hmac sha256 method and another problem while using openssl hmac
Hi, I need to have the same result while using : openssl dgst -sha256 -hmac "somestring" But the server rejecting my generated hmac with the code below . auto hmac = HMAC!SHA256("somestring".representation); hmac.put(url.representation); auto generatedHmac = hmac.finish(); string generatedHmacStr = std.digest.digest.toHexString(generatedHmac); Saying generatedHmac is not correct. Than I wanted to give a try with deimos-openssl like: ubyte[32] hmac_sha256( string key, string data) { HMAC_CTX *ctx = new HMAC_CTX; HMAC_CTX_init(ctx); auto digest = HMAC(EVP_sha256(), cast(void *) key, cast(int) key.length, cast(ubyte*) data, cast(int) data.length, null,null); ubyte[32] array = digest[0..32]; return array; } But I am getting a linking error : undefined reference to `_D6deimos7openssl3evp13env_md_ctx_st6__initZ' My dependencies in dub.json file is like: "dependencies": { "openssl": "~>1.1.6+1.0.1g", "vibe-d": "~>0.8.1", } I really want to solve linking error first. I tried installing openssl it didn't help. Any suggestions? Regards Erdem
Re: Problem with digest Hmac sha256 method and another problem while using openssl hmac
On 12/08/2017 01:05 PM, kerdemdemir wrote: > Hi, > > I need to have the same result while using : > > openssl Works for me as adapted from Phobos documentation: import std.stdio; import std.digest.hmac, std.digest.sha; import std.string : representation; void main() { auto hmac = HMAC!SHA256("key".representation); hmac.put("value".representation); foreach (m; hmac.finish()) { writef("%02x", m); } writeln(); } Assuming the program is called 'deneme', the result is the same: $ echo -n "value" | openssl dgst -sha256 -hmac "key" && ./deneme (stdin)= 90fbfcf15e74a36b89dbdb2a721d9aecffdfdddc5c83e27f7592594f71932481 90fbfcf15e74a36b89dbdb2a721d9aecffdfdddc5c83e27f7592594f71932481 Ali
Re: Parallel reads on std.container.array.Array
So I tried the same on Haswell processor with LDC 1.6.0 and it crashes ``` === Starting D version === Took 1 sec, 107 ms, and 383 μs to load 100 items. Gonna search in parallel... *** Error in `./dmain-ldc': double free or corruption (fasttop): 0x00edc6e0 *** *** Error in `./dmain-ldc': double free or corruption (fasttop): 0x00edc6e0 *** ``` DMD on the other hand takes forever to run and doesn't complete.
Sort in return statement
Anyone got ideas on how to get sort() working in the *return* statement? // ushort[] draw8Numbers() { import std.meta : aliasSeqOf; import std.range : iota; ushort[] numbers = [ aliasSeqOf!(iota(1,46)) ]; import std.random : randomShuffle; randomShuffle(numbers); import std.range : take; import std.algorithm.sorting : sort; return numbers.take(8); /* ok */ //return sort(numbers.take(8)); /* I want this, but it won't work. */ } // -
Re: Sort in return statement
On Sat, 09 Dec 2017 02:34:29 +, codephantom wrote: > Anyone got ideas on how to get sort() working in the *return* > statement? > > // > > ushort[] draw8Numbers() > { > import std.meta : aliasSeqOf; > import std.range : iota; > ushort[] numbers = [ aliasSeqOf!(iota(1,46)) ]; > > import std.random : randomShuffle; > randomShuffle(numbers); > > import std.range : take; > import std.algorithm.sorting : sort; > return numbers.take(8); /* ok */ > //return sort(numbers.take(8)); /* I want this, but it won't > work. */ > > } > > // - `sort` returns a SortedRange of ushorts, not an array of ushorts. Make it: ``` import std.array : array; return sort(numbers.take(8)).array; ``` --Ryan
Re: Check whether a file is empty.
On Friday, 8 December 2017 at 19:13:20 UTC, vino wrote: Hi, The code is same just copy pasted the code form Windows 7 into Windows 2003 and executed, in Windows 7 the log file is of size 0 where as in windows 2003 the log file is of size 2 byte where the log file in both the server is empty. From, Vino.B It certainly sounds like some kind of encoding issue. I guess you don't have much choice other than to read the contents of the file, and determine what it actually contains. Assuming these log files of yours are 'ascii text' files, then if the file is less than say .. 5 bytes.. you could check if it *only* contained *non-printable* characters, in which case it's 'likely' an empty file. something very silly, like this, might do it ;-) // --- bool isFileLikelyEmpty(string filename) { import std.exception, std.file, std.ascii, std.conv; File f = filename; enforce(f.size < 5, "This file is not likely to be empty." ~ " No point in continuing."); // 5 seems reasonable cutoff ;-) bool result = false; int charCount = 0; auto str = readText(filename); foreach(c; str) { // https://dlang.org/phobos/std_ascii.html#isPrintable if( !isPrintable(c) ) charCount++; } if(charCount == str.length) result = true; // file is likely empty, as all characters are non-printable. return result; } // -
Re: Sort in return statement
On Saturday, 9 December 2017 at 02:45:35 UTC, rjframe wrote: `sort` returns a SortedRange of ushorts, not an array of ushorts. Make it: ``` import std.array : array; return sort(numbers.take(8)).array; ``` --Ryan That's it! Thanks Ryan.
Re: Sort in return statement
On Saturday, 9 December 2017 at 03:24:52 UTC, codephantom wrote: On Saturday, 9 December 2017 at 02:45:35 UTC, rjframe wrote: `sort` returns a SortedRange of ushorts, not an array of ushorts. Make it: ``` import std.array : array; return sort(numbers.take(8)).array; ``` --Ryan That's it! Thanks Ryan. Yes, this works, and your algorithm would even accept arbitary random-access ranges, not merely arrays. But since we start explicitly with a ushort[] that this function has allocated just for this algorithm, we could save the extra allocation by the final call to array(). // ushort[] numbers = ... randomShuffle(numbers); import std.algorithm.sorting : sort; numbers = numbers[0 .. 8]; sort(numbers); return numbers; sort(numbers) does two things: (1) affect the underlying data, (2) return an input range with extra information that this returned range is sorted. But in our example, we don't need to allocate a fresh array from (2). We can return the sorted data from (1), this is already in array-form. -- Simon
std.conv.to!string refuses to convert a char* to string.
I am trying out the DJni library (https://github.com/Monnoroch/DJni). For some reason std.conv.to!string doesn't want to convert a char* to a string.The lines below are taken from the log. I see that the last frame is at gc_qalloc. I am not sure why it failed there. Can anybody elaborate on what is going on here ? Thanks in advance. 91 92 93 Stack: [0x7f749dfd9000,0x7f749e0da000], sp=0x7f749e0d8600, free space=1021k 94 Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code) 95 C [libPrompt.so+0x6b0cc] gc_qalloc+0x2c 96 C [libPrompt.so+0x7bf18] _D2rt8lifetime12__arrayAllocFNaNbmxC8TypeInfoxQlZS4core6memory8BlkInfo_+0xec 97 C [libPrompt.so+0x6c2fa] _d_newarrayU+0x86 98 C [libPrompt.so+0x593a7] _D6object__T4_dupTxaTaZQlFNaNbAxaZAa+0x1f 99 C [libPrompt.so+0x59384] _D6object__T11_trustedDupTxaTaZQtFNaNbNeAxaZAa+0x20 100 C [libPrompt.so+0x59360] _D6object__T3dupTaZQhFNaNbNdNfAxaZAa+0x20 101 C [libPrompt.so+0x5932a] _D3std4conv__T6toImplTAyaTPxaZQqFQhZ9__lambda2MFNaNbZQBf+0x3e 102 C [libPrompt.so+0x592ea] _D3std4conv__T6toImplTAyaTPxaZQqFNaNbQlZQs+0x16 103 C [libPrompt.so+0x592d1] _D3std4conv__T2toTAyaZ__TQlTPxaZQsFNaNbQlZQy+0x9 104 C [libPrompt.so+0x59289] Java_Prompt_getLine+0x61 105 j Prompt.getLine(Ljava/lang/String;)Ljava/lang/String;+0 106 j Prompt.main([Ljava/lang/String;)V+11 107 v ~StubRoutines::call_stub 108 V [libjvm.so+0x690c66] JavaCalls::call_helper(JavaValue*, methodHandle*, JavaCallArguments*, Thread*)+0x1056 109 V [libjvm.so+0x6d2072] jni_invoke_static(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x362 110 V [libjvm.so+0x6ee8da] jni_CallStaticVoidMethod+0x17a 111 C [libjli.so+0x7bdf] JavaMain+0x81f 112 C [libpthread.so.0+0x8184] start_thread+0xc4 113 114 Java frames: (J=compiled Java code, j=interpreted, Vv=VM code) 115 j Prompt.getLine(Ljava/lang/String;)Ljava/lang/String;+0 116 j Prompt.main([Ljava/lang/String;)V+11
Re: std.conv.to!string refuses to convert a char* to string.
On Saturday, 9 December 2017 at 05:55:21 UTC, Venkat wrote: I am trying out the DJni library (https://github.com/Monnoroch/DJni). For some reason std.conv.to!string doesn't want to convert a char* to a string.The lines below are taken from the log. I see that the last frame is at gc_qalloc. I am not sure why it failed there. Can anybody elaborate on what is going on here ? Thanks in advance. I've got no idea, but can you verify that you can print it with printf? Can you allocate other GC memory? Can you try using fromStringz instead of to!string and see what that does? Just shots in the dark...
Re: std.conv.to!string refuses to convert a char* to string.
Thanks for the quick response. std.string.fromStringz did the trick. I am not sure what was the deal with to!string.
Re: Temporary objects as function parameters or when-is-this-shit-going-to-end?
On Tuesday, 17 October 2017 at 19:00:38 UTC, Steven Schveighoffer wrote: In this case, an extra destructor call is made without a corresponding postblit or constructor. -Steve https://issues.dlang.org/show_bug.cgi?id=18050
Get pointer or reference of an element in Array(struct)
Is there a way to get the pointer or reference of an element in Array(T)? https://run.dlang.io/gist/70fd499afe8438d4877f57aec90c3091?compiler=dmd The assertion seems to fail below. Value copy is not is intended here. module test; void main() { struct Data { int id; } import std.container.array; Array!Data gallery; Data d1; gallery.insertBack(d1); auto d2 = gallery[0]; d2.id = 1; assert(d2.id == gallery[0].id, "neither ref nor pointer"); }
Re: Parallel reads on std.container.array.Array
On Saturday, 9 December 2017 at 01:34:40 UTC, Arun Chandrasekaran wrote: So I tried the same on Haswell processor with LDC 1.6.0 and it crashes ``` === Starting D version === Took 1 sec, 107 ms, and 383 μs to load 100 items. Gonna search in parallel... *** Error in `./dmain-ldc': double free or corruption (fasttop): 0x00edc6e0 *** *** Error in `./dmain-ldc': double free or corruption (fasttop): 0x00edc6e0 *** ``` Learnt (from David Nadlinger) that due to lifetime management of transitory ranges, they can't be used for parallel reads. Iterating by index has solved the problem. However, accessing the items in Array results in value copy. Is that expected? How can I fix this? http://forum.dlang.org/post/cfhkszdbkaezprbzr...@forum.dlang.org
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote: Is there a way to get the pointer or reference of an element in Array(T)? [...] auto d2 = gallery[0]; auto d2 = &gallery[0];
Re: Get pointer or reference of an element in Array(struct)
On Saturday, 9 December 2017 at 06:38:46 UTC, anonymous wrote: On Saturday, 9 December 2017 at 06:15:16 UTC, Arun Chandrasekaran wrote: Is there a way to get the pointer or reference of an element in Array(T)? [...] auto d2 = gallery[0]; auto d2 = &gallery[0]; Thanks. Just curious why reference can't be obtained here. Saves nasty null checks in most places.
Re: Sort in return statement
On Saturday, 9 December 2017 at 04:31:33 UTC, SimonN wrote: Yes, this works, and your algorithm would even accept arbitary random-access ranges, not merely arrays. Would be nice if I could do it all as a 'one liner': // int[] draw8Numbers() { import std.algorithm.sorting : sort; import std.random : randomShuffle; import std.meta : aliasSeqOf; import std.range : iota; import std.range : take; import std.array : array; // return a sorted array of 8 random numbers, between 1..45 inclusive. return sort(randomShuffle([ aliasSeqOf!(iota(1,46)) ]).take(8)).array; } // ---
Re: Sort in return statement
On Saturday, 9 December 2017 at 03:24:52 UTC, codephantom wrote: On Saturday, 9 December 2017 at 02:45:35 UTC, rjframe wrote: `sort` returns a SortedRange of ushorts, not an array of ushorts. Make it: ``` import std.array : array; return sort(numbers.take(8)).array; ``` --Ryan That's it! Thanks Ryan. You can also return a lazy range: ``` auto draw8Numbers() { import std.meta : aliasSeqOf; import std.range : iota, take; ushort[] numbers = [ aliasSeqOf!(iota(1,46)) ]; import std.random : randomShuffle; randomShuffle(numbers); import std.algorithm.sorting : sort; return sort(numbers[].take(8)); } void main() { import std.array; ushort[] nbrs = draw8Numbers.array; // evaluated after return, during assingment } ```
Re: Sort in return statement
On Saturday, 9 December 2017 at 02:45:35 UTC, rjframe wrote: On Sat, 09 Dec 2017 02:34:29 +, codephantom wrote: Anyone got ideas on how to get sort() working in the *return* statement? // ushort[] draw8Numbers() { import std.meta : aliasSeqOf; import std.range : iota; ushort[] numbers = [ aliasSeqOf!(iota(1,46)) ]; import std.random : randomShuffle; randomShuffle(numbers); import std.range : take; import std.algorithm.sorting : sort; return numbers.take(8); /* ok */ //return sort(numbers.take(8)); /* I want this, but it won't work. */ } // - `sort` returns a SortedRange of ushorts, not an array of ushorts. Make it: ``` import std.array : array; return sort(numbers.take(8)).array; ``` --Ryan Use .release to obtain the underlying array. No need to do another allocation! ``` numbers.take(8).sort.release; ```