Re: C Style char**

2019-05-13 Thread Doug Clayton via Digitalmars-d-learn

On Monday, 13 May 2019 at 09:56:19 UTC, evilrat wrote:

On Monday, 13 May 2019 at 09:24:34 UTC, Doug Clayton wrote:

[...]


You don't need to cast it, arrays have .ptr property to get 
pointer to first element, and in @safe you just [0] 
instead, or so I think.


If you are using static/literal arrays and know what you are 
doing this will work.


Otherwise it safer to just use regular string[] array and use 
something like that



const(char*)* toCStringList(string[] arr)
{
import std.string; // toStringz() - adds null 
terminator, allocates using GC if necessary
import std.array; // array() - eagerly converts range 
to array, allocates using GC

import std.algorithm; // map() - apply function to range
return arr
.map!(toStringz)
.array();
}


Thanks so much! That's much more elegant than what I was thinking 
I would have to use.




C Style char**

2019-05-13 Thread Doug Clayton via Digitalmars-d-learn

Hi All,

First time poster :)

I'm working on getting a binding to Vulkan working properly in D, 
but I've run into a roadblock. The good news is that the binding 
seems to work fine, but I'm having an issue getting a parameter 
that needs an input of a const(char*)* to receive all of the 
members of the array. It only will receive the first member.


I've looked through the documentation, but I haven't seen a clear 
way to cast a char*[] to a char** without it losing the rest of 
the members. Can anyone help?


Re: Code Reviewer

2015-08-13 Thread Clayton via Digitalmars-d-learn

On Wednesday, 12 August 2015 at 13:14:04 UTC, sigod wrote:

On Tuesday, 11 August 2015 at 22:50:52 UTC, Clayton wrote:

Hello everyone,

Am looking for someone who could help review my code . As an 
entry exercise to D am converting  3 C implementations of 
popular pattern  matching algorithms. The idea is to have 6 
final implementations ( 3 compile-time and 3 runtime) . I 
think am basically done with the coding, but being a beginner 
myself, I feel I need some do some critics so I can improve 
(especially on the compiletime ones).


I could have uploaded direct but I think the code may be way 
too long.


Help will be dearly appreciated.


Use this site: http://codereview.stackexchange.com/


Thanks sigod, I have also put the compiletime code here : 
http://dpaste.dzfl.pl/f0bec44f859e


Re: Code Reviewer

2015-08-13 Thread Clayton via Digitalmars-d-learn
On Wednesday, 12 August 2015 at 12:14:20 UTC, Rikki Cattermole 
wrote:

On 13/08/2015 12:09 a.m., Clayton wrote:
On Wednesday, 12 August 2015 at 02:49:59 UTC, Rikki Cattermole 
wrote:

On 12/08/2015 10:50 a.m., Clayton wrote:

[...]


Upload to e.g. Github/gist/pastebin.


Hi Rikki, can I have your email so I add you to my repository 
on BitBucket?


No need, as long as it is not propriety code feel free to post 
the links :)

That way anybody who is willing to comment can.



Thanks , I have put the compiletime code here : 
http://dpaste.dzfl.pl/f0bec44f859e


Re: Code Reviewer

2015-08-12 Thread Clayton via Digitalmars-d-learn
On Wednesday, 12 August 2015 at 02:49:59 UTC, Rikki Cattermole 
wrote:

On 12/08/2015 10:50 a.m., Clayton wrote:

Hello everyone,

Am looking for someone who could help review my code . As an 
entry
exercise to D am converting  3 C implementations of popular 
pattern
matching algorithms. The idea is to have 6 final 
implementations ( 3
compile-time and 3 runtime) . I think am basically done with 
the coding,
but being a beginner myself, I feel I need some do some 
critics so I can

improve (especially on the compiletime ones).

I could have uploaded direct but I think the code may be way 
too long.


Help will be dearly appreciated.


Upload to e.g. Github/gist/pastebin.


Hi Rikki, can I have your email so I add you to my repository on 
BitBucket?


Code Reviewer

2015-08-11 Thread Clayton via Digitalmars-d-learn

Hello everyone,

