Re: Efficient sort function allowing own test and swap function as parameter

2020-10-06 Thread Ali Çehreli via Digitalmars-d-learn

On 10/6/20 3:18 PM, Alaindevos wrote:
I have a large table consisting of two columns.One with words.Another 
with frequencies. I want to sort them efficiently according to the names 
or frequency.
For this I need an efficient sort function where I can plugin my proper 
test of order, and proper swap. Currently I do it using an own written 
bubble sort that doesn't scale well.




I had fun writing the following program. Note how makeIndex allows 
visiting elements in sorted order without actually sorting them.


import std.random;
import std.range;
import std.algorithm;
import std.conv;
import std.stdio;

struct S {
  string word;
  size_t frequency;
}

bool byWord(S a, S b) {
  return a.word < b.word;
}

bool byFrequency(S a, S b) {
  return a.frequency < b.frequency;
}

auto dump(R)(string title, R range) {
  writefln!"\n%s:\n%(%s\n%)"(title, range);
}

// A test function that makes an S
S makeS() {
  string makeWord() {
static letters = iota('a', 'z' + 1).map!(to!dchar).array;
return letters.randomSample(4).to!string; // Four-letter words! :p
  }
  size_t makeFrequency() {
return uniform(0, 100);
  }
  return S(makeWord(), makeFrequency());
}

// A test function that makes some S'es
S[] makeSs() {
  return 10.iota.map!(i => makeS()).array;
}

void main() {
  auto ss = makeSs();
  dump("Unsorted", ss);

  auto byWordIndexes = new size_t[ss.length];
  ss.makeIndex!byWord(byWordIndexes);
  dump("Still unsorted but visited by word order",
   byWordIndexes.map!(i => ss[i]));

  auto byFrequencyIndexes = new size_t[ss.length];
  ss.makeIndex!byFrequency(byFrequencyIndexes);
  dump("Still unsorted but visited by frequency order",
   byFrequencyIndexes.map!(i => ss[i]));

  ss.sort!byWord();
  dump("Actually sorted by words", ss);

  ss.sort!byFrequency();
  dump("Actually sorted by frequencies", ss);
}

Sample output:

Unsorted:
S("bfmp", 78)
S("imsx", 17)
S("kmwy", 60)
S("klpw", 92)
S("hnrt", 24)
S("aivz", 29)
S("prst", 24)
S("cdlm", 86)
S("alvz", 13)
S("mnxz", 52)

Still unsorted but visited by word order:
S("aivz", 29)
S("alvz", 13)
S("bfmp", 78)
S("cdlm", 86)
S("hnrt", 24)
S("imsx", 17)
S("klpw", 92)
S("kmwy", 60)
S("mnxz", 52)
S("prst", 24)

Still unsorted but visited by frequency order:
S("alvz", 13)
S("imsx", 17)
S("hnrt", 24)
S("prst", 24)
S("aivz", 29)
S("mnxz", 52)
S("kmwy", 60)
S("bfmp", 78)
S("cdlm", 86)
S("klpw", 92)

Actually sorted by words:
S("aivz", 29)
S("alvz", 13)
S("bfmp", 78)
S("cdlm", 86)
S("hnrt", 24)
S("imsx", 17)
S("klpw", 92)
S("kmwy", 60)
S("mnxz", 52)
S("prst", 24)

Actually sorted by frequencies:
S("alvz", 13)
S("imsx", 17)
S("hnrt", 24)
S("prst", 24)
S("aivz", 29)
S("mnxz", 52)
S("kmwy", 60)
S("bfmp", 78)
S("cdlm", 86)
S("klpw", 92)

Ali


Re: Efficient sort function allowing own test and swap function as parameter

2020-10-06 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Oct 06, 2020 at 10:18:39PM +, Alaindevos via Digitalmars-d-learn 
wrote:
> I have a large table consisting of two columns.One with words.Another
> with frequencies. I want to sort them efficiently according to the
> names or frequency.
> For this I need an efficient sort function where I can plugin my
> proper test of order, and proper swap. Currently I do it using an own
> written bubble sort that doesn't scale well.

Why don't you use std.algorithm.sort? That's what the standard library
is for.


T

-- 
Being able to learn is a great learning; being able to unlearn is a greater 
learning.


Efficient sort function allowing own test and swap function as parameter

2020-10-06 Thread Alaindevos via Digitalmars-d-learn
I have a large table consisting of two columns.One with 
words.Another with frequencies. I want to sort them efficiently 
according to the names or frequency.
For this I need an efficient sort function where I can plugin my 
proper test of order, and proper swap. Currently I do it using an 
own written bubble sort that doesn't scale well.




Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Johan via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:

There are two subtractions possible.
A machine-one which can be architecture dependent, does not 
have the same results on all computers, and behaves like a 
modulus in mathematics.

A logical one. For the last one higher classes might be needed.


The (signed and unsigned) integer wrap around is well-defined in 
D, and is architecture independent. That means for example that 
if a specific architecture does not have a subtract instruction 
that wraps around according to what D specifies, then the 
compiler will generate the necessary instructions such that still 
int.min - 1 == int.max. That's similar to how integer 
multiplication 'just works' on architectures that do not have a 
multiply instruction.


-Johan



Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread ikod via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 18:24:14 UTC, Alaindevos wrote:

There are two subtractions possible.
A machine-one which can be architecture dependent, does not 
have the same results on all computers, and behaves like a 
modulus in mathematics.

A logical one. For the last one higher classes might be needed.


Hello,

You may try 
https://dlang.org/phobos/std_experimental_checkedint.html in this 
case.


Regards,


Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Alaindevos via Digitalmars-d-learn

There are two subtractions possible.
A machine-one which can be architecture dependent, does not have 
the same results on all computers, and behaves like a modulus in 
mathematics.

A logical one. For the last one higher classes might be needed.


Re: Link Time Optimization Bitcode File Format

2020-10-06 Thread kinke via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 16:46:28 UTC, Severin Teona wrote:
Also, when I try to link the druntime with the application I 
want to write on the microcontroller, there are some link 
errors due to the file format.


This happens when you link manually, not through LDC. When 
running LDC with `-flto=full -O -v ...`, you'll see that it 
invokes gcc with extra flags, something like


-Wl,-plugin,/dlang/ldc-1.23.0/lib/LLVMgold-ldc.so 
-Wl,-plugin-opt=mcpu=x86-64 -Wl,-plugin-opt=O3 
-Wl,-plugin-opt=-function-sections -Wl,-plugin-opt=-data-sections


so that the linker gets to know how to deal with bitcode objects 
via the LLVM plugin.


See 
http://johanengelen.github.io/ldc/2016/11/10/Link-Time-Optimization-LDC.html for more infos about LTO.


Re: Link Time Optimization Bitcode File Format

2020-10-06 Thread IGotD- via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 16:46:28 UTC, Severin Teona wrote:

Hi all,

I am trying to build the druntime with the 'ldc-build-runtime' 
tool for microcontrollers (using the arm-none-eabi-gcc 
compiler) and therefore the size of the druntime should be as 
little as possible. One solution I had was to use Link Time 
Optimization (LTO) to reduce the size.


The problem now is the fact that when I compile the druntime 
with -flto=full or -flto=thin (as arguments for LDC2), the 
resulted object files (and also a big part of the runtime as it 
is a static library) have a different file format - LLVM IR 
bitcode - than I need, which is ELF 32-bit. Also, when I try to 
link the druntime with the application I want to write on the 
microcontroller, there are some link errors due to the file 
format.
I also tried using a different archiver - llvm-ar - but I had 
no luck.


Could you give me some advice about how should I fix this?

Thank you!


I have run into the same problem when using GNU ld. The problem 
is that my version of GNU ld, version 2.30.0.20180329 (which is 
ancient) cannot deal with the object file format when LTO is 
enabled. One way is to try llvm-ld and see if that one can handle 
it.





Link Time Optimization Bitcode File Format

2020-10-06 Thread Severin Teona via Digitalmars-d-learn

Hi all,

I am trying to build the druntime with the 'ldc-build-runtime' 
tool for microcontrollers (using the arm-none-eabi-gcc compiler) 
and therefore the size of the druntime should be as little as 
possible. One solution I had was to use Link Time Optimization 
(LTO) to reduce the size.


The problem now is the fact that when I compile the druntime with 
-flto=full or -flto=thin (as arguments for LDC2), the resulted 
object files (and also a big part of the runtime as it is a 
static library) have a different file format - LLVM IR bitcode - 
than I need, which is ELF 32-bit. Also, when I try to link the 
druntime with the application I want to write on the 
microcontroller, there are some link errors due to the file 
format.
I also tried using a different archiver - llvm-ar - but I had no 
luck.


Could you give me some advice about how should I fix this?

Thank you!


Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Ali Çehreli via Digitalmars-d-learn

On 10/6/20 5:24 AM, Alaindevos wrote:

Is that the expected behavior of the programmer?
Opinions can differ. Feel free to elaborate.


