Re: Help Finding Strange Memory Bug (Code linked to C library)

2013-12-08 Thread Craig Dillabaugh

On Saturday, 7 December 2013 at 23:11:39 UTC, Rémy Mouëza wrote:

My strategy here would be to:
A.  run the program in a debugger, say GDB, to get a exhaustive 
stacktrace for hints about where to look at.
B. have a quick look at the library directly (the Use the 
Source Luke strategy).


Since I was curious about your problem (you had everything 
correct - this should not fail), and have no access to your 
code, I checked out the shapelib code from its cvs public 
repository and found out that the last pointer, `double * 
padfMaxBound` is actually a pointer to an array of 4 elements: 
in shapelib/shpopen.c: SHPGetInfo: starting line 823:

for( i = 0; i  4; i++ )
{
if( padfMinBound != NULL )
padfMinBound[i] = psSHP-adBoundsMin[i];
if( padfMaxBound != NULL )
padfMaxBound[i] = psSHP-adBoundsMax[i];
}

This also explains why it does not segfault when you pass a 
null pointer: no array out of bounds happen then.


padfMaxBound should point to a `double []` or `double [4]` 
instead of a mere `double`.


I also ended checking the API documentation 
(http://shapelib.maptools.org/shp_api.html) and they do 
document that padfMinBound and padfMaxbound are pointers to 4 
values X, Y, Z and M in a four entry array.
You may want to check if there isn't any other of those C 
tricks hiding in your D bindings.


Good catch.  Now that you pointed it out I cannot for the life of
me figure out why I didn't think to check that!  To make matters
worse I didn't even have to go check the CVS, because I compiled
the library from source and had all the C code sitting just a
couple of 'cd's away.  For some reason it never occurred to me
that it might be expecting an array - even though one might
reasonably assume that the bounds on a multidimensional dataset
would have more than one dimension.

Thank you.






On 12/07/2013 04:29 PM, Craig Dillabaugh wrote:

Hello,
I recently wrote bindings to the C-library Shapelib (it 
reads/writes a

common file format used in Geographic Information Systems).

I've been trying to write a small test program to make sure my 
bindings

'work' and I've come across a bizarre memory bug.  I THINK I've
identified the code that causes the problem, but I have no 
idea why.


My test-suite includes this function:

void shapeRead(string filename) {
  SHPHandle hShp = SHPOpen( std.string.toStringz( filename ), 
rb );


  int n, shp_type;
  double pad_min_bound, pad_max_bound;

  SHPGetInfo( hShp, n, shp_type, pad_min_bound, 
pad_max_bound);


  SHPClose( hShp );
}

If I comment out the SHPGetInfo call, then the segmentation 
fault
doesn't happen, but if its there then the program segfaults 
AFTER the

shapeRead function exits (eg. SHPClose runs fine) ).

In fact if I call SHPGetInfo as follows, the crash doesn't 
occur:
SHPGetInfo( hShp, n, shp_type, pad_min_bound, null); //NULL 
pointer

last arg.

So in C the function SHPGetInfo is:

void SHPAPI_CALL
  SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * 
pnShapeType,
  double * padfMinBound, double * padfMaxBound 
);


While my D binding is (pretty much the same):

extern( C ) void SHPGetInfo( SHPHandle hSHP, int* pnEntities,
  int* pnShapeType, double* padfMinBound, double* 
padfMaxBound );



I have no idea what is going on.  The sizes of ints and 
doubles are 4
and 8 bytes using gcc on my system (which is how I compiled my 
C
library) so those match the sizes of the corresponding D types 
[I
thought maybe there was a type-size mismatch and that was 
causing

something to be overwritten, but it doesn't appear that way].

Any hints on where to look next would be appreciated.

Craig


Re: Help Finding Strange Memory Bug (Code linked to C library)

2013-12-08 Thread Craig Dillabaugh

On Saturday, 7 December 2013 at 23:35:49 UTC, Ali Çehreli wrote:

On 12/07/2013 03:11 PM, Rémy Mouëza wrote:

 the last pointer, `double * padfMaxBound` is actually a
pointer to an array
 of 4 elements:

Great sleuthing! :)

This thread is a good example of C's Biggest Mistake:


http://www.drdobbs.com/architecture-and-design/cs-biggest-mistake/228701625

Ali


If by C's Biggest Mistake you mean Craig's Biggest Mistake
you are incorrect. As hard as it may be to imagine I've done even
dumber things:o)

It is too bad that when I do do something dumb I tend to post 
about it on the internet!


Cheers


the Result Type

2013-12-08 Thread seany
std.algorithm.splitter seems to return all its return values as a 
type Result, without quotes, and i dont not seem to be able to 
cast it to string[] or int[]  with cast(string[]) ( or even cast 
(string) - i tried that too).


I tried to use a function

void function(T, R)(T arr, out R output)
{

foreach(elem; arr)
{
output ~= elemM
}
}

i have an occasion where R is int[], and one where it is 
string[]; and
but it says, that int can not be appended to string[], and string 
can not be appended to int[], and compilation fails.


Nonetheless removing either of the occasions, solves it.

how to deal with  this Result type?


Re: the Result Type

2013-12-08 Thread Ali Çehreli

On 12/08/2013 12:24 AM, seany wrote:

 std.algorithm.splitter seems to return all its return values as a type
 Result, without quotes, and i dont not seem to be able to cast it to
 string[] or int[]  with cast(string[]) ( or even cast (string) - i tried
 that too).

 I tried to use a function

 void function(T, R)(T arr, out R output)
 {

 foreach(elem; arr)
 {
  output ~= elemM
 }
 }

 i have an occasion where R is int[], and one where it is string[]; and
 but it says, that int can not be appended to string[], and string can
 not be appended to int[], and compilation fails.

 Nonetheless removing either of the occasions, solves it.

 how to deal with  this Result type?

Many other algorithms return Result, which are independent from each 
other. :) (Lookup Voldemort types.)


Such types are just lazy ranges. When you eagerly need an actual array 
of the elements, call std.array.array on Result:


import std.array;
import std.algorithm;

void main()
{
auto input = hello world;
auto splittedWords = input.splitter(' ').array;

static assert(is (typeof(splittedWords) == string[]));
}

Ali



Re: Equality == comparisons with floating point numbers

2013-12-08 Thread Abdulhaq




... I thought I did, but now I'm up against an interesting 
conundrum: while equality == comparison can fail here for 
32-bit, isIdentical comparison can fail even for 64-bit, 
although only for the release-mode build.


What's particularly odd is that if before calling 
assert(isIdentical( ... )) I use writeln to print the value of 
isIdentical(...) to the screen, then it prints true, and the 
assertion passes.  If I don't have the print statement, then 
the assert fails.


I'm presuming that calling writefln to print the variable 
involves it being taken off the FPU?


I'm just guessing now but it seems that you are in an area that 
changes depending on which compiler you are using (how does it 
compile the FP instructions, does it use SSE instructions, how is 
it checking equality) and which exact processor are you on, does 
it support IEEE754, does the compiler try to support IEEE754 
exactly? I haven't seen much in the forums about FP behaviour in 
e.g. dmd. E.g. how does it deal with the issues raised in 
http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf? The people who 
know these things can found discussing them at 
http://forum.dlang.org/thread/khbodtjtobahhpzma...@forum.dlang.org?page=3#post-l4rj5o:24292k:241:40digitalmars.com 
:-).


It's generally held that checking FP numbers for exact equality 
isn't practical and it's better to go for equality within a 
certain tolerance - any reason why you're not happy with that :-)?




Re: the Result Type

2013-12-08 Thread seany

O_O

with that knowledge, would also be possible to define new types 
(not aliases, but new encapsulated types) representing things 
such as Graph, Ring, Topology, surreal number, etc?


I dont find this in your book, would you consider either adding 
this Volodemrot types, or in case they already are there, give me 
a link (both the introductions in d wiki and dr. doobs blog was 
very short)


Re: the Result Type

