Re: why the sort result is different with C#

2016-01-22 Thread mzfhhhh via Digitalmars-d-learn

i know the reason,C# have several compares methods.
String.Compare , String.CompareOrdinal

//now the output is same as the D code
Array.Sort(arr, string.CompareOrdinal);




why the sort result is different with C#

2016-01-21 Thread mzfhhhh via Digitalmars-d-learn

D code:
auto arr = ["b1=1", "b=2","a1=1", "a=2"];
writeln(arr.sort());

output:["a1=1", "a=2", "b1=1", "b=2"]

C# code:
var arr = new string[]{ "b1=1", "b=2", "a1=1", "a=2" };
Array.Sort(arr);

output:["a=2","a1=1","b=2","b1=1"]


Re: pi program

2015-09-25 Thread mzfhhhh via Digitalmars-d-learn



If we worry about the string form and instead try:

double reduce_string_loop() {
  return reduce!"1.0 / (a * a)"(iota(1, 100));
}

double reduce_function_loop() {
  return reduce!((n) => 1.0 / (n * n))(iota(1, 100));
}



it should be write :
double reduce_loop() {
  //return iota(1, 100).map!"1.0 / (a * a)".sum;
  return iota(1, 100).reduce!"a + 1.0 / (b * b)";
}



Re: Chaining struct method invocations

2015-09-07 Thread mzfhhhh via Digitalmars-d-learn

On Monday, 7 September 2015 at 14:12:25 UTC, Bahman Movaqar wrote:
I need some help understand the behaviour of my code[1].  
Specifically I have trouble with `add` method on line 79.


My impression is that since it returns `this`, multiple 
invocations can be chained like `obj.add(X).add(Y).add(Z)`.  
However the test on line 92 fails and if I do a `writeln`, only 
"p1" and "p2" records show up.


What am I missing here?  Thanks in advance.

[1] 
https://github.com/bahmanm/d-etudes/blob/master/source/e002/models.d


struct is a value type,you can convert to ref type by "ref":

struct Test
{
int a;

Test add1()
{
a++;
return this;
}
ref Test add2()
{
a++;
return this;
}
}

Test t1;
t1.add1.add1;
writeln(t1.a);//1

Test t2;
t2.add2.add2;
writeln(t2.a);//2


Re: What is the D way to map a binary file to a structure?

2015-08-30 Thread mzfhhhh via Digitalmars-d-learn

On Saturday, 29 August 2015 at 12:56:08 UTC, cym13 wrote:

Hi,

Let's say I have a simple binary file whose structure is 
well-known. Here is

an example which stores points:

struct Point {
long x;
long y;
long z;
}

struct BinFile {
uintmagicNumber;  // Some identifier
ulong   pointsNumber;
Point[] points;   // Array of pointsNumber points.
}

What is the best way to read some file and fill a structure 
with it? Would
reading the file into a void[] and then casting it to the 
struct work with

things like internal struct padding?


struct Point {
 long x;
 long y;
 long z;
}

struct BinFile {
uintmagicNumber;  // Some identifier
ulong   pointsNumber;
Point[] points;   // Array of pointsNumber points.

this(ubyte [] buf)
{
auto f = cast(BinFile *)buf.ptr;
this = cast(BinFile)*f;
this.points = 
(cast(Point*)f.points)[0..cast(uint)this.pointsNumber];

}
}




why adding extern(C) cause a runtime error?

2015-07-10 Thread mzfhhhh via Digitalmars-d-learn

win7   x86  dmd2.067.1 ok
ubuntu x64  dmd2.067.1 error
-
import std.stdio;
import std.socket;

extern(C)
void recv()
{
writeln(recv...);
}

extern(C)
void send()
{
writeln(send...);
}


int main(string[] argv)
{
//copy from std.socket unittest

immutable ubyte[] data = [1, 2, 3, 4];
auto pair = socketPair();
scope(exit) foreach (s; pair) s.close();

pair[0].send(data);

auto buf = new ubyte[data.length];
pair[1].receive(buf);
assert(buf == data);

return 0;
}
--
send...
recv...
core.exception.AssertError@a.d(27): Assertion failure

./a() [0x43d61f]
./a(_Dmain+0xcc) [0x43d1bc]
./a(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) 
[0x4400fb]
./a(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2a) 
[0x44004e]
./a(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll()+0x30) [0x4400b4]
./a(void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate())+0x2a) 
[0x44004e]

./a(_d_run_main+0x1dc) [0x43ffc8]
./a(main+0x17) [0x43d637]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) 
[0x7f5fabd8fec5]




build vibe-d-0.7.23 error on win7 x86?

2015-04-20 Thread mzfhhhh via Digitalmars-d-learn

win 7 x86,3GB ram:

1. dmd 2.066 vibe-d-0.7.23, it's ok.

2. dmd 2.067 vibe-d-0.7.23,
   show error msg out of memory

why?


how call a c function with stdcall?

2015-03-24 Thread mzfhhhh via Digitalmars-d-learn

for example:use vc compile on x86
func.c:
__declspec(dllexport) int _stdcall sub(int a,int b)
{
return a-b;
}

func.def:

LIBRARY
EXPORTS
sub