The following is even more "expected". ;) Subtract zero from -1 and you 
get size_t.max.


void main() {
  int[] arr;
  int i = -1;

  auto u = (i - arr.length);// -1 - 0

  assert(u == size_t.max);   // the surprise
  static assert (is (typeof(u) == size_t));  // the reason
}

Ali


Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 12:24:56 UTC, Alaindevos wrote:

Is that the expected behavior of the programmer?
Opinions can differ. Feel free to elaborate.


It's expected behavior:

"If both operands are of integral types and an overflow or 
underflow occurs in the computation, wrapping will happen. For 
example, uint.max + 1 == uint.min, uint.min - 1 == uint.max, 
int.max + 1 == int.min, and int.min - 1 == int.max."


https://dlang.org/spec/expression.html#add_expressions


Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Imperatorn via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 12:24:56 UTC, Alaindevos wrote:

Is that the expected behavior of the programmer?
Opinions can differ. Feel free to elaborate.


Elaborate please. Are you really asking if one can do subtraction 
in D.


Re: It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Alaindevos via Digitalmars-d-learn

On Tuesday, 6 October 2020 at 12:24:56 UTC, Alaindevos wrote:

Is that the expected behavior of the programmer?
Opinions can differ. Feel free to elaborate.

E.g. length of a string unsigned long.


It is possible to substract 5 from 3 unsigned integer

2020-10-06 Thread Alaindevos via Digitalmars-d-learn

Is that the expected behavior of the programmer?
Opinions can differ. Feel free to elaborate.


Re: vibe.d / experience / feedback

2020-10-06 Thread ddcovery via Digitalmars-d-learn
On Thursday, 1 October 2020 at 06:32:23 UTC, Robert M. Münch 
wrote:
Hi, we are currently using vibe.d for a prototype and I want to 
post some experiences. I know one shouldn't only address the 
problems but provide some solutions.


However, our current use-case is that we want to get a job 
done, and not creating a web-framework.

...
So, overall... compared to other web-frameworks we know, there 
are many pitfalls which just cost a lot of time... We think 
it's a good base, but there needs to be much less magic, much 
better docs, much less complexity.


Hi Robert,

I found myself in a similar situation recently, and I can't help 
but ask you: What technology do you use regularly? What 
drives/draws you to try dlang/vibe.d? Do you have other 
alternatives to dlang/vibe.d for your project?
In my case we usually work in Node+js/ts (previously Scala+Play) 
and I wanted to jump to something really performant for a new 
project without losing code expressiveness and development speed. 
Dlang seemed a good alternative (I like it much more than Go or 
Rust).





Re: vibe.d / experience / feedback

2020-10-06 Thread Arun via Digitalmars-d-learn
On Thursday, 1 October 2020 at 06:32:23 UTC, Robert M. Münch 
wrote:
Hi, we are currently using vibe.d for a prototype and I want to 
post some experiences. I know one shouldn't only address the 
problems but provide some solutions.


However, our current use-case is that we want to get a job 
done, and not creating a web-framework.


1. For many things the docs are missing, or inconsistent, or 
wrong. So, it's pretty hard to make your way through it. Or you 
just start reading the code and reverse engineer vibe. => We 
are waisting a lot of time.


2. There are many inconsistencies / half-done implementations 
in vibe. Again, this makes it hard to find out what works, what 
doesn't or which parts behave differently. => Costs a lot of 
time.


3. Vibe can't handle GET & POST on the same URL... and the bad 
thing is, the later route will just be ignored, so you don't 
get any hint or crash. The docs don't mention this clearly. => 
We waisted a lot of time to find out, that this doesn't work.


4. Vide uses an own JSON type, not the standard one. We don't 
understand why, this just makes things much more complicated 
and one has to mess around with this.


5. One can't access the raw HTTP request body, things must be 
go through Vibe's JSON parser. To get access to the raw body, a 
lot of workarounds are necessary.


So, overall... compared to other web-frameworks we know, there 
are many pitfalls which just cost a lot of time... We think 
it's a good base, but there needs to be much less magic, much 
better docs, much less complexity.


I concur with 1 and 2. We waste lot of time trying to understand 
vibe code instead of working on the business logic.


Never had issues with 3.

I've found vibe JSON to be a relative of nlohmann::json 
https://github.com/nlohmann/json
Similar API, seamless code reusability (copy paste). I never 
really cared about std.json as the usability is not good.


5. is definitely possible with readAll. Never had issues with 
this. Just dig more into the code ☺️