Re: std.file.read returns void[] why?

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 17 April 2014 at 21:27:44 UTC, Steven Schveighoffer 
wrote:
On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra 
monarchdo...@gmail.com wrote:
void[] will only make sense once you've accepted that 
void.sizeof == 1.


It is already accepted that when we talk about length in a 
void[], it's the number of bytes. But the data has no formal 
type.


Well, I always thought that void[] slice meant there are 
slice.length items, starting at slice.ptr. I don't know the size 
of the individual items.


For example, in C, a lot of functions take void* first, size_t 
num, size_t width.


In fact, most of druntime functions take void[] buffers that 
work that way. There's an associated typeid, so that you can now 
how large each individual items are.


But any array implicitly casts to void[]. This is why it makes 
a good parameter for read or write (when reading or writing the 
binary data).


I guess. I just find it kind of strange that a type that has no 
type would have an actual sizeof. Then again, I thought void had 
no sizeof in C, but I just checked, and I was wrong.


Well, I guess void[] is C++'s char* for indiscriminate 
buffers. Speaking of which, does void* trigger strict 
aliasing in D? This subject seems like a hot potato no-one 
wants to touch.


No, it's equivalent to void *, not char *.

in D, ubyte[] would be the equivalent of C's char *.

-Steve


Correct.


Re: std.file.read returns void[] why?

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On Fri, 18 Apr 2014 02:04:16 -0400, monarch_dodra monarchdo...@gmail.com  
wrote:



On Thursday, 17 April 2014 at 21:27:44 UTC, Steven Schveighoffer wrote:
On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra  
monarchdo...@gmail.com wrote:
void[] will only make sense once you've accepted that void.sizeof ==  
1.


It is already accepted that when we talk about length in a void[], it's  
the number of bytes. But the data has no formal type.


Well, I always thought that void[] slice meant there are slice.length  
items, starting at slice.ptr. I don't know the size of the individual  
items.


For example, in C, a lot of functions take void* first, size_t num,  
size_t width.


In fact, most of druntime functions take void[] buffers that work that  
way. There's an associated typeid, so that you can now how large each  
individual items are.


import std.stdio;

void main()
{
   int[] x = new int[5];
   void[] y = x;
   writeln(y.length); // 20
}

When a function takes a typeid, that usually is because the translation is  
not made. In druntine cases, the compiler is removing the type, and  
sticking it into the typeid instead. But the length has not been  
translated to bytes! It's still in terms of the original type.


In those cases, it's equivalent to:

void[] y = *cast(void[]*)x;

which would make y.length == 5.

But any array implicitly casts to void[]. This is why it makes a good  
parameter for read or write (when reading or writing the binary data).


I guess. I just find it kind of strange that a type that has no type  
would have an actual sizeof. Then again, I thought void had no sizeof in  
C, but I just checked, and I was wrong.


It's a little strange, but while void has no size, void[] *does* have a  
size. The size is in bytes. You can think of an array as starts at this  
address, and ends at that address. Because addresses are in terms of  
bytes, so is the length of that array.


I admit, I didn't think C's void had a size ;) I'm pretty sure it doesn't  
in D, but then again...


-Steve


Re: Dynamically Sized Structs

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On Fri, 18 Apr 2014 00:05:03 -0400, Kagamin s...@here.lot wrote:

Well, it's proof of concept of bound checked variable-size struct, I  
wrote it in a minute. It even compiles and runs.


Please take no offense :) I just was pointing out a difference between a  
hand-managed and hand-written struct that I might have written and the one  
that you created. Depending on usage, yours might be sufficient.


Note, you could probably, with mixin magic, make a version that could be  
emplaced inside a struct or class without an extra indirection.


-Steve


Re: std.file.read returns void[] why?

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 13:08:04 UTC, Steven Schveighoffer 
wrote:
I admit, I didn't think C's void had a size ;) I'm pretty sure 
it doesn't in D, but then again...


-Steve


Yeah... static assert(void.sizeof == 1); passes :/

So in any case, long story short:
void[]: This is an un-typed buffer, pointing to a memory 
location that starts at .ptr, and is .length bytes in length.


Also, as far as the GC is concerned, void is a type that should 
be scanned (whether or not the data originally allocated was 
marked as such is another issue).


Re: Dynamically Sized Structs

2014-04-18 Thread Kagamin via Digitalmars-d-learn
Oh, and I don't believe, that a variable-size struct can be 
emplaced inside fixed-size struct or class. Only as a smart 
pointer from the example.