Am looking for someone who could help review my code . As an 
entry exercise to D am converting  3 C implementations of popular 
pattern  matching algorithms. The idea is to have 6 final 
implementations ( 3 compile-time and 3 runtime) . I think am 
basically done with the coding, but being a beginner myself, I 
feel I need some do some critics so I can improve (especially on 
the compiletime ones).


I could have uploaded direct but I think the code may be way too 
long.


Help will be dearly appreciated.



Re: Measuring Execution time

2015-07-23 Thread Clayton via Digitalmars-d-learn

On Wednesday, 22 July 2015 at 09:32:15 UTC, John Colvin wrote:

On Wednesday, 22 July 2015 at 09:23:36 UTC, Clayton wrote:

[...]


The normal way of doing this would be using 
std.datetime.StopWatch:


StopWatch sw;
sw.start();
algorithm();
long exec_ms = sw.peek().msecs;


Am wondering how possible is to restrict that all algorithms get 
run on a specific core( e.g. CPU 0 ) since I wanted my test run 
on the same environment.


Re: Measuring Execution time

2015-07-22 Thread Clayton via Digitalmars-d-learn

On Wednesday, 22 July 2015 at 09:32:15 UTC, John Colvin wrote:

On Wednesday, 22 July 2015 at 09:23:36 UTC, Clayton wrote:

[...]


The normal way of doing this would be using 
std.datetime.StopWatch:


StopWatch sw;
sw.start();
algorithm();
long exec_ms = sw.peek().msecs;


Much appreciated, that works well John . Learning goes on... 
thanks again


Measuring Execution time

2015-07-22 Thread Clayton via Digitalmars-d-learn
How does one represent Duration in only Micro-seconds, or 
milliseconds. Trying to measure the execution time of an 
algorithm and I get 4 ms, 619 μs, and 8 hnsecs , I want to sum 
all these and get total hnsecs or μs .


I would also  appreciate advise on  whether this is the best way 
to measure the execution time of an algorithm.




import std.datetime;
import std.stdio;

void algorithm( ){
writeln(Hello!);
}
void main(){

auto stattime = Clock.currTime();
algorithm( );
endttime = Clock.currTime();

auto duration = endttime - stattime;

writeln(Hello Duration == , duration);

}


monitoring variable evaluation time

2015-07-20 Thread Clayton via Digitalmars-d-learn
What could be the best-tool for monitoring the evaluation time of 
a variable . What I usually do is run the command :-


- dmd -J. program.d

Then I inspect the program.o file using vi for presence of 
compile-time constants and enums. I am wondering if this is the 
right way to do this. Am a bit skeptical about this as am working 
on pushing everything that the compiler can do to the compiler 
for faster run-time.


Re: monitoring variable evaluation time

2015-07-20 Thread Clayton via Digitalmars-d-learn

On Monday, 20 July 2015 at 09:29:26 UTC, anonymous wrote:

On Monday, 20 July 2015 at 08:53:52 UTC, Clayton wrote:

[...]



You mean you want to know if some value is pre-computed during 
compilation?


[...]



Thankyou a lot for the suggestion. Am noting as I learn the 
language.


Re: monitoring variable evaluation time

2015-07-20 Thread Clayton via Digitalmars-d-learn

On Monday, 20 July 2015 at 10:11:10 UTC, Marc Schütz wrote:

On Monday, 20 July 2015 at 08:53:52 UTC, Clayton wrote:

[...]


I'm not sure that's what you want to know, but...

The evaluation time of expressions is completely deterministic. 
Global (module level) or static variables, default values for 
struct/class members, template arguments, as well as enums, are 
evaluated at compile-time and thus their construction has no 
runtime costs.


[...]



Thankyou a lot for the suggestion. Am noting as I learn the 
language.


static-if cant compile

2015-07-19 Thread Clayton via Digitalmars-d-learn

Pardon me for trivial question, Am new to D.


Why would a statement as below fail to compile. The plan is to do 
some computation at compile-time hence considering the static-if 
statement which fails to compile. The regular  if-statement 
compiles but is not useful since it is a runtime construct.