2013-12-08 Thread Marco Leise
Am Sun, 08 Dec 2013 09:24:53 +0100
schrieb seany se...@uni-bonn.de:

 std.algorithm.splitter seems to return all its return values as a 
 type Result, without quotes, and i dont not seem to be able to 
 cast it to string[] or int[]  with cast(string[]) ( or even cast 
 (string) - i tried that too).
 
 I tried to use a function
 
 void function(T, R)(T arr, out R output)
 {
 
 foreach(elem; arr)
 {
  output ~= elemM
 }
 }
 
 i have an occasion where R is int[], and one where it is 
 string[]; and
 but it says, that int can not be appended to string[], and string 
 can not be appended to int[], and compilation fails.
 
 Nonetheless removing either of the occasions, solves it.
 
 how to deal with  this Result type?

That confused me as a beginner as well until I understood that
these Result types are actually just structs. They are
generated at compile-time and share commonly known methods
like .front or .popFront() that identify them as ranges.

Arrays are a sub-set of ranges. The algorithms in D typically
take a range as input and a range as output. Where possible
they try not to go over the whole input at once, but only as
needed. This is called lazy evaluation and is one reason you
don't get returned a complete array. In some cases the input
might even be infinite: Try returning an array for splitter
over a random number generator as input range!

So what do you do with a Range struct? There are 3 options:
* If you really want an array you can call the function
  array() on the Result:
  http://dlang.org/phobos/std_array.html#.array
* If you want to further process your Result with other
  algorithms, just chain them together. E.g.:
  arr.splitter(abc).sort.uniq();
* If you want to iterate over the Result, foreach works on
  those Result ranges:
  foreach (elem; arr.splitter(abc)) {...}

The range concept makes it easy to apply any kind of algorithm
on stuff that can be represented as a consecutive items of
the same type. And it allows those algorithms to only do as
much work as needed to get the result. A good example is
http://dlang.org/phobos/std_range.html#take , which sets a
limit to the number of items to be used from the input.
So you can have an infinite random number generator, but only
take the first 50 numbers like so: randomGen.take(50);

By the way File(myfile).byLine() gives you a range over the
lines of text of myfile. That's quite handy if you have an
array of strings like a dictionary in a text file.

-- 
Marco



Re: File Picker

2013-12-08 Thread Marco Leise
Am Sun, 08 Dec 2013 05:49:34 +0100
schrieb Malkierian rhyd...@gmail.com:

 On Saturday, 7 December 2013 at 23:18:18 UTC, Adam D. Ruppe wrote:
  On Saturday, 7 December 2013 at 23:00:00 UTC, Malkierian wrote:
  Is there anything in D that currently brings up a window to 
  find and choose a file, or am I going to have to make it 
  myself?  Isn't there a built-in something or other I can hook 
  into in Windows?
 
  Yeah, on Windows, you can just call the GetOpenFileName 
  function (or GetSaveFileName if saving) and use the common 
  dialog.
  http://msdn.microsoft.com/en-us/library/windows/desktop/ms646927(v=vs.85).aspx
 
 
  I wrote this quick example to show how you can use it in D:
 
  http://arsdnet.net/dcode/open.d
 
 
  Since the windows headers distributed with dmd are woefully 
  incomplete, the first thing I did was copy/paste the struct and 
  file definition from MSDN.
 
  Then, below that, is the main() function which shows how to 
  call it. There's a lot of customization you can do there, see 
  the Microsoft docs for more info (or search the web for any C 
  examples, the function works the same way in D.)
 
 Man, that's great, thanks.  However, I have it set up in my 
 application, and when I first call it, I get this window:
 
 http://gyazo.com/02bc18bdc23fdf3c24aa4ff70b46be1f
 
 Then, if I cancel and open it again, I get the actual browser, 
 but then my program freezes up:
 
 http://gyazo.com/4e5e873e57cd7a234d56c7a42198ab89
 
 Any idea why it doesn't work the first time, but then does the 
 second and freezes?

Maybe it requires a working Windows® event loop in your
application, as is typical for GUI applications on any
platform. Windows typically generate all sorts of events, like
mouse clicks, key strokes, resize events etc. They add up in
the event queue and a white window like yours is typical of
Windows® to indicate that this application is no longer
working off its event loop. (Or in your case never started to
do so.)
If that is indeed the problem, worry not, because most events
can be handled by the default handler, but you'll need to
write an simple event loop. I'm not sure, but it could be that
you'll need to create a dummy window as well since event loops
work with window handles. Maybe it is ok to pass 0 everywhere,
maybe you need a valid handle.

-- 
Marco



Re: the Result Type

2013-12-08 Thread Marco Leise
Am Sun, 08 Dec 2013 09:59:55 +0100
schrieb seany se...@uni-bonn.de:

 O_O
 
 with that knowledge, would also be possible to define new types 
 (not aliases, but new encapsulated types) representing things 
 such as Graph, Ring, Topology, surreal number, etc?

All these Result types are simply structs. Structs with
template parameters. Your question is a bit vague. You will
not be able to use D algorithms on anything but consecutive
items of the same type as I worte in my other post, if that
is the question.

Other than that the Result types are just:

struct Result(T)
{
...
}

You can call them templated struct or generic type. Many
languages offer this in some way including C++, Java, Delphi
and C#. If you have used any of those programming languages
before you are probably already familiar with the concept.

In the most simple form you can use them to create type
specialized containers, graphs or this:

struct Matrix(T, size_t width, size_t height)
if (isNumeric!T)
{
...
}

(A matrix with a fixed width and height that can be used if T
 is a built-in integer or floating point type.)

-- 
Marco



Re: Threading methodology

2013-12-08 Thread Marco Leise
Am Sat, 07 Dec 2013 17:53:06 +0100
schrieb Frustrated c1514...@drdrb.com:

 I have to process n arrays in some partial order. Instead of all 
 working only on the n arrays and reusing them, [...]

Wait, what partial order and how is it relevant? Who is all
in all working? Why only the n arrays, I thought those are
all?

 if I duplicate
 them(effectively write once read many) does that make things 
 simpler and allow threading to be used more efficiently?

 Basically, instead of having to worry about threads waiting on 
 other threads when they are writing the data I can just have each 
 thread write to it's own duplicate array(although hopefully won't 
 have to duplicate the buffer as that should not be necessary?). 
 This should then make it much easier to deal because there is 
 essentially no write contention.
 
 If I'm not mistaken this is basically the idea of TLS? (read many 
 write once time idea so that one doesn't have to lock memory 
 which stalls other threads in some cases and/or uses expensive 
 locks?)

The idea of TLS in D as I understand it, is that your global
variables are not shared between threads by default as is the
case in C. So as you point out you need not lock them since each
thread has its own copy.

 The case I gave above is a little more simplified than the actual 
 issue but the idea is simply that by writing to a threads own 
 buffer reduces inter-threading issues and increases performance. 
 One issue I have is how to combine the data back.
 
 In fact, the real case is basically n arrays. Each array is 
 processed to create m arrays, which are processed to create l 
 arrays and so on. Not all processing of each array takes the same 
 time and I would like to multithread the overall processing by 
 allowing threads to pick which (sub)arrays need processing and 
 process them, but in their own space, and at the end send them 
 back to the main thread. (But one thread may need access to 
 another's buffer but only for reading so it shouldn't be a 
 problem)
 
 This uses a lot more memory but if there are N threads and n 
 arrays and N  n, no thread will be wasted. (in my case I would 
 have n*m*l which will almost always be larger than N so threads 
 will always be doing something except towards the end)
 
 Another way to see the problem is to think of a digraph where 
 each node processes all connected to it. All independent nodes 
 must be processed first which then works itself up to the most 
 dependent nodes. Every processed node uses a new buffer to write 
 to instead of writing to the same buffer which locks that buffer 
 and all other threads that may be trying to read from it(which is 
 the key in my mind that this method is better except it requires 
 more memory).
 
 I will of course mark nodes as being processed or not so threads 
 don't work on unprocessed buffers and this allows dependent nodes 
 to start up once all there dependencies become processed.
 
 Does any of this make sense?

