Re: countUntil for SortedRange

2015-08-11 Thread John Colvin via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 19:30:02 UTC, Laeeth Isharc wrote:

Hi.

Basic question: suppose I have a SortedRange and want to find 
the index of the first entry of an array of structs matching a 
needle struct.


What's the best way to do that?  It's not clear that countUntil 
treats a SortedRange specially.


I could get the lowerBound and then length or walkLength (can't 
remember which applies).


But I figure there must be a better way.

Thanks.


Laeeth.


Yeah, I was wrong when I said it was treated specially. 
lowerBound followed by length should do it without any notable 
inefficiencies. I guess you could wrap it as `sortedCountUntil` 
for convenience if you like.


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.



Problem with dmd 2.068 Win 32

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

Hi!

My project has an error link:

Error 42: Symbol Undefined 
_D6object9Exception6__ctorMFNaNbNfAyaAyakC6object9ThrowableZC9Exception


On dmd 2.067.* everything gathered without mistakes. Where to 
look for a mistake?


Re: Problem with dmd 2.068 Win 32

2015-08-11 Thread Dominikus Dittes Scherkl via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 15:04:29 UTC, MGW wrote:

Hi!

My project has an error link:

Error 42: Symbol Undefined 
_D6object9Exception6__ctorMFNaNbNfAyaAyakC6object9ThrowableZC9Exception


On dmd 2.067.* everything gathered without mistakes. Where to 
look for a mistake?


See the changelog.
The compiler is now pickier if you forgot to link something 
explicitly.


Re: Problem with dmd 2.068 Win 32

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

Thanks to all! The problem is localized!


Re: Problem with dmd 2.068 Win 32

2015-08-11 Thread Martin Nowak via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 15:04:29 UTC, MGW wrote:

Hi!

My project has an error link:

Error 42: Symbol Undefined 
_D6object9Exception6__ctorMFNaNbNfAyaAyakC6object9ThrowableZC9Exception


On dmd 2.067.* everything gathered without mistakes. Where to 
look for a mistake?


Try ddemangle (part of the distribution).

ddemangle
_D6object9Exception6__ctorMFNaNbNfAyaAyakC6object9ThrowableZC9Exception

-

pure nothrow @safe Exception 
object.Exception.__ctor(immutable(char)[], immutable(char)[], 
uint, object.Throwable)


In the current release @nogc was added.
https://github.com/D-Programming-Language/druntime/blob/v2.068.0/src/object.d#L1614

You either have a wrong import paths (check which dmd.conf is 
used with 'dmd -v non_existent') or a stable object_.di file.


Re: Problem with dmd 2.068 Win 32

2015-08-11 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 11 August 2015 at 15:10:57 UTC, Dominikus Dittes 
Scherkl wrote:

See the changelog.
The compiler is now pickier if you forgot to link something 
explicitly.


That shouldn't affect an Exception constructor through since they 
are part of the core druntime.



I suspect it has to do with the update leaving some old files 
behind... might help to clean out the old dir and reinstall the 
dmd.


Re: Problem with dmd 2.068 Win 32

2015-08-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 15:18:31 UTC, Martin Nowak wrote:
You either have a wrong import paths (check which dmd.conf is 
used with 'dmd -v non_existent') or a stable object_.di file.


Or maybe a stale .obj or .lib file, referencing the old symbol.

Try a make clean too - delete any .obj and .lib files in your 
project directory and rebuild.


countUntil for SortedRange

2015-08-11 Thread Laeeth Isharc via Digitalmars-d-learn

Hi.

Basic question: suppose I have a SortedRange and want to find the 
index of the first entry of an array of structs matching a needle 
struct.


What's the best way to do that?  It's not clear that countUntil 
treats a SortedRange specially.


I could get the lowerBound and then length or walkLength (can't 
remember which applies).


But I figure there must be a better way.

Thanks.


Laeeth.


Re: Infinity loop with dates comparison

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

On Tuesday, 11 August 2015 at 19:56:02 UTC, Suliman wrote:

Date startDate = Date.fromISOExtString(2014-08-01);

[...]

Date nextday;