Re: Dynamically Sized Structs

2014-04-18 Thread Kagamin via Digitalmars-d-learn
I mean, it doesn't cover all scenarios, but can be extended to 
support them. The indexes array does just that: it's emplaced 
without indirection, but still is bound checked.


Re: Dynamically Sized Structs

2014-04-18 Thread Kagamin via Digitalmars-d-learn
On Friday, 18 April 2014 at 13:10:28 UTC, Steven Schveighoffer 
wrote:
Note, you could probably, with mixin magic, make a version that 
could be emplaced inside a struct or class without an extra 
indirection.


Speaking about mixin magic, you probably suggest to do it overly 
generically, though it seems, use cases for variable size structs 
are specialized, specialized code and specialized data 
structures, so I suspect specialized approach will be more 
productive, intuitive, understandable, simple and straightforward 
for such tasks, generic approach is probably inadequate here. But 
if you can do it, you can try.


On Concurrency

2014-04-18 Thread Nordlöw
Could someone please give some references to thorough explainings 
on these latest concurrency mechanisms


- Go: Goroutines
- Coroutines (Boost):
  - https://en.wikipedia.org/wiki/Coroutine
  - 
http://www.boost.org/doc/libs/1_55_0/libs/coroutine/doc/html/coroutine/intro.html
- D: core.thread.Fiber: 
http://dlang.org/library/core/thread/Fiber.html

- D: vibe.d

and how they relate to the following questions:

1. Is D's Fiber the same as a coroutine? If not, how do they 
differ?


2. Typical usecases when Fibers are superior to 
threads/coroutines?


3. What mechanism does/should D's builtin Threadpool ideally use 
to package and manage computations?


4. I've read that vibe.d's has a more lightweight mechanism than 
what core.thread.Fiber provides. Could someone explain to me the 
difference? When will this be introduced and will this be a 
breaking change?


5. And finally how does data sharing/immutability relate to the 
above questions?


Re: Dynamically Sized Structs

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn

On Fri, 18 Apr 2014 12:59:35 -0400, Kagamin s...@here.lot wrote:

Oh, and I don't believe, that a variable-size struct can be emplaced  
inside fixed-size struct or class. Only as a smart pointer from the  
example.


It goes at the end. Then you need to allocate the whole thing with that in  
mind.


-Steve


Re: On Concurrency

2014-04-18 Thread Nordlöw
Correction: The references I gave _are_ theoretically thorough, 
so I'm satisified with these for now. I'm however still 
interested in the D-specific questions I asked.


Reducing an array

2014-04-18 Thread Tim Holzschuh via Digitalmars-d-learn

Hi there,

I try to remove all equal elements of an array, thus [2,2] -- [2].

I thought this maybe would be possible with std.algorithm.reduce, but at 
least the way I tried it doesn't work:


arr.reduce( (a,b) = a != b );

Is there a simple solution using Phobos-functions?

Thank you,
Tim


Re: Reducing an array

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On Thu, 17 Apr 2014 09:46:25 -0400, Tim Holzschuh via Digitalmars-d-learn  
digitalmars-d-learn@puremagic.com wrote:



Hi there,

I try to remove all equal elements of an array, thus [2,2] -- [2].

I thought this maybe would be possible with std.algorithm.reduce, but at  
least the way I tried it doesn't work:


arr.reduce( (a,b) = a != b );


reduce doesn't do what you think it does. It applies a function to all  
elements, keeping track of the result of each function call, and passing  
it to the next one.


In other words, reduce!fn(a, range) is like doing this:

fn(range[5], fn(range[4], fn(range[3], fn(range[2], fn(range[1],  
fn(range[0], a));


What you want is probably uniq:

http://dlang.org/library/std/algorithm/uniq.html

Note that it works on a SORTED range, so you want to sort first.

Note also that what it returns is not an array, but a lazily iterated  
range over the array. If you want another array, this is the code I would  
use:


arr.sort();
arr = arr.uniq.array();

-Steve


Re: Reducing an array

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 20:27:20 UTC, Steven Schveighoffer 
wrote:
Note also that what it returns is not an array, but a lazily 
iterated range over the array. If you want another array, this 
is the code I would use:


arr.sort();
arr = arr.uniq.array();

-Steve


Out of curiosity, if the requirement was to *also* preserve 
ordering (eg: remove all non-first elements), how would you go at 
it?


[2, 1, 1, 3, 2, 3] = [2, 1, 3];

Maybe std.algorithm's `makeIndex` would help here?

Bonus points for doing it inplace.


Re: Reducing an array

2014-04-18 Thread bearophile via Digitalmars-d-learn

monarch_dodra:

Out of curiosity, if the requirement was to *also* preserve 
ordering (eg: remove all non-first elements), how would you go 
at it?


[2, 1, 1, 3, 2, 3] = [2, 1, 3];

Maybe std.algorithm's `makeIndex` would help here?

Bonus points for doing it inplace.


This preserves ordering and it's in-place. Not tested much:

void main() {
import std.stdio, std.traits;

auto data = [2, 1, 1, 3, 2, 3];

bool[ForeachType!(typeof(data))] seen;
size_t pos = 0;
foreach (immutable i; 0 .. data.length)
if (data[i] !in seen) {
if (pos != i)
data[pos] = data[i];
seen[data[i]] = true;
pos++;
}
data.length = pos;

data.writeln;
}


Bye,
bearophile


arrays in DMD V2

2014-04-18 Thread steven kladitis via Digitalmars-d-learn

import std.stdio;


void main()
{

inta0[];
inta1[][];
string a2[][];
string a3[][string];
string a4[][string][string];
//string a4[string][string][string]; is this the same as above
inta5[][string][string][string];
inta6[string][int][string][float];
inta7[int][int][string];

// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???




}

//help please


Re: arrays in DMD V2

2014-04-18 Thread Jesse Phillips via Digitalmars-d-learn

On Saturday, 19 April 2014 at 00:27:32 UTC, steven kladitis wrote:

import std.stdio;


void main()
{

inta0[];
inta1[][];
string a2[][];
string a3[][string];
string a4[][string][string];
//string a4[string][string][string]; is this the same as 
above

inta5[][string][string][string];
inta6[string][int][string][float];
inta7[int][int][string];

// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???

}

//help please


First, don't use that syntax. everything should be placed on the 
type:


Second,

void main() {
inta5[][string][string][string];
pragma(msg, typeof(a5));
}

Third,

http://dlang.org/arrays.html
http://dlang.org/hash-map.html


Re: arrays in DMD V2

2014-04-18 Thread steven kladitis via Digitalmars-d-learn
Thanks, I am trying to understand what I am doing. The docs seem 
unclear to me on how to initialize these.  I think there are 
three ways.

 with brackets , the other with foreach or direct assignments.
-- here is a longer version
-- uncomment out the assignments to see the errors I get.
-- I am trying to understand arrays.



On Saturday, 19 April 2014 at 01:13:55 UTC, Jesse Phillips wrote:
On Saturday, 19 April 2014 at 00:27:32 UTC, steven kladitis 
wrote:

import std.stdio;


void main()
{

inta0[];
inta1[][];
string a2[][];
string a3[][string];
string a4[][string][string];
//string a4[string][string][string]; is this the same as 
above

inta5[][string][string][string];
inta6[string][int][string][float];
inta7[int][int][string];

// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???

}

//help please


First, don't use that syntax. everything should be placed on 
the type:


Second,

void main() {
inta5[][string][string][string];
pragma(msg, typeof(a5));
}

Third,

http://dlang.org/arrays.html
http://dlang.org/hash-map.html




Re: arrays in DMD V2

2014-04-18 Thread steven kladitis via Digitalmars-d-learn

import std.stdio;


void main()
{

inta0[];
inta1[][];
string a2[][];
string a3[][string];
string a4[][string][string];
//string a4[string][string][string]; is this the same as above
inta5[][string][string][string];
inta6[string][int][string][float];
inta7[int][int][string];

// how do you initialize each array, what type are they 
multidimensional???
// how do you define all of the above with limits on each 
dimension???


a0  = [1,2,3];
//writefln( a0 );

a1  = [ [ 1,2,3 ],[4,5,6]];
//writefln( a1 );

a2  = [ [a,b ],[ c,d ] ];

//writefln ( a2 );

//a3  = [ [a,b ],[ c,d ] ];
// does not work

//a4= [ [a,b ];
// does not work.

//a5  = [ [1,a,b]];

  pragma(msg, typeof(a0));
   pragma(msg, typeof(a1));
pragma(msg, typeof(a2));
 pragma(msg, typeof(a3));
  pragma(msg, typeof(a4));
   pragma(msg, typeof(a5));
pragma(msg, typeof(a6));
 pragma(msg, typeof(a7));
//  does not work

//a6= [ 1,[a,1,b,4.0]];
// does not work


//a7 = [ 1,1,a];
// does not work


// also how would you do in a foreach?



}