It is a little clearer now, but I didn't fully understand it
yet. The sub-arrays need to be processed before the parent
arrays can be completed? When does a thread need access to
another thread's buffer? What if you have only 1 CPU core and
there is only one thread and one buffer? Does your algorithm
break? :p

-- 
Marco



Re: the Result Type

2013-12-08 Thread Ali Çehreli

On 12/08/2013 12:59 AM, seany wrote:

 I dont find this in your book, would you consider either adding this
 Volodemrot types,

I think at least a short mention is in order. :)

There are two reasons why they don't appear in the book (yet):

1) They are not a proper language feature, rather a happy discovery. (I 
remember Andrei's original newsgroup post about this discovery but I 
cannot find it at this time.)


2) They were discovered after I started writing the book.

Ali



Re: the Result Type

2013-12-08 Thread bearophile

Ali Çehreli:

When you eagerly need an actual array of the elements, call 
std.array.array on Result:


import std.array;
import std.algorithm;

void main()
{
auto input = hello world;
auto splittedWords = input.splitter(' ').array;


Or just use the eager split() function.

Bye,
bearophile


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

Jay Norwood:


enum Suit { spades, hearts=4, diamonds=10, clubs }

foreach (i, member; EnumMembers!Suit)


Here 'i' is the index of the enumeration type tuple.

This code lacks the [] I added in my code, so your foreach is a 
static one. To tell them apart when I read the code I sometimes 
add a comment:


/*static*/ foreach (i, member; EnumMembers!Suit)

Bye,
bearophile


Re: Equality == comparisons with floating point numbers

2013-12-08 Thread Ali Çehreli

On 12/08/2013 01:55 AM, Joseph Rushton Wakeling wrote:

 back to my original solution of approxEqual

I don't know whether it helps here but just to complete the picture, 
there is also std.math.feqrel:


  http://dlang.org/phobos/std_math.html#.feqrel

Ali



Re: Help Finding Strange Memory Bug (Code linked to C library)

2013-12-08 Thread Ali Çehreli

On 12/08/2013 12:16 AM, Craig Dillabaugh wrote:

 I cannot for the life of me figure out why I didn't think to check that!

D has already ruined your mind! :p

Ali



Re: the Result Type

2013-12-08 Thread seany

On Sunday, 8 December 2013 at 11:02:40 UTC, Ali Çehreli wrote:
(I remember Andrei's original newsgroup post about this 
discovery but I cannot find it at this time.)


http://www.digitalmars.com/d/archives/digitalmars/D/announce/Voldemort_Types_in_D_23511.html

this?


array question

2013-12-08 Thread seany

consider the follwoing:

import tango.io.Stdout, tango.io.Path, tango.text.Util;
import std.algorithm, std.string , std.stdio, std.array, 
std.conv, std.regex, std.typecons;


//i know al imports are not necessary for this example, just ^c^v 
from my actual code


alias string[] surSegments


void makeHashmap(T,R)(T[] plainArr, string hashes, out R[] 
hashMap)

{
//first split the hashes
string [] hashesArr = std.algorithm.splitter(hashes, ',').array;

for(int i = 0; i  plainArr.length; i++)
{
R hashElement;
for(int j = 0; j  hashesArr.length; j++)
{
hashElement[hashesArr[j]] =  = plainArr[i][j];
}
hashMap ~= hashElement;
}

}


void main()
{

surSegments[] s = [[a,b,c,d], [e,f,g,h]];
h = 1,2,3,4;
surSegments[string][] ss;

makeHashmap(s, h, ss);


}

i expect ss to look like :
[
[1 = a, 2 = b, 3 = c, 4 = d],
[1 = e, 2 = f, 3 = g, 4 = h]
]

etc.

instead i get compilation error:
Error: cannot implicitly convert expression 
(hashesArr[cast(ulong)j]) of type string to string[]



isn't hashElement of type surSegment[string] and 
hashElement[somestring] of type string, just like plainArr[i][j] 
??




Re: Equality == comparisons with floating point numbers

2013-12-08 Thread Joseph Rushton Wakeling

On 08/12/13 12:13, Ali Çehreli wrote:

I don't know whether it helps here but just to complete the picture, there is
also std.math.feqrel:

   http://dlang.org/phobos/std_math.html#.feqrel


Thanks! :-)  Checking the value of feqrel followed by isIdentical looks like it 
might work.  (OK, technically the two are telling you equivalent things, but I'd 
rather have built-in redundancy of the tests than revert to approxEqual.)




Running DMD tests

2013-12-08 Thread Dmitry Olshansky

I'm trying my hand at a simple pesky bug in DMD.
About to run test suite but must be doing something wrong.
Any help would be appreciated.

I followed this:
http://wiki.dlang.org/Building_DMD#How_to_run_the_test_suite_in_dmd.2Ftest