while (nextday  currentDate)
{
nextday = startDate + 1.days;
writeln(nextday);
}


startDate doesn't change, so every iteration just sets nextday to 
2014-08-01 + 1 day = 2014-08-02.


Re: Infinity loop with dates comparison

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

On Tuesday, 11 August 2015 at 19:56:02 UTC, Suliman wrote:

The code look very trivial, but I am getting infinity loop like:
2014-Aug-02
2014-Aug-02
2014-Aug-02
...
2014-Aug-02


Date startDate = Date.fromISOExtString(2014-08-01);

Date currentDate =  to!(Date)(Clock.currTime()-1.days); 
//because current day is not finished


writeln(startDate);
writeln(currentDate);

Date nextday;

while (nextday  currentDate)
{
nextday = startDate + 1.days;
writeln(nextday);
}


This isn't a D problem, you just always set nextday to the same 
value that doesn't change (startDate + 1.days).


Maybe what you meant was:

nextday = startDate;
while (nextday  currentDate)
{
nextday = nextday + 1.days;
writeln(nextday);
}



Infinity loop with dates comparison

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

The code look very trivial, but I am getting infinity loop like:
2014-Aug-02
2014-Aug-02
2014-Aug-02
...
2014-Aug-02


Date startDate = Date.fromISOExtString(2014-08-01);

Date currentDate =  to!(Date)(Clock.currTime()-1.days); //because 
current day is not finished


writeln(startDate);
writeln(currentDate);

Date nextday;

while (nextday  currentDate)
{
nextday = startDate + 1.days;
writeln(nextday);
}


Re: Infinity loop with dates comparison

2015-08-11 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Aug 11, 2015 at 07:56:00PM +, Suliman via Digitalmars-d-learn wrote:
[...]
 Date startDate = Date.fromISOExtString(2014-08-01);
 
 Date currentDate =  to!(Date)(Clock.currTime()-1.days); //because current
 day is not finished
 
   writeln(startDate);
   writeln(currentDate);
 
   Date nextday;
 
   while (nextday  currentDate)
   {
   nextday = startDate + 1.days;
  ^
Because you're always computing from startDate, which is constant. That
should be 'nextday' instead.


   writeln(nextday);
   }


T

-- 
Making non-nullable pointers is just plugging one hole in a cheese grater. -- 
Walter Bright


Convert a hex color string into r,g,b components.