-
i use implib.exe to create a omf format lib.

D code:
import std.exception;

//this is a cdecl call
extern(C) int sub(int a,int b);

void main()
{
auto c1 = sub(2,1);

//when this line run,the c1 value is broken
auto c2 = enforceEx!Exception(sub(1,2));
}
--
my question is how write D code to call the c function


Re: how call a c function with stdcall?

2015-03-24 Thread mzfhhhh via Digitalmars-d-learn

On Tuesday, 24 March 2015 at 15:26:22 UTC, Adam D. Ruppe wrote:

try extern(Windows) isntead of extern(C).


use extern(Windows) or extern(System)

the compile show the link error:
Error 42: Symbol Undefined _sub@8


dll export func name is sub,
and the dll is 3rd party,i can't change the dll code.

i use implib.exe /system func.lib func.dll to create a func.lib.

how can i link correctly?


Re: how call a c function with stdcall?

2015-03-24 Thread mzfhhhh via Digitalmars-d-learn

On Wednesday, 25 March 2015 at 00:26:21 UTC, Adam D. Ruppe wrote:

On Wednesday, 25 March 2015 at 00:09:33 UTC, mzf wrote:
i use implib.exe /system func.lib func.dll to create a 
func.lib.


You might also need to specify the .def file as the final 
argument to that.


http://digitalmars.com/ctg/implib.html

The def file can list aliases for the functions. Maybe try it 
without the /system flag too. I know there's a way but I don't 
know exactly what, I just tend to guess and check a little...


1.def file:
  LIBRARY func

  EXPORTS
  _sub@8 = sub

2.create lib
  implib.exe func.lib func.def

-
now it works fine,thanks!


Re: How to write asia characters on console?

2015-02-08 Thread mzfhhhh via Digitalmars-d-learn


Thanks, my problem has been solved:)
---
import std.stdio;
import core.stdc.wchar_;

extern(C) int setlocale(int, char*);

static this()
{
   fwide(core.stdc.stdio.stdout, 1);
   setlocale(0, cast(char*)china);
}

int main(string[] args)
{
string s1 = hello你好;
writefln(%s, s1);
return 0;
}


This solution has a problem, look at this:
http://forum.dlang.org/thread/jfawjvoedsxsznsue...@forum.dlang.org#post-tyslcbycorgfaqimldnd:40forum.dlang.org


Re: why GC not work?

2015-02-08 Thread mzfhhhh via Digitalmars-d-learn

On Saturday, 7 February 2015 at 06:08:39 UTC, ketmar wrote:

On Sat, 07 Feb 2015 04:30:07 +, Safety0ff wrote:


False pointers, current GC is not precise.


not only that. constantly allocating big chunks of memory will 
inevitably
lead to OOM due to current GC design. you can check it with 
manual
freeing. there were some topics about it, and the solution is 
either use
64 bit OS ('cause the memory is here, but there is no address 
space to

allocate it), or use `malloc()` and `free()` from libc.


hi,i still have two questions:

1. how large is the big chunks  ? 10MB? 50MB? ...
  where are topics about this?
  if I don't know the exact size, it is very likely a memory
leak

2. auto buf = new byte[](1024*1024*100);
  now the gc can't free this buf.
  can i free it by manual?


why GC not work?

2015-02-06 Thread mzfhhhh via Digitalmars-d-learn

import std.stdio;

void testGC()
{
auto b = new byte[](1024*1024*100);
writeln(malloc 100M!);
}

void main()
{
foreach(i;1..100)
{
testGC();
}
}
--
core.exception.OutOfMemoryError@(0)

win7 x86,dmd v2.066.0


how convert the range to slice ?

2015-01-28 Thread mzfhhhh via Digitalmars-d-learn

is there any simple way to convert?

int [] arr1 = [1,2,3].map!a*2;   //compile error
int [] arr2 = [1,2,3].filter!a3;//compile error


Re: why spawn crash?

2015-01-23 Thread mzfhhhh via Digitalmars-d-learn

thanks,you are right.

window console show chinese char is not right,
so i try to add this code:

extern(C) int setlocale(int, char*);
static this()
{
fwide(core.stdc.stdio.stdout, 1);
setlocale(LC_CTYPE, cast(char*)china);
}

it's looks like solve the problem,but caused another problem.

now i use chcp 65001 command to change the code page and change 
the

font to lucida console.it works correctly!


why spawn crash?

2015-01-22 Thread mzfhhhh via Digitalmars-d-learn

i wrote a test code:

void worker(int firstNumber)
{
 Thread.sleep(1.msecs);
}

void main()
{
foreach (i; 1 .. 1000) {
spawn(worker, i );
writeln(i);
}
thread_joinAll();
writeln(ok);
}


sometimes it's ok,sometimes it's crashed ! why ?
here is one of times callstack message:

test.exe!_free()   
test.exe!_fwide()  
test.exe!_fwide()  
test.exe!__ReleaseSemaphore()  
test.exe!_fwide()  

test.exe!main@__modctor() 行 3
 
	test.exe!rt@minfo@__T14runModuleFuncsS482rt5minfo11ModuleGroup11runTlsCtorsMFZ9__lambda1Z@runModuleFuncs()

test.exe!___threadstartex@4()