And here is what I get (I'm on 64-bit Ubuntu):

dmitry@dmitry-VirtualBox ~/dmd2/src/dmd/test $ make MODEL=64
Creating output directory: test_results
Building d_do_test tool
OS: linux
Running runnable tests
make[1]: *** No rule to make target `test_results/runnable/A16.d.out', 
needed by `run_runnable_tests'.  Stop.

make: *** [start_runnable_tests] Error 2
dmitry@dmitry-VirtualBox ~/dmd2/src/dmd/test $



--
Dmitry Olshansky


Re: array question

2013-12-08 Thread Ali Çehreli

On 12/08/2013 03:51 AM, seany wrote:

 consider the follwoing:

 import tango.io.Stdout, tango.io.Path, tango.text.Util;
 import std.algorithm, std.string , std.stdio, std.array, std.conv,
 std.regex, std.typecons;

 //i know al imports are not necessary for this example, just ^c^v from
 my actual code

 alias string[] surSegments

There are the missing semicolon above and other problems with the code.

 void makeHashmap(T,R)(T[] plainArr, string hashes, out R[] hashMap)
 {
  //first split the hashes
  string [] hashesArr = std.algorithm.splitter(hashes, ',').array;

  for(int i = 0; i  plainArr.length; i++)
  {
  R hashElement;
  for(int j = 0; j  hashesArr.length; j++)
  {
  hashElement[hashesArr[j]] =  = plainArr[i][j];

I think you wanted to append:

hashElement[hashesArr[j]] ~= plainArr[i][j];

What helped me see what was going on was a bunch of pragma(msg) lines:

pragma(msg, typeof(hashElement));
pragma(msg, typeof(hashesArr[j]));
pragma(msg, typeof(hashElement[hashesArr[j]]));
pragma(msg, typeof(plainArr[i][j]));

Ali



Re: the Result Type

2013-12-08 Thread Ali Çehreli

On 12/08/2013 03:41 AM, seany wrote:

On Sunday, 8 December 2013 at 11:02:40 UTC, Ali Çehreli wrote:

(I remember Andrei's original newsgroup post about this discovery but
I cannot find it at this time.)


http://www.digitalmars.com/d/archives/digitalmars/D/announce/Voldemort_Types_in_D_23511.html


this?


There was an earlier newsgroup post like look what I've just discovered.

Ali



Re: Unresolvable references to dlopen, dlclose etc

2013-12-08 Thread Mike Parker

On 12/8/2013 6:12 AM, Mafi wrote:

On Friday, 6 December 2013 at 16:54:14 UTC, Jacob Carlborg wrote:




I would say that linking order shouldn't matter. But for some reason
it does. This not really my area of expertise but I know that others
have had the same problem. You can try and search the newsgroups for
linking order related problems.


So after some tweaking I made it work. Specfying the libpath with -L-L
and the actual -L-l invokes gcc correctly. But the other behavior is
still odd for me. I mean invoking dmd with

dmd myprog.d /path/to/libSomething.a -L[Linkerflags]

links with

gcc myprog.o [Linkerflags] /path/to/libSomething.a [Phobos]

you must

dmd myprog.d -L-L/path/to/ -l-lSomething

and hope the file name has the format libName.a .

But why? Both are objects. Why seperate them? For me there is no reason
to ever want this order!


This is how the gcc linker works. It always causes confusion when people 
first encounter it. And even for people who have experience with it, 
there are corner cases where it's not always obvious that link order was 
the cause of an error. Google for gcc link order and you'll likely 
find some useful reading somewhere.


Re: array question

2013-12-08 Thread seany

On Sunday, 8 December 2013 at 13:47:43 UTC, Ali Çehreli wrote:

On 12/08/2013 03:51 AM, seany wrote:

 consider the follwoing:

 import tango.io.Stdout, tango.io.Path, tango.text.Util;
 import std.algorithm, std.string , std.stdio, std.array,
std.conv,
 std.regex, std.typecons;

 //i know al imports are not necessary for this example, just
^c^v from
 my actual code

 alias string[] surSegments

There are the missing semicolon above and other problems with 
the code.


 void makeHashmap(T,R)(T[] plainArr, string hashes, out R[]
hashMap)
 {
  //first split the hashes
  string [] hashesArr = std.algorithm.splitter(hashes,
',').array;

  for(int i = 0; i  plainArr.length; i++)
  {
  R hashElement;
  for(int j = 0; j  hashesArr.length; j++)
  {
  hashElement[hashesArr[j]] =  = plainArr[i][j];

I think you wanted to append:

hashElement[hashesArr[j]] ~= plainArr[i][j];

What helped me see what was going on was a bunch of pragma(msg) 
lines:


pragma(msg, typeof(hashElement));
pragma(msg, typeof(hashesArr[j]));
pragma(msg, typeof(hashElement[hashesArr[j]]));
pragma(msg, typeof(plainArr[i][j]));

Ali



no i wanted to set hashElement[hashesArr[j]] =  plainArr[i][j];
I realise that it had become string[][string][]

sorry my bad


Re: the Result Type

2013-12-08 Thread Philippe Sigaud
On Sun, Dec 8, 2013 at 9:59 AM, seany se...@uni-bonn.de wrote:
 O_O

 with that knowledge, would also be possible to define new types (not
 aliases, but new encapsulated types) representing things such as Graph,
 Ring, Topology, surreal number, etc?

Other posters already answered your questions concerning ranges.
In short, you can define internal structs (or classes, or functions)
inside a function, another struct, a class, and return them. They can
be parameterized on the host type template parameters, if any.

All the different types in std.algorithm and std.range have *nothing*
in common (no common base class, nor are they part of a common
datatype), *except* they all respect the range 'concept'. As long as
something (class or struct) has the three basic range methods (empty /
front / popFront), it can be used with other functions in
std.algorithm and std.range.

Returning to you question, now. You can do exactly with other types
what was done with ranges in the standard library. As long as you
define the basic properties of a Graph / Ring / Whatever, and
associate it with a guard template (see std.range.isInputRange), any
type can be used as a Graph.

Maybe you could give us more details on what you want to realize exactly?


Re: File Picker

2013-12-08 Thread Malkierian

On Sunday, 8 December 2013 at 09:17:37 UTC, Marco Leise wrote:


Maybe it requires a working Windows® event loop in your
application, as is typical for GUI applications on any
platform. Windows typically generate all sorts of events, like
mouse clicks, key strokes, resize events etc. They add up in
the event queue and a white window like yours is typical of
Windows® to indicate that this application is no longer
working off its event loop. (Or in your case never started to
do so.)
If that is indeed the problem, worry not, because most events
can be handled by the default handler, but you'll need to
write an simple event loop. I'm not sure, but it could be that
you'll need to create a dummy window as well since event loops
work with window handles. Maybe it is ok to pass 0 everywhere,
maybe you need a valid handle.


That's rather unfortunate, as I was using DSFML as my main event 
generator and handler.  I had a DSFML window up already that I 
was calling that function from.  I don't know how to make the 
crossover between the two in D.  I think I'll have to look into 
GtkD instead.  Thanks for the idea, though.


Re: Shared library: loading doesn't call shared static this

2013-12-08 Thread Ellery Newcomer

On Sunday, 8 December 2013 at 10:31:32 UTC, Mathias LANG wrote:


Thank you, and yazd, it did the trick.
May I ask why I don't want to call it multiple time though ?
From the sentence If the runtime was already successfully 
initialized this returns true., I though this was handled in 
some way. Or do you mean, multiple time in case of multiple 
libraries ?


rt_term at least will [did] segfault when you call it a second 
time. So just don't have N shared libs each with a ctor calling 
rt_init and dtor calling rt_term.


Only const or immutable class thread local variables are allowed

2013-12-08 Thread Joseph Rushton Wakeling

Hello all,

I have a challenge, which is this: I'd like to have a public property which will 
return a reference to an internally stored class instance.


ref MyClass myProperty() @property
{
...
}

However, this runs into a problem: I can't use static to internally store a 
class instance.


I worked out a cheaty way to get around this which goes like this:

ref MyClass myProperty() @property
{
struct MyCheat
{
MyClass whatIReallyWant = new MyClass;
}

static MyCheat cheat;
// ... do other stuff ...
return cheat.whatIReallyWant;
}

... but I'm wondering if anyone has any alternative suggestions (or warnings or 
caveats about the cheaty method).


Thanks  best wishes,

-- Joe


Re: how to compose delegate type

2013-12-08 Thread Ellery Newcomer

On Sunday, 8 December 2013 at 00:43:51 UTC, Jesse Phillips wrote:


What is wrong with the current template which returns an 
immutable delegate type? It still store you're immutable member 
function.


It composes the wrong type. It composes a type that has different 
constness than the target type, which will likely cause problems 
due to transitive const.


Anyways, I'm trying to find a workaround to

https://d.puremagic.com/issues/show_bug.cgi?id=11694



Re: File Picker

2013-12-08 Thread Adam D. Ruppe

On Sunday, 8 December 2013 at 04:49:35 UTC, Malkierian wrote:
Any idea why it doesn't work the first time, but then does the 
second and freezes?


Could be a missing argument to the function, I did a quick test 
on Windows XP and it looks like you're on Vista.


Later today, I'll be on my other Windows computer and I'll try it 
there and see what's going on.


Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
I see comments about enums being somehow implemented as tuples, 
and comments about tuples somehow being implemented as structs, 
but I couldn't find examples of static initialization of arrays 
of either.


Finally after playing around with it for a while, it appears this 
example below works for static array of struct initialization.  
It also doesn't display the  enum bug of hearts2 coming back as 
hearts in the iteration.


I tried C static array of struct initialization syntax, and it 
didn't work, which kind of surprises me.


module main;

import std.stdio;

void main()
{
struct Suit {string nm; int val;};

static Suit[5] suits =  [
Suit(spades,1),
Suit(hearts,4),
Suit(hearts2,4),
Suit(diamonds,10),
Suit(clubs,11)
];


foreach (member;  suits)
{
writefln(%s: %d, member.nm, member.val);
}

}

D:\dprojects\ConsoleApp1\ConsoleApp1dmd -run main.d
spades: 1
hearts: 4
hearts2: 4
diamonds: 10
clubs: 11


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

Jay Norwood:


I see comments about enums being somehow implemented as tuples,


Enums are usually implemented as ints, unless you specify a 
different type.




and comments about tuples somehow being implemented as structs,


Phobos Tuples are implemented with structs.


but I couldn't find examples of static initialization of arrays 
of either.


Here it is:


import std.typecons;

enum Foo { A, B, C }
Foo[] a1 = [Foo.A, Foo.B];

alias T = Tuple!(int, int);
T[] a1 = [T(1, 2), T(3, 4)];

void main() {}



It also doesn't display the  enum bug of hearts2 coming back as 
hearts in the iteration.


Because those are different strings.


I tried C static array of struct initialization syntax, and it 
didn't work, which kind of surprises me.


Here it is:


void main() {
static struct Suit { string nm; int val; }

static Suit[5] suits =  [
{spades, 1},
{hearts, 4}];
}


Bye,
bearophile


Re: Only const or immutable class thread local variables are allowed

2013-12-08 Thread Ali Çehreli

On 12/08/2013 10:00 AM, Joseph Rushton Wakeling wrote:

 I have a challenge, which is this: I'd like to have a public property
 which will return a reference to an internally stored class instance.

  ref MyClass myProperty() @property
  {
  ...
  }

First, the usual question: Since class varibles are already references, 
do you really need to return ref?


In any case, I think class static this is the solution:

class MyClass
{
static MyClass whatIReallyWant;

static this()
{
whatIReallyWant = new MyClass;
}

/* ref */ MyClass myProperty() @property
{
return whatIReallyWant;
}
}

void main()
{
auto m = new MyClass;
auto s = m.myProperty;
}

Ali



Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood

Yes, thanks, that syntax does work for the initialization.

The C syntax that failed for me was using the curly brace form 
shown in the following link.


http://www.c4learn.com/c-programming/c-initializing-array-of-structure/

Also, I think I was trying forms of defining the struct and 
initializing the array in the same line... something like this C:
static struct Suit{ int i; long lv;} suits[3] = {{1, 2L},{2, 
4L},{3,9L}};


It looks to me like D requires a named struct definition in a 
separate line from the array  definition.  If that is so, then 
the C initialization of an array with an unnamed struct type, 
like this, would require a struct type name.


static struct { int i; long lv;} suits[3] = {{1, 2L},{2, 
4L},{3,9L}};


So, from your static intialization example, this works.

Also, the conversion of struct to tuple makes the writefln 
tupleof conversion on the struct a little cleaner, since you only 
have to specify the single tuple parameter.



module main;

import std.stdio;

void main()
{
struct Suit {string nm; int val; int val2; string shortNm;};

static Suit[5] suits =  [
{spades,1,6,spd},
{hearts,4,10,hrt},
{hearts2,4,10,hrt2},
{diamonds,10,16,dmd},
{clubs,11,17,clb}
];

foreach (member;  suits)
{
auto tup = member.tupleof;
writefln(%s %d %d %s, tup);
}

}

prints
spades 1 6 spd
hearts 4 10 hrt
hearts2 4 10 hrt2
diamonds 10 16 dmd
clubs 11 17 clb

I also tried using writefln(tup) and writeln(tup) in the example 
above.  The output from writeln(tup) looks like it is headed in 
the right direction.  Maybe a writecsv(tup) would be useful.


spades16spd
hearts410hrt
hearts2410hrt2
diamonds1016dmd
clubs1117clb


Re: Only const or immutable class thread local variables are allowed

2013-12-08 Thread Joseph Rushton Wakeling

On 08/12/13 21:12, Ali Çehreli wrote:

In any case, I think class static this is the solution:


I think I may have misled you by talking about properties, because I _don't_ 
mean a property of a class.  I mean a public standalone function that is marked 
as a @property, which returns a persistent instance of some class.


The actual motivation is reimplementing std.random.rndGen but with class-based 
RNGs instead of structs :-)


A consequence of this is that I don't think a static class instance can work, 
because the returned class has to be non-const -- it's an RNG that will be updated!


Re: File Picker

2013-12-08 Thread Adam D. Ruppe
Hmm, I just tried from my Windows 7 computer and it worked.  If 
you do my sample program without changes


http://arsdnet.net/dcode/open.d

does it work, or is the problem after copy/pasting it into the 
rest of your program? Also are you compiling 64 bit? I only tried 
32 bit since my laptop has a 32 bit processor so that could be a 
problem too.


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

Jay Norwood:

If that is so, then the C initialization of an array with an 
unnamed struct type, like this, would require a struct type 
name.


static struct { int i; long lv;} suits[3] = {{1, 2L},{2, 
4L},{3,9L}};


Giving a struct a name is often a good idea. But if you don't 
want to name it, then you can use a Phobos Tuple. You can even 
omit its field names if you want.




struct Suit {string nm; int val; int val2; string shortNm;};


Better:

static struct Suit {string nm; int val; int val2; string 
shortNm;}


Generally inside functions it's better to define static struct. 
And struct definitions don't need a trailing semicolon.




foreach (member;  suits)


Generally it's better to attach an 'immutable' there, this is not 
always possible, but it's a good habit:


  foreach (immutable member;  suits)


I also tried using writefln(tup) and writeln(tup) in the 
example above.  The output from writeln(tup) looks like it is 
headed in the right direction.  Maybe a writecsv(tup) would be 
useful.


Try:

member.writeln;

Bye,
bearophile


Re: Only const or immutable class thread local variables are allowed

2013-12-08 Thread qznc
On Sunday, 8 December 2013 at 21:32:35 UTC, Joseph Rushton 
Wakeling wrote:

On 08/12/13 21:12, Ali Çehreli wrote:

In any case, I think class static this is the solution:


I think I may have misled you by talking about properties, 
because I _don't_ mean a property of a class.  I mean a public 
standalone function that is marked as a @property, which 
returns a persistent instance of some class.


The actual motivation is reimplementing std.random.rndGen but 
with class-based RNGs instead of structs :-)


A consequence of this is that I don't think a static class 
instance can work, because the returned class has to be 
non-const -- it's an RNG that will be updated!


I understand you are talking about the Singleton design pattern.
You might want to look how std.parallelism does it with the 
default global thread pool.


https://github.com/D-Programming-Language/phobos/blob/master/std/parallelism.d#L3261


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

  foreach (immutable member;  suits)


Sometimes you have to use:

  foreach (const member;  suits)

Bye,
bearophile


Re: File Picker

2013-12-08 Thread Malkierian

On Sunday, 8 December 2013 at 22:26:11 UTC, Adam D. Ruppe wrote:
Hmm, I just tried from my Windows 7 computer and it worked.  If 
you do my sample program without changes


http://arsdnet.net/dcode/open.d

does it work, or is the problem after copy/pasting it into the 
rest of your program? Also are you compiling 64 bit? I only 
tried 32 bit since my laptop has a 32 bit processor so that 
could be a problem too.


No, I'm not building in 64bit.  However, I just tried it, copy 
paste into my main, no additional libs or includes, only main.d 
compiling.  Still get a window like that first image, and if I 
put another one in after it, it does the same not responding 
issue.


Re: File Picker

2013-12-08 Thread Malkierian

On Sunday, 8 December 2013 at 23:17:46 UTC, Malkierian wrote:

On Sunday, 8 December 2013 at 22:26:11 UTC, Adam D. Ruppe wrote:
Hmm, I just tried from my Windows 7 computer and it worked.  
If you do my sample program without changes


http://arsdnet.net/dcode/open.d

does it work, or is the problem after copy/pasting it into the 
rest of your program? Also are you compiling 64 bit? I only 
tried 32 bit since my laptop has a 32 bit processor so that 
could be a problem too.


No, I'm not building in 64bit.  However, I just tried it, copy 
paste into my main, no additional libs or includes, only main.d 
compiling.  Still get a window like that first image, and if I 
put another one in after it, it does the same not responding 
issue.


I am working in Xamarin, though, if that makes a difference.


Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood

On Sunday, 8 December 2013 at 22:30:25 UTC, bearophile wrote:



Try:

member.writeln;

Bye,
bearophile


yeah, that's pretty nice.

module main;

import std.stdio;

void main()
{
struct Suit {string nm; int val; int val2; string shortNm;};

static Suit[5] suits =  [
{spades,1,6,spd},
{hearts,4,10,hrt},
{hearts2,4,10,hrt2},
{diamonds,10,16,dmd},
{clubs,11,17,clb}
];

foreach (immutable member;  suits)
{
member.writeln();
}

}

output:
immutable(Suit)(spades, 1, 6, spd)
immutable(Suit)(hearts, 4, 10, hrt)
immutable(Suit)(hearts2, 4, 10, hrt2)
immutable(Suit)(diamonds, 10, 16, dmd)
immutable(Suit)(clubs, 11, 17, clb)


Re: File Picker

2013-12-08 Thread Jeremy DeHaan

On Sunday, 8 December 2013 at 23:32:54 UTC, Malkierian wrote:

On Sunday, 8 December 2013 at 23:17:46 UTC, Malkierian wrote:
On Sunday, 8 December 2013 at 22:26:11 UTC, Adam D. Ruppe 
wrote:
Hmm, I just tried from my Windows 7 computer and it worked.  
If you do my sample program without changes


http://arsdnet.net/dcode/open.d

does it work, or is the problem after copy/pasting it into 
the rest of your program? Also are you compiling 64 bit? I 
only tried 32 bit since my laptop has a 32 bit processor so 
that could be a problem too.


No, I'm not building in 64bit.  However, I just tried it, copy 
paste into my main, no additional libs or includes, only 
main.d compiling.  Still get a window like that first image, 
and if I put another one in after it, it does the same not 
responding issue.


I am working in Xamarin, though, if that makes a difference.


I just tested it myself building in Xamarin. Worked like it is 
supposed to, so I'm not sure what's up. I'm running Win7 though.


When I get home I can see if running this along side DSFML's 
event stuff is a problem, but I hope it isn't something specific 
to your computer. That would blow.


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

   static Suit[5] suits =  [
{spades,1,6,spd},
{hearts,4,10,hrt},
{hearts2,4,10,hrt2},
{diamonds,10,16,dmd},
{clubs,11,17,clb}


Also, in D it's better to put a space after every comma, to 
increase readability a little:


static immutable Suit[5] suits =  [
{spades, 1, 6, spd},

Bye,
bearophile


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

Jay Norwood:


struct Suit {string nm; int val; int val2; string shortNm;};


You have missed my suggestions above regarding the struct :-)

Look at this:


void main() {
int x;
struct Foo1 {
int bar1() { return x; }
}
pragma(msg, Foo1.sizeof);
static struct Foo2 {
// this gives an error
int bar2() { return x; }
}
pragma(msg, Foo2.sizeof);
}


bar2() gives an error because it can't access x. If you comment 
out the bar2 line, the output is:


4u
1u

Usually you don't want your struct defined inside a function to 
contain a pointer to the enclosing function.






static Suit[5] suits =  [
{spades,1,6,spd},
{hearts,4,10,hrt},
{hearts2,4,10,hrt2},
{diamonds,10,16,dmd},
{clubs,11,17,clb}
];


Unless you have to mutate the contents of a variable, like your 
suits, define it const or immutable:


  static immutable Suit[5] suits =  [
   {spades,1,6,spd},
  ...
 ];

Generally in D add const/immutable to all variables that you 
don't need to mutate, if/where you can.


Bye,
bearophile


Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
It looks like the writeln() does a pretty good job, even for enum 
names.


I also saw a prettyprint example that prints the structure member 
name, and compared its output.

http://forum.dlang.org/thread/ip23ld$93u$1...@digitalmars.com


module main;

import std.stdio;
import std.traits;

void main()
{
enum Suit { spades, hearts=4, diamonds=10, clubs }
enum SuitShort { spd, hrt=4, dmd=10, clb }

static struct Suits { Suit nm; int val; int val2;  SuitShort 
shortNm;}


static Suits[] suits =  [
{Suit.spades, 1, 6, SuitShort.spd},
{Suit.hearts, 4, 10, SuitShort.hrt},
{Suit.diamonds, 4, 10, SuitShort.dmd},
{Suit.clubs, 10, 16, SuitShort.clb}
];

foreach (immutable member;  suits)
{
auto fields = __traits(allMembers, typeof(member));
auto values = member.tupleof;

foreach (index, value; values)
{
writef(%s=%s, , fields[index], value);
}
writeln();
member.writeln();
}
}


output:
nm=spades, val=1, val2=6, shortNm=spd,
immutable(Suits)(spades, 1, 6, spd)
nm=hearts, val=4, val2=10, shortNm=hrt,
immutable(Suits)(hearts, 4, 10, hrt)
nm=diamonds, val=4, val2=10, shortNm=dmd,
immutable(Suits)(diamonds, 4, 10, dmd)
nm=clubs, val=10, val2=16, shortNm=clb,
immutable(Suits)(clubs, 10, 16, clb)


Re: iterate over enum name:value pairs

2013-12-08 Thread bearophile

Jay Norwood:

Using enums, despite their problems, if often better than strings.



static Suits[] suits =  [
{Suit.spades, 1, 6, SuitShort.spd},
{Suit.hearts, 4, 10, SuitShort.hrt},
{Suit.diamonds, 4, 10, SuitShort.dmd},
{Suit.clubs, 10, 16, SuitShort.clb}
];

foreach (immutable member;  suits)



In some cases you can also use with() to avoid struct/enum name 
repetitions (unfortunately it creates a scope, so if you define 
an immutable variable inside it, it will be invisible and 
destroyed once the with() scope ends. This reduces the usefulness 
of with()).



with (Suit) with (SuitShort)
{
static Suits[] suits = [
{spades,1,  6, spd},
{hearts,4, 10, hrt},
{diamonds,  4, 10, dmd},
{clubs,10, 16, clb}
];

foreach (immutable member;  suits)
...


Bye,
bearophile


regarding Latin1 to UTF8 encoding

2013-12-08 Thread Hugo Florentino

Hi,

I am having some problems trygin to pass regular expressions to a 
webpage encoded in Latin1. I have unsuccessfully tried to convert it to 
UTF8 before passing the regular expression.


Initially I tried to do something like this:

auto input = readText(myfile.htm);
auto output = replace(input, re1, re2);

But I got this error when trying to run the application:
std.utf.UTFException@C:\DMD2\Windows\bin\..\..\src\phobos\std\utf.d(1113): 
Invalid UTF-8 sequence (at index 1)


I then tried this, but the error remains

auto input = readText(myfile.htm);
string buffer;
transcode(input, buffer);
auto output = replace(buffer, re1, re2);

Also, this did not work:

auto input = cast(string) read(myfile.htm);
string buffer;
transcode(input, buffer);
auto output = replace(buffer, re1, re2);

core.exception.AssertError@std.encoding(1995): Assertion failure

Please, any help would be appreciated.

Regards, Hugo


Re: regarding Latin1 to UTF8 encoding

2013-12-08 Thread Adam D. Ruppe

On Monday, 9 December 2013 at 02:40:29 UTC, Hugo Florentino wrote:

auto input = readText(myfile.htm);


Don't use readText if it isn't utf-8; readtext assumes it is utf 
8.


I've never actually used std.encoding (I wrote my own encoding 
module for my dom.d, which I used for website scraping too) but I 
think this is what you want:


Latin1String input = cast(Latin1String) 
std.file.read(myfile.htm);

string buffer;
transcode(input, buffer);
auto output = replace(buffer, re1, re2);


see if that works


Re: File Picker

2013-12-08 Thread Adam D. Ruppe

I wonder if your other DSFML thingy use threads or something...

the GetOpenFileName function blocks until the user makes their 
choice. So that could be stalling the SFML event loop, or maybe 
having threading issues. I don't know. But I get the feeling that 
the problem is one of these two things.


Modify const reference data

2013-12-08 Thread Heinz

[DMD 2.064.2]

Hello,

I've been strugling with a solution before bothering you guys 
here (again). I have my own complex code but i made a VERY simple 
test case to reproduce my problem, here's the code:


private import std.stdio;

class A
{
private const ubyte* data;
private ubyte[] abc;

this()
{
abc = [1,2,3,4];
data = cast(const ubyte*)abc.ptr;
}

public void print()
{
for(size_t i = 0; i  4; i++)
{
writefln(%d, data[i]);
}
}
}

class B
{
private const ubyte* data;
private ubyte[] abc;

this()
{
data = cast(const ubyte*)abc.ptr;
}

public void foo()
{
abc = [1,2,3,4];
}

public void print()
{
for(size_t i = 0; i  4; i++)
{
writefln(%d, data[i]);
}
}
}

void main()
{
A a = new A();
a.print(); // OK.

B b = new B();
b.print(); // Crash.
}

The thing is that i can not use the data that const variable 
data in class B is referencing because in my original code i 
get Invalid Memory Operation, in the test case i get access 
violation but it doesn't matter the name of the exception, the 
only thing that matters is that the exception raises for the same 
reason. I understand that i should not modify the const data 
outside a constructor but by stricts reason i can't avoid that.


Do you guys have any other aproach to get away with this? (modify 
a const reference data and then manipulate that data). THANK YOU 
very much in advance. D2 learner by the way.


Re: Modify const reference data

2013-12-08 Thread Heinz
Duhhh! i got the pink avatar by default. That sucks. Pink is just 
not my color.


Re: regarding Latin1 to UTF8 encoding

2013-12-08 Thread Hugo Florentino

On Mon, 09 Dec 2013 03:44:19 +0100, Adam D. Ruppe wrote:

On Monday, 9 December 2013 at 02:40:29 UTC, Hugo Florentino wrote:

auto input = readText(myfile.htm);


Don't use readText if it isn't utf-8; readtext assumes it is utf 8.

I've never actually used std.encoding (I wrote my own encoding module
for my dom.d, which I used for website scraping too) but I think this
is what you want:

Latin1String input = cast(Latin1String) std.file.read(myfile.htm);
string buffer;
transcode(input, buffer);
auto output = replace(buffer, re1, re2);


see if that works


Actually, it did work, even keeping input type as auto.
It seems the explicit typecast to Lating1String was the required 
element for it to work, which makes sense now that I think about it.


Thanks a lot for the (amazingly quick) reply ;)

Now, if I may add a closely related doubt:

Suppose myfile.txt was given to me daily by careless people who 
usually save it as Latin1 but from time to time might save it as UTF8.
Is there a way to detect the encoding prior to typecasting/loading the 
file?


Regards, Hugo


Re: regarding Latin1 to UTF8 encoding

2013-12-08 Thread Adam D. Ruppe

On Monday, 9 December 2013 at 03:07:58 UTC, Hugo Florentino wrote:
Is there a way to detect the encoding prior to 
typecasting/loading the file?


UTF-8 can be detected fairly reliably, but not much luck for 
other encodings. A Windows-1258 and a Latin1 file, for example, 
are usually fairly indistinguishable from a binary perspective - 
they use the same numbers, just for different things.


(It is possible to distinguish them if you use some context and 
grammar check kind of things, but that's not easy.)



But utf-8 has a neat feature: any non-ascii stuff needs to 
validate, and it is unlikely that random data would correctly 
validate.


std.utf.validate can do that (though it throws an exception if it 
fails, ugh!)


So here's how I did it in my own characterencodings.d:

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/characterencodings.d#L138


string utf8string;
import std.utf;
try {
validate!string(cast(string) rawdata);
// validation passed, assume it is UTF-8 and use 
it

utf8string = cast(string) rawdata;
} catch(UTFException t) {
   // not utf-8, try latin1
   transcode(cast(Latin1String) rawData, utf8string);
}

// now go ahead and use utf8 string, it should be set


Re: Modify const reference data

2013-12-08 Thread Adam D. Ruppe

Easy problem in class B: data is null!

On Monday, 9 December 2013 at 02:53:01 UTC, Heinz wrote:

class B
{
private const ubyte* data;
private ubyte[] abc;

this()
{
data = cast(const ubyte*)abc.ptr;
}



Since abc isn't initialized in this constructor, abc.ptr is null. 
So data is null too.




public void print()
{
for(size_t i = 0; i  4; i++)
{
writefln(%d, data[i]);
}
}
}



And since data is null, data[i] will be a memory 
err/segfault/access violation/whatever it is called.


Re: regarding Latin1 to UTF8 encoding

2013-12-08 Thread Hugo Florentino

On Mon, 09 Dec 2013 04:19:51 +0100, Adam D. Ruppe wrote:

On Monday, 9 December 2013 at 03:07:58 UTC, Hugo Florentino wrote:
Is there a way to detect the encoding prior to typecasting/loading 
the file?


UTF-8 can be detected fairly reliably, but not much luck for other
encodings. A Windows-1258 and a Latin1 file, for example, are usually
fairly indistinguishable from a binary perspective - they use the 
same

numbers, just for different things.

(It is possible to distinguish them if you use some context and
grammar check kind of things, but that's not easy.)


But utf-8 has a neat feature: any non-ascii stuff needs to validate,
and it is unlikely that random data would correctly validate.

std.utf.validate can do that (though it throws an exception if it
fails, ugh!)

So here's how I did it in my own characterencodings.d:


https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/characterencodings.d#L138


string utf8string;
import std.utf;
try {
validate!string(cast(string) rawdata);
// validation passed, assume it is UTF-8 and use it
utf8string = cast(string) rawdata;
} catch(UTFException t) {
   // not utf-8, try latin1
   transcode(cast(Latin1String) rawData, utf8string);
}

// now go ahead and use utf8 string, it should be set


Clever solution, thanks.
Coud this work using scope instead of try/catch?

P.S. Nice unit, by the way.


Re: regarding Latin1 to UTF8 encoding

2013-12-08 Thread Adam D. Ruppe

On Monday, 9 December 2013 at 03:33:46 UTC, Hugo Florentino wrote:

Coud this work using scope instead of try/catch?


Maybe, but I don't think it would be very pretty. Really, I think 
validate should return a bool instead of throwing, but since it 
doesn't the try/catch is as close as it gets.



P.S. Nice unit, by the way.


BTW if you need to parse random html, grab that file and my dom.d 
from the same repo.


auto document = new Document();
document.parseGarbage(whatever_data);

parseGarbage tries to determine the character encoding 
automatically, from the validate check or the meta tags in the 
HTML if they are there, then guessing if not. It is pretty good 
at parsing broken html tag soup to make a dom similar to the 
browser.


Then you can get data out of it doing things like

auto firstParagraph = document.querySelector(p:first-child);
if(firstParagraph is null) writeln(no first child paragraph);
else writeln(first child paragraph text: , 
firstParagraph.innerText);


and stuff like that, if you have used Javascript before dom.d 
should look fairly familiar.


Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood

Thanks.  That's looking pretty clean.

I had already tried the shorter enum names without using the with 
statement, and it failed to compile.  I thought it might work 
since the struct definition already specifies the enum type for 
the two members.





with (Suit) with (SuitShort)
{
static Suits[] suits = [
{spades,1,  6, spd},
{hearts,4, 10, hrt},
{diamonds,  4, 10, dmd},
{clubs,10, 16, clb}
];

foreach (immutable member;  suits)
...


Bye,
bearophile




Re: File Picker

2013-12-08 Thread Malkierian

On Monday, 9 December 2013 at 02:45:48 UTC, Adam D. Ruppe wrote:

I wonder if your other DSFML thingy use threads or something...

the GetOpenFileName function blocks until the user makes their 
choice. So that could be stalling the SFML event loop, or maybe 
having threading issues. I don't know. But I get the feeling 
that the problem is one of these two things.


Yes, but like I said, I took DSFML completely out of the project 
to do a test compile using only main.d and not linking any 
external libs (DSFML, GtkD, or DerelictAL). No special build 
instructions or anything in Xamarin, but the first time it's 
called, I get that first picture and the second time (after 
clicking cancel on the first one it's opened again in the code), 
it shows the proper file structure and freezes.  In my regular 
setup (not the main.d exclusive test), my program would continue 
to work just fine until I called the file chooser a second time.


Re: iterate over enum name:value pairs

2013-12-08 Thread Jay Norwood
I notice that if Suit and SuitShort have an enum with the same 
name, then you still have to fully qualify the enum names when 
using the with statement.  So, for example, if spd in SuitShort 
was renamed spades, the first entry in the array initialization 
would have to be {Suit.spades, 1, 6, SuitShort.spades}.



   with (Suit) with (SuitShort)
   {
   static Suits[] suits = [
   {spades,1,  6, spd},
   {hearts,4, 10, hrt},
   {diamonds,  4, 10, dmd},
   {clubs,10, 16, clb}
   ];

   foreach (immutable member;  suits)
   ...


Bye,
bearophile




Re: Modify const reference data

2013-12-08 Thread Ali Çehreli

On 12/08/2013 07:24 PM, Adam D. Ruppe wrote:

Easy problem in class B: data is null!

On Monday, 9 December 2013 at 02:53:01 UTC, Heinz wrote:

class B
{
private const ubyte* data;
private ubyte[] abc;

this()
{
data = cast(const ubyte*)abc.ptr;
}



Since abc isn't initialized in this constructor, abc.ptr is null. So
data is null too.



public void print()
{
for(size_t i = 0; i  4; i++)
{
writefln(%d, data[i]);
}
}
}



And since data is null, data[i] will be a memory err/segfault/access
violation/whatever it is called.


Apparently the OP intended to set it in foo(). If the data is actually 
mutable and there really is no way other than going against the type 
system, foo() must be called at least once and can be implemented like this:


public void foo()
{
abc = [1,2,3,4];
cast(ubyte*)data = abc.ptr;
}

// ...

B b = new B();
b.foo();
b.print(); // now OK

Ali



Compile error using synchronized in ldc (but not dmd)

2013-12-08 Thread Nikhil Padmanabhan

Hello,

The following code snippet fails to compile on ldc2 (0.12.1), but 
successfully compiles on dmd 2.064.2 :


struct Particle {
double x,y,z,w,x2;
this(double[] arr) {
x = arr[0]; y = arr[1]; z=arr[2]; w=arr[3];
x2 = x*x + y*y + z*z;
}
}

synchronized class SyncArray {
private Particle[] buf;

void push(Particle[] arr1) {
buf.length = arr1.length;
buf[] = arr1[];
}

Particle[] pop() {
auto _tmp = new Particle[buf.length];
_tmp[] = buf[];
buf = null;
return _tmp;
}
}

The error message in ldc2 :
test.d(14): Error: cannot implicitly convert expression (arr1[]) 
of type Particle[] to const(shared(Particle)[])
test.d(19): Error: cannot implicitly convert expression 
(this.buf[]) of type shared(Particle)[] to const(Particle[])


What am I doing wrong here?

Thanks in advance!
-- Nikhil

P.S. I've been enjoying coding in D a lot -- many thanks to the 
entire D community!


Re: File Picker

2013-12-08 Thread Malkierian

On Monday, 9 December 2013 at 04:01:54 UTC, Malkierian wrote:

On Monday, 9 December 2013 at 02:45:48 UTC, Adam D. Ruppe wrote:

I wonder if your other DSFML thingy use threads or something...

the GetOpenFileName function blocks until the user makes their 
choice. So that could be stalling the SFML event loop, or 
maybe having threading issues. I don't know. But I get the 
feeling that the problem is one of these two things.


Yes, but like I said, I took DSFML completely out of the 
project to do a test compile using only main.d and not linking 
any external libs (DSFML, GtkD, or DerelictAL). No special 
build instructions or anything in Xamarin, but the first time 
it's called, I get that first picture and the second time 
(after clicking cancel on the first one it's opened again in 
the code), it shows the proper file structure and freezes.  In 
my regular setup (not the main.d exclusive test), my program 
would continue to work just fine until I called the file 
chooser a second time.


What version(s) of dmd are you using?


Re: Compile error using synchronized in ldc (but not dmd)

2013-12-08 Thread Nikhil Padmanabhan
Just a quick follow-up : replacing the array operation by a 
foreach works around this issue, but I don't understand why it 
failed in the first place.



On Monday, 9 December 2013 at 04:26:01 UTC, Nikhil Padmanabhan 
wrote:

Hello,

The following code snippet fails to compile on ldc2 (0.12.1), 
but successfully compiles on dmd 2.064.2 :


struct Particle {
double x,y,z,w,x2;
this(double[] arr) {
x = arr[0]; y = arr[1]; z=arr[2]; w=arr[3];
x2 = x*x + y*y + z*z;
}
}

synchronized class SyncArray {
private Particle[] buf;

void push(Particle[] arr1) {
buf.length = arr1.length;
buf[] = arr1[];
}

Particle[] pop() {
auto _tmp = new Particle[buf.length];
_tmp[] = buf[];
buf = null;
return _tmp;
}
}

The error message in ldc2 :
test.d(14): Error: cannot implicitly convert expression 
(arr1[]) of type Particle[] to const(shared(Particle)[])
test.d(19): Error: cannot implicitly convert expression 
(this.buf[]) of type shared(Particle)[] to const(Particle[])


What am I doing wrong here?

Thanks in advance!
-- Nikhil

P.S. I've been enjoying coding in D a lot -- many thanks to the 
entire D community!




Re: File Picker

2013-12-08 Thread Jeremy DeHaan

On Monday, 9 December 2013 at 04:52:21 UTC, Malkierian wrote:

On Monday, 9 December 2013 at 04:01:54 UTC, Malkierian wrote:
On Monday, 9 December 2013 at 02:45:48 UTC, Adam D. Ruppe 
wrote:
I wonder if your other DSFML thingy use threads or 
something...


the GetOpenFileName function blocks until the user makes 
their choice. So that could be stalling the SFML event loop, 
or maybe having threading issues. I don't know. But I get the 
feeling that the problem is one of these two things.


Yes, but like I said, I took DSFML completely out of the 
project to do a test compile using only main.d and not linking 
any external libs (DSFML, GtkD, or DerelictAL). No special 
build instructions or anything in Xamarin, but the first time 
it's called, I get that first picture and the second time 
(after clicking cancel on the first one it's opened again in 
the code), it shows the proper file structure and freezes.  In 
my regular setup (not the main.d exclusive test), my program 
would continue to work just fine until I called the file 
chooser a second time.


What version(s) of dmd are you using?


I used 2.064.2, and it worked for me. I know you recently 
upgraded though. Maybe you could try uninstalling and then 
reinstalling the compiler?


Re: File Picker

2013-12-08 Thread Malkierian

On Monday, 9 December 2013 at 05:09:42 UTC, Jeremy DeHaan wrote:

On Monday, 9 December 2013 at 04:52:21 UTC, Malkierian wrote:

On Monday, 9 December 2013 at 04:01:54 UTC, Malkierian wrote:
On Monday, 9 December 2013 at 02:45:48 UTC, Adam D. Ruppe 
wrote:
I wonder if your other DSFML thingy use threads or 
something...


the GetOpenFileName function blocks until the user makes 
their choice. So that could be stalling the SFML event loop, 
or maybe having threading issues. I don't know. But I get 
the feeling that the problem is one of these two things.


Yes, but like I said, I took DSFML completely out of the 
project to do a test compile using only main.d and not 
linking any external libs (DSFML, GtkD, or DerelictAL). No 
special build instructions or anything in Xamarin, but the 
first time it's called, I get that first picture and the 
second time (after clicking cancel on the first one it's 
opened again in the code), it shows the proper file structure 
and freezes.  In my regular setup (not the main.d exclusive 
test), my program would continue to work just fine until I 
called the file chooser a second time.


What version(s) of dmd are you using?


I used 2.064.2, and it worked for me. I know you recently 
upgraded though. Maybe you could try uninstalling and then 
reinstalling the compiler?


*sigh* No dice.  And I can't get GtkD to work either, because of 
entry point errors...


Re: Only const or immutable class thread local variables are allowed

2013-12-08 Thread Joseph Rushton Wakeling

On 09/12/13 01:24, Ali Çehreli wrote:

On 12/08/2013 02:40 PM, qznc wrote:


I understand you are talking about the Singleton design pattern.
You might want to look how std.parallelism does it with the default
global thread pool.

https://github.com/D-Programming-Language/phobos/blob/master/std/parallelism.d#L3261


David Simcha presented it as a D-specific pattern and explained how D avoids at
least one of the bugs of double-checked locking:


(i) That's very cool :-D

(ii) I still think it's not what I want.  The static class instance doesn't 
need to be globally global, I want the default thread-local storage as per the 
existing std.random.rndGen.  Hence the solution I arrived at, but which I'm sure 
could be improved.


Simultaneous Assignment

2013-12-08 Thread Dfr

Does D has somtething similar ?

http://code.google.com/p/go-wiki/wiki/SimultaneousAssignment

I tried this way, but it not worked out.

if((int x = 10)  0) {
writefln(x is %s, x);
}