Re: Labels in struct

2015-01-31 Thread zeljkog via Digitalmars-d-learn

On 31.01.15 17:04, tcak wrote:

I do not have a big example in the end to show, but is there any way to
put labels into struct definitions?

struct CommunicationMessage{
 char[2] signature;

mainData:
 int info1;
 int info2;

extraData:
 ushort checksum;

content:
}



Members of type struct are actualy labels.

struct CommunicationMessage{
struct Md {
int info1;
int info2;
};
struct Ed {
ushort checksum;
}
struct Cnt {
}
char[2] signature;
Md mainData;
Ed extraData;
Cnt content;
}



Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread zeljkog via Digitalmars-d-learn

On 31.01.15 15:56, zeljkog wrote:

On 31.01.15 15:45, RuZzz wrote:

Maybe I need good examples on C++, for this class, because a lot of
examples on C++.


Maybe this can help you:
https://github.com/andersonpd/decimal


Also
http://code.dlang.org/packages/eris


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread zeljkog via Digitalmars-d-learn

On 31.01.15 15:45, RuZzz wrote:

Maybe I need good examples on C++, for this class, because a lot of
examples on C++.


Maybe this can help you:
https://github.com/andersonpd/decimal


Error: closures are not yet supported in CTFE

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

I tried:

--
import std.stdio, std.algorithm;

auto unique(){
bool[int] c;
return (int a){
if (a in c)
return false;
else{
c[a] = true;
return true;
}
};
}

void main()
{
[1, 5, 5, 2, 1, 5, 6, 6].filter!(unique()).writeln;
}
--

And got:
Error: closures are not yet supported in CTFE

This works:

void main()
{
auto x = unique();
[1, 5, 5, 2, 1, 5, 6, 6].filter!x.writeln;
}

Is first version near?




Re: Defining a static array with values in a range

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

On 22.01.15 20:05, Meta wrote:

On Thursday, 22 January 2015 at 19:00:47 UTC, zeljkog wrote:

On 22.01.15 19:26, Meta wrote:

On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote:
Whoops, I forgot to make it a template to force CTFE.



You can force CTFE assigning to manifest constant.

enum t = charRange!...


By wrapping it in a template the array will always be generated at
compile time, even if you assign it to a runtime variable.


Yes, but then you can not use runtime spec.


Re: Defining a static array with values in a range

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

On 22.01.15 19:26, Meta wrote:

On Thursday, 22 January 2015 at 18:23:00 UTC, Meta wrote:
Whoops, I forgot to make it a template to force CTFE.



You can force CTFE assigning to manifest constant.

enum t = charRange!...


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread zeljkog via Digitalmars-d-learn

If you wish run this just add:

struct S{
   int n1, n2, n3, n4, n5 ...;
}


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread zeljkog via Digitalmars-d-learn
On 19.12.14 23:56, Ali Çehreli wrote:
> Can we see the test code please.
> 
> Ali

import std.stdio, std.datetime, std.random;

int N = 10_000;
int len = 1000;
int k = 4;

struct S{
   int n1, n2;
//~ this(this){
//~ writeln("this(this)");
//~ }
}

ref T[] append(T, Args...)(ref T[] arr, auto ref Args args)
{
   static if (args.length == 1)
  return arr ~= args[0];
   else{
  arr.length += args.length;
  foreach(i, ref e; args)
 arr[$ - args.length + i] = e;
  return arr;
   }
}

void append1(T)(ref T[] arr, T[] args...)
{
   arr ~= args;
}


void main() {
   S[] arr1 = new S[len];
   S[] arr2, arr3, arr4, arr5;
   foreach (i; 0..len)
  arr1[i] = S(uniform(0, 100), uniform(0, 100));
   auto sw = new StopWatch;

   sw.start();
   foreach(n; 0..N){
  for (int i = 0; i < len; i += k){
 arr2 ~= arr1[i];
 arr2 ~= arr1[i+1];
 arr2 ~= arr1[i+2];
 arr2 ~= arr1[i+3];
//~ arr2 ~= arr1[i+4];
//~ arr2 ~= arr1[i+5];
  }
  delete arr2;
   }
   sw.stop();
   long tm = sw.peek.msecs;
   writeln(tm);

   sw.reset();
   sw.start();
   foreach(n; 0..N){
  for (int i = 0; i < len; i += k){
 arr3 ~= [arr1[i], arr1[i+1], arr1[i+2], arr1[i+3]/+  , arr1[i+4], 
arr1[i+5] +/];
  }
  delete arr3;
   }
   sw.stop();
   tm = sw.peek.msecs;
   writeln(tm);

   sw.reset();
   sw.start();
   foreach(n; 0..N){
  for (int i = 0; i < len; i += k){
 arr4.append(arr1[i], arr1[i+1], arr1[i+2], arr1[i+3]/+   , arr1[i+4], 
arr1[i+5] +/);
  }
  delete arr4;
   }
   sw.stop();
   tm = sw.peek.msecs;
   writeln(tm);

   sw.reset();
   sw.start();
   foreach(n; 0..N){
  for (int i = 0; i < len; i += k){
 arr5.append1(arr1[i], arr1[i+1], arr1[i+2], arr1[i+3]/+   , arr1[i+4], 
arr1[i+5] +/);
  }
  delete arr5;
   }
   sw.stop();
   tm = sw.peek.msecs;
   writeln(tm);
}


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-19 Thread zeljkog via Digitalmars-d-learn

On 19.12.14 16:25, Steven Schveighoffer wrote:

This is surprising to me. I would expect especially ints to be faster.
In reality, there is no difference between arr ~= args and arr.length +=
args.length, and then copying, it's just how you do the copying. Perhaps
it's the call to memcpy in the runtime that is slower than looping.