The error message I get is :  Error: variable i cannot be read at 
compile time





foreach( i; 0..size-1){
static if  ( i == -1 ){
//Do something
}

}


String Metaprogramming

2015-07-18 Thread Clayton via Digitalmars-d-learn
Am new to D programming, am considering it since it supports 
compile-time function execution . My challenge is how can I 
re-implement the function below so that it is fully executed in 
compile-time. The function should result to tabel1 being computed 
at compile-time. There seems to be a lot of mutation happening 
here yet I have heard no mutation should take place in 
meta-programming as it subscribes to functional programming 
paradigm.




void computeAtCompileTime( ref string pattern ,ref int[char] 
tabel1){

int size = to!int(pattern.length) ;

foreach( c; ALPHABET){
tabel1[c] = size;
}

for( int i=0;isize -1 ; ++i){   //Initialise array
tabel1[pattern[i]] = size -i-1;

pragma(msg, format(reached pattern  
table1[pattern[i]]=(%s) here,
table1[pattern[i]].stringof  ~ v=~ (size 
-i-1).stringof));

}




}



Re: String Metaprogramming

2015-07-18 Thread Clayton via Digitalmars-d-learn

On Saturday, 18 July 2015 at 16:01:25 UTC, Nicholas Wilson wrote:

On Saturday, 18 July 2015 at 13:48:20 UTC, Clayton wrote:

[...]




[...]


change function signature to
int[char] function(string) or as the char type is the index 
probably better of as
int[256] function(string). also probably no need to take 
pattern by ref as it is effectively struct{ size_t length; 
char* ptr;}. also we aren't going to modify it.


int[256] computeAtCompileTime(string pattern)
{

[...]
pattern.length is a size_t no need to change its type in 
another variable.  you are unlikely to be dealing with string 
longer than 2^32 (also signedness) but w/e
int[256] ret; // implicitly initialised to int.init 
(i.e. 0)



[...]

can just foreach over pattern
foreach(i, c; pattern)
ret[c] = pattern.length - i -1;


[...]



[...]


if you want this to be not callable at runtime then wrap the 
main body (sans variable declaration) with

if (__ctfe)
{
 ...

}
Thanks  Nicholas , I have integrated some of your advice on the 
edited code i.e. foreach and ref in pattern . Hope I fully 
understood  what you meant. Am yet to look whether I still need 
to change the signature . I have heared there are two approaches 
to this, Where does one really draw the line between CTFE and 
Template metaprogramming?


Re: String Metaprogramming

2015-07-18 Thread Clayton via Digitalmars-d-learn

On Saturday, 18 July 2015 at 13:56:36 UTC, Adam D. Ruppe wrote:

On Saturday, 18 July 2015 at 13:48:20 UTC, Clayton wrote:
There seems to be a lot of mutation happening here yet I have 
heard no mutation should take place in meta-programming as it 
subscribes to functional programming paradigm.


That's not true in D, you can just write a regular function and 
evaluate it in a compile time context, like initializing a 
static variable.


You usually don't need to write special code for compile time 
stuff in D.



Thanks , you were right . It seems there are some key words 
though which one has to use so that the code gets executed on 
compile-time .For example I had to change the second forloop to a 
foreach loop, and then put and enum to ensure that 
TableFromCompiler gets evaluated at compiletime. Having written 
the code this way though gives rise to some other question, D 
supports 2 approches to compiletime metaprogramming i.e. CTFE and 
Templates, it seems am not very sure which paradigm my code falls 
in.



import std.stdio;
import std.string;
import std.conv;


I[C]  computeAtCompileTime(S ,C,I)( const S  pattern ){
  I[C] table1;

  const int size = to!int(pattern.length) ;//Length of the 
pattern to be matched


  foreach( c; ALPHABET){   //Initialise array
  table1[c] = size;
}

  foreach(i; 0..size-1){
 table1[pattern[i]] = size -i-1;
  }
  return table1;
}

void main(){

 enum TableFromCompiler  = computeAtCompileTime!(const string 
,char, int)(pattern);


 writeln(TableFromCompiler);
 }