2015-08-11 Thread Marcin Szymczak via Digitalmars-d-learn
When programming i have encountered a simple ( i think ) problem, 
yet i can't get my head around it. I am trying to convert a 
string ( like #FF00FF for magenta ) into a color. I figured out 
that i need to skip the first character '#' and then using 
chunks range convert each pair of 2 chars into a number and 
assign it to specific component. The snippet looks like this


Color color;
auto chunk = chunks( str[1..$], 2 );
color.r = to!ubyte( chunk.front, 16 ); chunk.popFront;
color.g = to!ubyte( chunk.front, 16 ); chunk.popFront;
color.b = to!ubyte( chunk.front, 16 ); chunk.popFront;

But the compilation fails, stating

/usr/include/dlang/dmd/std/conv.d(295): Error: template 
std.conv.toImpl cannot deduce function from argument types 
!(ubyte)(Take!string, int), candidates are:
/usr/include/dlang/dmd/std/conv.d(361):std.conv.toImpl(T, 
S)(S value) if (isImplicitlyConvertible!(S, T)  
!isEnumStrToStr!(S, T)  !isNullToStr!(S, T))
/usr/include/dlang/dmd/std/conv.d(475):std.conv.toImpl(T, 
S)(ref S s) if (isRawStaticArray!S)
/usr/include/dlang/dmd/std/conv.d(491):std.conv.toImpl(T, 
S)(S value) if (!isImplicitlyConvertible!(S, T)  
is(typeof(S.init.opCast!T()) : T)  !isExactSomeString!T  
!is(typeof(T(value
/usr/include/dlang/dmd/std/conv.d(542):std.conv.toImpl(T, 
S)(S value) if (!isImplicitlyConvertible!(S, T)  is(T == 
struct)  is(typeof(T(value
/usr/include/dlang/dmd/std/conv.d(591):std.conv.toImpl(T, 
S)(S value) if (!isImplicitlyConvertible!(S, T)  is(T == class) 
 is(typeof(new T(value
/usr/include/dlang/dmd/std/conv.d(295):... (9 more, -v to 
show) ...
source/engine/graphics/core.d(43): Error: template instance 
std.conv.to!ubyte.to!(Take!string, int) error instantiating


I would really love to solve this problem using ranges, because i 
am learning how to use them. Unfortunately even such a simple 
task seems so hard for me ;(


Re: Convert a hex color string into r,g,b components.

2015-08-11 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 22:11:51 UTC, Marcin Szymczak wrote:
/usr/include/dlang/dmd/std/conv.d(295): Error: template 
std.conv.toImpl cannot deduce function from argument types 
!(ubyte)(Take!string, int), candidates are:



I don't think to! with the base given works on the chunked 
ranges, it just works on regular strings.


I would really love to solve this problem using ranges, because 
i am learning how to use them. Unfortunately even such a simple 
task seems so hard for me ;(


meh, I'd just slice the string.


Re: Derelict, SDL, and OpenGL3: Triangle Tribulations

2015-08-11 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 12 August 2015 at 05:35:30 UTC, Mike Parker wrote:


One of the best I've seen is by Anton Gerdelan [1]. The four


[1] http://antongerdelan.net/opengl/index.html


Re: Derelict, SDL, and OpenGL3: Triangle Tribulations

2015-08-11 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 12 August 2015 at 03:32:47 UTC, DarthCthulhu wrote:


So, any ideas what I'm doing wrong?


Too much to list. I suggest you get going with a good tutorial. 
One of the best I've seen is by Anton Gerdelan [1]. The four 
basic tutorials he has on his site will be enough to get you up 
and running. He used to have a lot more there, but he's published 
them all in a book. Even though there are numerous free resources 
online, I think Anton's ebook is well worth the ~$9.00 you pay 
for it. His tutorials are quite detailed and do a lot more than 
showing you a bunch of code to copy and paste. Work your way 
through that book and you'll know your way around well enough to 
do what you need to do for any basic OpenGL renderer.


Re: Derelict, SDL, and OpenGL3: Triangle Tribulations

2015-08-11 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 12 August 2015 at 05:34:22 UTC, BBasile wrote:

static this()
{
DerelictGL3.load;
DerelictGL.load;
DerelictSDL2.load;
SDL_Init(SDL_INIT_VIDEO);
}



I should point out that *either* DerelictGL3 *or* DerelictGL 
should be loaded, but never both. DerelictGL actually extends 
DerelictGL3. When you call load on it, it calls super.load (ditto 
for reload), with the net effect that you're loading all of the 
DerelictGL3 stuff twice. If you need the deprecated stuff, just 
use DerelictGL; if you don't, just use DerelictGL3.


Re: Derelict, SDL, and OpenGL3: Triangle Tribulations

2015-08-11 Thread Mike Parker via Digitalmars-d-learn

On Wednesday, 12 August 2015 at 05:34:22 UTC, BBasile wrote:

For me the following code works:

---
import derelict.sdl2.sdl;
import derelict.opengl3.gl3;
import derelict.opengl3.gl;

import std.stdio;

static this()
{
DerelictGL3.load;
DerelictGL.load;
DerelictSDL2.load;
SDL_Init(SDL_INIT_VIDEO);
}

static ~this()
{
SDL_Quit();
DerelictGL3.unload;
DerelictSDL2.unload;
}

GLuint initVAO () {

// An array of 3 vectors which represents 3 vertices
static const GLfloat[] g_vertex_buffer_data = [
  -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f, 1.0f, 0.0f,
];

// This will identify our vertex buffer
GLuint vertexbuffer;

	// Generate 1 buffer, put the resulting identifier in 
vertexbuffer

glGenBuffers(1, vertexbuffer);

	// The following commands will talk about our 'vertexbuffer' 
buffer

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.length * 
GL_FLOAT.sizeof, g_vertex_buffer_data.ptr, GL_STATIC_DRAW);


glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,  null);  

glBindBuffer(GL_ARRAY_BUFFER, 0);

return vertexbuffer;
}

void main(string[] args)
{

auto flags =  SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | 
SDL_WINDOW_RESIZABLE;

auto win = SDL_CreateWindow( null, 50, 50, 800, 600, flags);
auto ctxt = SDL_GL_CreateContext(win);

DerelictGL3.reload;

GLuint vertexbuffer = initVAO();

SDL_Event ev;
while (true)
{
if (SDL_WaitEvent(ev))
{
glClear(GL_COLOR_BUFFER_BIT);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindVertexArray(vertexbuffer);

// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(vertexbuffer);
glBindVertexArray(0);

SDL_GL_SwapWindow(SDL_GL_GetCurrentWindow());
}
if (ev.type == SDL_QUIT)
break;
}

SDL_DestroyWindow(win);
SDL_GL_DeleteContext(ctxt);
}
---

It looks like it's the window/context creation that fail for 
you because the OpenGL code is 100% the same.



It seems to me that your driver is doing things it isn't actually 
supposed to do. This code is binding a vertex buffer object with 
a function which is supposed to bind a vertex array object. The 
only reason the vbo is bound at all is because of the call to 
glBindBuffer in the misnamed initVAO -- a function which never 
even initializes a vao. The spec actually requires a vao to be 
created and a shader program to be bound, so I would expect a 
conforming driver to show nothing.


I expect a couple of calls to glError will not come up empty.


Re: Code Reviewer

2015-08-11 Thread Rikki Cattermole via Digitalmars-d-learn

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.



Re: Derelict, SDL, and OpenGL3: Triangle Tribulations

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

On Wednesday, 12 August 2015 at 03:32:47 UTC, DarthCthulhu wrote:
So I decided to try some OGL3 stuff in D utilizing the Derelict 
bindings and SDL. Creating an SDL-OGL window worked fine, but 
I'm having trouble with doing the most basic thing of rendering 
a triangle. I get the window just fine and the screen is being 
properly cleared and buffered, but no triangle.


So, any ideas what I'm doing wrong?


For me the following code works:

---
import derelict.sdl2.sdl;
import derelict.opengl3.gl3;
import derelict.opengl3.gl;

import std.stdio;

static this()
{
DerelictGL3.load;
DerelictGL.load;
DerelictSDL2.load;
SDL_Init(SDL_INIT_VIDEO);
}

static ~this()
{
SDL_Quit();
DerelictGL3.unload;
DerelictSDL2.unload;
}

GLuint initVAO () {

// An array of 3 vectors which represents 3 vertices
static const GLfloat[] g_vertex_buffer_data = [
  -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f, 1.0f, 0.0f,
];

// This will identify our vertex buffer
GLuint vertexbuffer;

	// Generate 1 buffer, put the resulting identifier in 
vertexbuffer

glGenBuffers(1, vertexbuffer);

	// The following commands will talk about our 'vertexbuffer' 
buffer

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.length * 
GL_FLOAT.sizeof, g_vertex_buffer_data.ptr, GL_STATIC_DRAW);


glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,  null);  

glBindBuffer(GL_ARRAY_BUFFER, 0);

return vertexbuffer;
}

void main(string[] args)
{

auto flags =  SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | 
SDL_WINDOW_RESIZABLE;

auto win = SDL_CreateWindow( null, 50, 50, 800, 600, flags);
auto ctxt = SDL_GL_CreateContext(win);

DerelictGL3.reload;

GLuint vertexbuffer = initVAO();

SDL_Event ev;
while (true)
{
if (SDL_WaitEvent(ev))
{
glClear(GL_COLOR_BUFFER_BIT);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindVertexArray(vertexbuffer);

// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(vertexbuffer);
glBindVertexArray(0);

SDL_GL_SwapWindow(SDL_GL_GetCurrentWindow());
}
if (ev.type == SDL_QUIT)
break;
}

SDL_DestroyWindow(win);
SDL_GL_DeleteContext(ctxt);
}
---

It looks like it's the window/context creation that fail for you 
because the OpenGL code is 100% the same.


Re: countUntil for SortedRange

2015-08-11 Thread Laeeth Isharc via Digitalmars-d-learn

On Tuesday, 11 August 2015 at 21:38:49 UTC, John Colvin wrote:

On Tuesday, 11 August 2015 at 19:30:02 UTC, Laeeth Isharc wrote:

Hi.

Basic question: suppose I have a SortedRange and want to find 
the index of the first entry of an array of structs matching a 
needle struct.


What's the best way to do that?  It's not clear that 
countUntil treats a SortedRange specially.


I could get the lowerBound and then length or walkLength 
(can't remember which applies).


But I figure there must be a better way.

Thanks.


Laeeth.


Yeah, I was wrong when I said it was treated specially. 
lowerBound followed by length should do it without any notable 
inefficiencies. I guess you could wrap it as `sortedCountUntil` 
for convenience if you like.


Thanks, John.

Laeeth



Derelict, SDL, and OpenGL3: Triangle Tribulations

2015-08-11 Thread DarthCthulhu via Digitalmars-d-learn
So I decided to try some OGL3 stuff in D utilizing the Derelict 
bindings and SDL. Creating an SDL-OGL window worked fine, but I'm 
having trouble with doing the most basic thing of rendering a 
triangle. I get the window just fine and the screen is being 
properly cleared and buffered, but no triangle.


Code:

GLuint initVAO () {

// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = [
  -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f, 1.0f, 0.0f,
];

// This will identify our vertex buffer
GLuint vertexbuffer;

	// Generate 1 buffer, put the resulting identifier in 
vertexbuffer

glGenBuffers(1, vertexbuffer);

	// The following commands will talk about our 'vertexbuffer' 
buffer

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
	glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.length * 
GL_FLOAT.sizeof, g_vertex_buffer_data.ptr, GL_STATIC_DRAW);


glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,  null);  

glBindBuffer(GL_ARRAY_BUFFER, 0);

return vertexbuffer;
}

void main() {

string title = Hello, world! With SDL2 OpenGL3.3!;
writefln(title);

// Create a new OGL Window
Window win = new SDL_OGL_Window(title);

GLuint vertexbuffer = initVAO();

// Main loop flag
bool quit = false;

//Event handler
SDL_Event e;

glClearColor(0,0,0.4,0);

do {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindVertexArray(vertexbuffer);

// Draw the triangle
glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableVertexAttribArray(vertexbuffer);
glBindVertexArray(0);

SDL_GL_SwapWindow(win.window);

// Handle events on queue
while( SDL_PollEvent( e ) != 0 ) {
//User requests quit
if( e.type == SDL_QUIT ) {
quit = true;
}
}

const Uint8* currentKeyStates = SDL_GetKeyboardState( null );

// Escape to quit as well.
if( currentKeyStates[ SDL_SCANCODE_ESCAPE ] ) {
quit = true;;
}

} while ( !quit );

glDeleteBuffers(1, vertexbuffer);

}

The SDL_OGL_Window is an object that creates an SDL window and 
binds a OpenGL context to it; I don't think it's relevant for 
what's going on, but if anyone thinks it might be the culprit, 
I'll post it.


So, any ideas what I'm doing wrong?


Re: Derelict, SDL, and OpenGL3: Triangle Tribulations

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

On Wednesday, 12 August 2015 at 03:32:47 UTC, DarthCthulhu wrote:
So I decided to try some OGL3 stuff in D utilizing the Derelict 
bindings and SDL. Creating an SDL-OGL window worked fine, but 
I'm having trouble with doing the most basic thing of rendering 
a triangle. I get the window just fine and the screen is being 
properly cleared and buffered, but no triangle.


[...]


You need a vertex and a fragment shader. You can't render 
anything in OGL3 without shaders.


Also, you seem to be confusing Vertex Array Objects and Vertex 
Buffer Objects. You are creating a VBO and try to bind it as a 
VAO. You should put a glGenVertexArrays somewhere there.