I think it's compile time looping, it's unrolled.


Did you compile both tests with the same command line parameters?


Yes.


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-18 Thread zeljkog via Digitalmars-d-learn
On 18.12.14 14:50, Steven Schveighoffer wrote:
> I wonder how your code compares to this:
> 
> void append(T)(ref T[] arr, T[] args...)
> {
> arr ~= args;
> }

This is ~20% slower for ints, but it difference  increases for bigger structs.


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-17 Thread zeljkog via Digitalmars-d-learn

On 17.12.14 13:30, Tobias Pankrath wrote:

void append(T, Args...)(ref T[] data, Args args)
{
 static size_t estimateLength(Args args)
 {
 size_t result;
 foreach(e; args)
 static if(hasLength!(typeof(e)))
 result += e.length;
 else
 result += 1;

 return result;
 }

 auto app = appender!(T[])(data);
 app.reserve(data.length + estimateLength(args));

 foreach(e; args)
 app.put(e);
 data = app.data;
}

void main()
{
 import std.stdio;
 int[] data;
 append(data, 1, 2, only(1, 2, 3), iota(4, 9));
 writeln(data);
}

---

Maybe appender.put should get an overload that does the same, though I
didn't had the need for it yet.


It's for convenient one line: arr.append(e1, e2, ...);
I said "something like that" :)


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-17 Thread zeljkog via Digitalmars-d-learn
On 15.12.14 01:00, "Nordlöw" wrote:
> Isn't this algorithm already encoded somewhere in Phobos?

void append(T, Args...)(ref T[] arr, auto ref Args args){
{
   static if (args.length == 1)
  arr ~= args[0]; // inlined
   else{
  arr.length += args.length;
  foreach(i, e; args)
 arr[$ - args.length + i] = e;
   }
}

I've just tested, this looks usable.
Equal for 1 item, considerably (~40%) faster for 2, and much (100+%) faster for 
more.
Also more convenient.

Maybe samthing like that should go to Fobos.


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread zeljkog via Digitalmars-d-learn

On 15.12.14 01:00, "Nordlöw" wrote:

Isn't this algorithm already encoded somewhere in Phobos?

I don't know.


Re: Fastest Way to Append Multiple Elements to an Array

2014-12-14 Thread zeljkog via Digitalmars-d-learn
On 15.12.14 00:16, "Nordlöw" wrote:
> or some other trick?

void append(T, Args...)(ref T[] arr, Args args){
  arr.length += args.length;
  foreach(i, e; args)
arr[$ - args.length + i] = args[i];
}


void main()
{
  int[] arr = [1, 2, 3, 4, 5];
  arr.append(3, 10, 14);
  writeln(arr);
}

output:
[1, 2, 3, 4, 5, 3, 10, 14]


Re: fibers and ranges: what's wrong here?

2014-12-13 Thread zeljkog via Digitalmars-d-learn

On 13.12.14 13:01, zeljkog wrote:


void main() {
   auto tt = Tree(5, [Tree(7,[Tree(11), Tree(4)]), Tree(10)]);
   auto tr1 = TreeRange(tt);
   foreach(v; tr1){
 writef("%2d, ", v);
   }
   writeln();
   for(auto r = TreeRange(tt); !r.empty; r.popFront())
 writef("%2d, ", r.front);
   writeln();
}



Sorry, this works:

void main() {
  auto tt = Tree(5, [Tree(7,[Tree(11), Tree(4)]), Tree(10)]);
  foreach(v; TreeRange(tt)){
writef("%2d, ", v);
  }
  writeln();
  for(auto r = TreeRange(tt); !r.empty; r.popFront())
writef("%2d, ", r.front);
  writeln();
}

I needed this:

struct TreeRange{
  this (this){ throw new Exception("TreeRange is noncopyable!"); }
  ...
}

Or use class :)


fibers and ranges: what's wrong here?

2014-12-13 Thread zeljkog via Digitalmars-d-learn

import std.stdio, core.thread;

struct Tree{
int val;
Tree[] tree;
}

struct TreeRange{
  Tree curtree;
  bool empty;
  Fiber worker;

  this(Tree t){
worker = new Fiber(&fiberFunc);
curtree = t;
popFront();
  }
  void fiberFunc(){
Tree t = curtree;
Fiber.yield();
foreach(child; t.tree){
  curtree = child;
  fiberFunc();
}
  }
  int front(){ return curtree.val; };
  void popFront() {
worker.call();
empty = worker.state == Fiber.State.TERM;
  }
}

void main() {
  auto tt = Tree(5, [Tree(7,[Tree(11), Tree(4)]), Tree(10)]);
  auto tr1 = TreeRange(tt);
  foreach(v; tr1){
writef("%2d, ", v);
  }
  writeln();
  for(auto r = TreeRange(tt); !r.empty; r.popFront())
writef("%2d, ", r.front);
  writeln();
}

output:
 5,  5,  5,  5,  5,
 5,  7, 11,  4, 10,

Is it supposed to work?


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-21 Thread zeljkog

On 21.09.2011 19:12, Christophe Travert wrote:

Nice. It is better with gdc linux 64bits too. I wanted to avoid
conditional expressions like ?: but it's actually slightly faster that
way.


It is not compiled in as conditional jump.


Re: toUTFz and WinAPI GetTextExtentPoint32W

2011-09-21 Thread zeljkog

On 21.09.2011 01:57, Christophe wrote:


size_t myCount(string text)
{
   size_t n = text.length;
   for (uint i=0; i>6;
   n -= (s>>1) - ((s+1)>>2);
 }
   return n;
}



Here is a more readable and a bit faster version on dmd windows:

size_t utfCount(string text)
{
size_t n = 0;
for (uint i=0; i>6)^0b10)? 1: 0;
return n;
}