Re: Trying to understand multidimensional arrays in D

2017-01-29 Thread Ali Çehreli via Digitalmars-d-learn

On 01/25/2017 05:47 PM, Profile Anaysis wrote:

> a 4x4 matrix and have a history of it. Just
> n 4x4 matrices but each matrix is a fixed size but there can be an
> arbitrary(dynamic) number.

I don't think using aliases is recommended yet. It can simplify things a 
lot:


import std.stdio;

alias Row = int[4];
alias Matrix = Row[4];
alias History = Matrix[];

Row makeRow(int value) {
Row row;
row = value;// Special array syntax; all elements are now 'value'
return row;
}

Matrix makeMatrix() {
Matrix matrix;
foreach (int i, ref row; matrix) {
row = makeRow(i + 1);
}
return matrix;
}

void main() {
History history;
foreach (i; 0..3) {
history ~= makeMatrix();
}
writeln(history);
}

As others have said, D's array definition is natural because unlike C's 
inside-out (or is that outside-in?) syntax, it follows from the alias 
syntax. Replacing History inside main with Matrix[], etc.:


History history;// is the same as:
Matrix[] history;   // is the same as:
Row[4][] history;   // is the same as:
int[4][4][] history;

Ali



Re: Problems compiling sqlite-d

2017-01-29 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 30 January 2017 at 02:46:34 UTC, Nestor wrote:
Well, I had downloaded the github version a few days back but 
yesterday managed to get dub to fetch properly, so I just 
fetched package arsd, and took the units from there.


Oh, that is ancient and not even mine - I don't have access to 
it, it was set up by a third party years ago!




d:\prj\sqltest2\source>dmd app


That is always wrong unless your entire program consists *only* 
of app.d. When compiling, you need to have imported modules 
available or you'll see "module foo is found in file foo.d that 
cannot be found", and when linking, you need to have the code 
available or you'll see "Symbol Undefined". Since compiling and 
linking are done in the same step unless you ask it not to, 
missing modules may lead to one, the other, or both.


The easiest solution is to pass all the modules to the compiler 
at once. Then it will be able to find them for import and will 
compile and link them automatically. If you link separately (such 
as to a .lib or .dll), you can specify them rather than all the 
.d files, but it still needs the .d for the import!


Whether it is library modules or another module in your own file, 
it is the same, they all need to be there.




If I specify all source files, there are even more problems:
 Error 42: Symbol Undefined _sqlite3_open


It apparently couldn't find sqlite3.lib.

Files sqlite3.{def|dll|lib} are on both source/ and 
source/arsd/ (just in case)


Try specifying it on the command line too:

dmd app.d database.d sqlite.d sqlite3.lib

Though this may still require sqlite3.dll there too, unless it 
was built statically.


Re: Problems compiling sqlite-d

2017-01-29 Thread Nestor via Digitalmars-d-learn

On Monday, 30 January 2017 at 02:25:40 UTC, Adam D. Ruppe wrote:

On Monday, 30 January 2017 at 00:06:00 UTC, Nestor wrote:
I wasn't doing it explicitly. However I just did that and 
still encountered a few errors, which I removed with this 
patch:


Where did you get that ancient version? The latest versions of 
the files work just fine out of the box, and they have for 
about a year now.


these links work:

https://github.com/adamdruppe/arsd/blob/master/database.d
https://github.com/adamdruppe/arsd/blob/master/sqlite.d


Well, I had downloaded the github version a few days back but 
yesterday managed to get dub to fetch properly, so I just fetched 
package arsd, and took the units from there.


Anyway, I have just downloaded from github the files you 
recomend, but...


d:\prj\sqltest2\source>dmd app
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
app.obj(app)
 Error 42: Symbol Undefined 
_D4arsd6sqlite6Sqlite6__ctorMFAyaiZC4arsd6sqlite6Sqlite

app.obj(app)
 Error 42: Symbol Undefined _D4arsd6sqlite6Sqlite7__ClassZ
app.obj(app)
 Error 42: Symbol Undefined _D4arsd6sqlite12__ModuleInfoZ
Error: linker exited with status 163488904

If I specify all source files, there are even more problems:

d:\prj\sqltest2\source>dmd app.d arsd\sqlite.d arsd\database.d
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_open
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_finalize
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_prepare_v2
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_mprintf
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_free
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_exec
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_last_insert_rowid
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_changes
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_errmsg
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_close
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_reset
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_blob
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_bytes
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_int
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_name
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_step
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_text
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_double
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_type
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_column_count
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_null
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_blob
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_double
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_int
app.obj(app)
 Error 42: Symbol Undefined _sqlite3_bind_text
Error: linker exited with status 211947944

Source of app.d couldn't be simpler:

import std.stdio;
void main() {
  import arsd.sqlite;
  auto db = new Sqlite("data.db");
}

Files sqlite3.{def|dll|lib} are on both source/ and source/arsd/ 
(just in case)
I also moved your files to the same location of app.d but it 
makes no difference.


Re: Problems compiling sqlite-d

2017-01-29 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 30 January 2017 at 00:06:00 UTC, Nestor wrote:
I wasn't doing it explicitly. However I just did that and still 
encountered a few errors, which I removed with this patch:


Where did you get that ancient version? The latest versions of 
the files work just fine out of the box, and they have for about 
a year now.


these links work:

https://github.com/adamdruppe/arsd/blob/master/database.d
https://github.com/adamdruppe/arsd/blob/master/sqlite.d


Re: D idom for removing array elements

2017-01-29 Thread albert-j via Digitalmars-d-learn

On Monday, 30 January 2017 at 00:17:51 UTC, ag0aep6g wrote:

[...]


Great explanation, thank you!



Re: D idom for removing array elements

2017-01-29 Thread ag0aep6g via Digitalmars-d-learn

On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote:

int[] arr;
foreach (i; 0..10)
arr ~= i;

writeln("Original array: ",arr);
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -- OK

auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2);


arrMap is a range. The filter and map operations only happen when 
the range is iterated. The array on which arrMap operates is the 
memory that is referred to by arr. If you change the values in 
that memory before evaluating arrMap, the result will be affected.


However, `filter` also does something on creation: it skips to 
the first element that passes the predicate. I.e., it skips to 6 
in your code. Or rather, it skips to the memory location where 6 
is stored at the time. Since it happens on creation, that 
skipping will not be re-evaluated when you iterate over arrMap 
multiple times.



writeln("arrMap: ", arrMap);
// [36, 49, 64, 81] -- OK


Note that the values are computed while printing to the screen. 
They're not stored anywhere.



int[] toRemove = [1, 2, 9];
arr = arr.remove!(x => toRemove.canFind(x)).array;


Removing works by overwriting the array with only the wanted 
values and discarding the rest.


The case at hand in detail:

* Look at arr[0]. It's 0. That's not in [1, 2, 9], so it's copied 
to arr[0].

* Look at arr[1]. It's 1. That's in [1, 2, 9], so it's discarded.
* Look at arr[2]. It's 2. That's in [1, 2, 9], so it's discarded.
* Look at arr[3]. It's 3. That's not in [1, 2, 9], so it's copied 
to arr[1].
* arr[4] through arr[8] are not in [1, 2, 9] either, so they're 
copied to arr[2] through arr[6].

* arr[9] is in [1, 2, 9], so it's not copied.

Note that arr[7] through arr[9] have not been overwritten. So 
you've got this in arr: [0, 3, 4, 5, 6, 7, 8, 7, 8, 9]. The last 
three values are then sliced off, because three values have been 
removed. Those values are still there in memory, though. The 
array's new length just stops before them.



writeln("Original array after removal: ", arr);
// [0, 3, 4, 5, 6, 7, 8] -- OK

arr ~= arrMap.array;

writeln("Original array after removal and concatenation: ", 
arr);

// [0, 3, 4, 5, 6, 7, 8, 64, 49, 64, 81] -- what?


Now, you've created arrMap before calling `remove`, so it still 
uses the old length for its underlying array. But it sees the new 
values, because `remove` has updated them in place. That means, 
it sees the values mentioned above: [0, 3, 4, 5, 6, 7, 8, 7, 8, 
9]. Except, `filter` has already skipped the first six elements. 
So arrMap sees this: [8, 7, 8, 9]. Square those values and you 
get [64, 49, 64, 81], which is what you see.


Generally, I'd say don't alter the underlying range/array between 
creating and evaluating a range, nor during evaluation. `filter` 
is most probably not the only range that misbehaves in that 
situation.


Instead of mixing lazy ranges with the eager, in-place `remove`, 
you can use filter again:



void main()
{
import std.array: array;
import std.algorithm: canFind, filter, map;
import std.range: chain;
import std.stdio: writeln;

int[] arr;
foreach (i; 0..10) arr ~= i;

immutable int[] toRemove = [1, 2, 9];
arr = chain(
arr.filter!(x => !toRemove.canFind(x)),
arr.filter!(x => x > 5).map!(x => x^^2),
).array;

writeln(arr); /* prints "[0, 3, 4, 5, 6, 7, 8, 36, 49, 64, 
81]" */

}



Re: D idom for removing array elements

2017-01-29 Thread albert-j via Digitalmars-d-learn

On Sunday, 29 January 2017 at 23:48:40 UTC, Jordan Wilson wrote:


You need to do something like this:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array;

It's because arrMap is lazy evaluated.



So does it mean that I cannot assign FilterResult and MapResult 
to a variable and safely use it later, because underlying array 
may change meanwhile? Since I am dealing with large arrays, I 
thought I'd save some memory by not calling .array on MapResult.


Re: Problems compiling sqlite-d

2017-01-29 Thread Nestor via Digitalmars-d-learn

On Sunday, 29 January 2017 at 17:36:45 UTC, Adam D. Ruppe wrote:

On Sunday, 29 January 2017 at 16:26:30 UTC, Nestor wrote:

dmd yourfile.d database.d sqlite.d

I have just tried your way and I get some errors:
 Error 42: Symbol Undefined 
_D4arsd8database3Row7opIndexMFkAyaiZAya


Are you sure you passed those two database.d and sqlite.d 
modules to the compiler?


I wasn't doing it explicitly. However I just did that and still 
encountered a few errors, which I removed with this patch:


--- original\sqlite.d   2017-01-29 10:53:35 -0100
+++ modified\sqlite.d   2017-01-29 19:00:23 -0100
@@ -22 +22,2 @@
-import std.c.stdlib;
+import core.stdc.string : strlen;
+import core.stdc.stdlib : malloc, free;
@@ -143 +144 @@
-   sizediff_t a = std.c.string.strlen(mesg);
+   sizediff_t a = strlen(mesg);
@@ -164 +165 @@
-   sizediff_t a = std.c.string.strlen(mesg);
+   sizediff_t a = strlen(mesg);
@@ -285 +286 @@
-   sizediff_t l = std.c.string.strlen(str);
+   sizediff_t l = strlen(str);
@@ -335 +336 @@
-   sizediff_t l = 
std.c.string.strlen(str);
+   sizediff_t l = strlen(str);
@@ -558 +559 @@
-   p = std.c.stdlib.malloc(sz);
+   p = malloc(sz);
@@ -569 +570 @@
-   std.c.stdlib.free(p);
+   free(p);
@@ -626 +627 @@
-   sizediff_t b = std.c.string.strlen(columns[a]);
+   sizediff_t b = strlen(columns[a]);
@@ -632 +633 @@
-   sizediff_t d = std.c.string.strlen(text[a]);
+   sizediff_t d = strlen(text[a]);

However a couple of errors remain with database.d which I don't 
know how to fix:


arsd\database.d(644): Error: function std.json.JSONValue.type () 
const is not callable using argument types (JSON_TYPE)
arsd\database.d(647): Error: function std.json.JSONValue.type () 
const is not callable using argument types (JSON_TYPE)




Re: D idom for removing array elements

2017-01-29 Thread Jordan Wilson via Digitalmars-d-learn

On Sunday, 29 January 2017 at 23:42:40 UTC, Jordan Wilson wrote:

On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote:

On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote:

[...]


I am trying to wrap my head around lazy evaluation during 
filtering/mapping, but there's something I don't understand.
I want to create an array, square some elements, remove some 
elements from original array and add the squared ones to the 
original array:


[...]


You need to do something like this:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array;

It's because arrMap is lazy evaluated.


Specifically:
arr ~= arrMap.array;
will cause arrMap to be evaluated again using whatever arr is.

So instead of:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2);
// mutate arr
arr ~= arrMap.array;

you would want:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array;
// mutate arr
arr ~= arrMap;



Re: D idom for removing array elements

2017-01-29 Thread Jordan Wilson via Digitalmars-d-learn

On Sunday, 29 January 2017 at 21:41:57 UTC, albert-j wrote:

On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote:

[...]


I am trying to wrap my head around lazy evaluation during 
filtering/mapping, but there's something I don't understand.
I want to create an array, square some elements, remove some 
elements from original array and add the squared ones to the 
original array:


[...]


You need to do something like this:
auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2).array;

It's because arrMap is lazy evaluated.


Re: Loading assimp

2017-01-29 Thread Dlearner via Digitalmars-d-learn
Need to rez this thread because I ran into a wall.  Two little 
things:


1) Can't seem to get the Importer class to work ("undefined 
identifier 'Importer' ", etc), and
2) GetTexture and GetTextureCount for aiMaterial don't seem to 
work
(source\model.d(105,28): Error: no property 'GetTextureCount' for 
type 'aiMaterial*'
source\model.d(107,6): Error: no property 'GetTexture' for type 
'aiMaterial*')


There are some const issues, too, but I can hack around them.  
Does anyone have any experience with these?


Re: Partial arrays reclaimed?

2017-01-29 Thread ag0aep6g via Digitalmars-d-learn

On Friday, 27 January 2017 at 23:22:17 UTC, Nick Sabalausky wrote:

Suppose an array is being used like a FIFO:

---
T[] slice;

// Add:
slice ~= T();

// Remove:
slice = slice[1..$];
---

Assuming of course there's no other references to the memory, 
as this gets used, does the any of the memory from the removed 
elements ever get GC'd?


If the array has no additional capacity, then appending will 
relocate the data. I.e., copy it to a larger allocation. The old 
data can then be collected. Since the old first element is not 
part of the new array, it's doesn't get copied over. So the 
allocation doesn't grow indefinitely.


Also, if this is a long-running process, isn't there a 
potential danger in the array just marching through the address 
space and running out of room? (ie either running out of of 
continuous space, or hitting 0xFFF)


If you append and pop the front over and over, the program should 
reuse old locations, cycling through them.


Re: D idom for removing array elements

2017-01-29 Thread albert-j via Digitalmars-d-learn

On Saturday, 28 January 2017 at 11:54:58 UTC, cym13 wrote:




I am trying to wrap my head around lazy evaluation during 
filtering/mapping, but there's something I don't understand.
I want to create an array, square some elements, remove some 
elements from original array and add the squared ones to the 
original array:


import std.stdio, std.algorithm, std.array;

int[] arr;
foreach (i; 0..10)
arr ~= i;

writeln("Original array: ",arr);
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -- OK

auto arrMap = arr.filter!(x => x > 5).map!(x => x^^2);
writeln("arrMap: ", arrMap);
// [36, 49, 64, 81] -- OK

int[] toRemove = [1, 2, 9];
arr = arr.remove!(x => toRemove.canFind(x)).array;

writeln("Original array after removal: ", arr);
// [0, 3, 4, 5, 6, 7, 8] -- OK

arr ~= arrMap.array;

writeln("Original array after removal and concatenation: ", 
arr);

// [0, 3, 4, 5, 6, 7, 8, 64, 49, 64, 81] -- what?

The last result is not what I wanted. I would expect [0, 3, 4, 5, 
6, 7, 8] and [36, 49, 64, 81] concatenated into [0, 3, 4, 5, 6, 
7, 8, 36, 49, 64, 81], but something else is happening here.
It looks like arr = arr.remove!  is messing things up, but 
why?




Re: Partial arrays reclaimed?

2017-01-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, January 27, 2017 18:22:17 Nick Sabalausky via Digitalmars-d-learn 
wrote:
> Suppose an array is being used like a FIFO:
>
> ---
> T[] slice;
>
> // Add:
> slice ~= T();
>
> // Remove:
> slice = slice[1..$];
> ---
>
> Assuming of course there's no other references to the memory, as this
> gets used, does the any of the memory from the removed elements ever get
> GC'd?
>
> Also, if this is a long-running process, isn't there a potential danger
> in the array just marching through the address space and running out of
> room? (ie either running out of of continuous space, or hitting 0xFFF)

Given that a dynamic array refers to a block of memory, whether that's the
whole block of memory or just a portion of it, and that as you shrink the
array, it's just moving where the pointer refers to in the block, there's no
way that it can release the memory. A reallocation would have to take place
so that the elements would be copied to a new block of memory and the
dynamic array would then refer to the new block. And the only times that a
reallocation is going to take place are when you append to the array or when
you call reserve, and the array doesn't have enough capacity to grow in
place to fulfill that reserve.

So, if all you're doing is shrinking the array, then there's no way that any
of its memory is going to be freed. You'd pretty much have to dup it at some
point and assign the new array back to it if you want to make that possible.

- Jonathan M Davis



Re: Problems compiling sqlite-d

2017-01-29 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 29 January 2017 at 16:26:30 UTC, Nestor wrote:

dmd yourfile.d database.d sqlite.d

I have just tried your way and I get some errors:
 Error 42: Symbol Undefined 
_D4arsd8database3Row7opIndexMFkAyaiZAya


Are you sure you passed those two database.d and sqlite.d modules 
to the compiler?


Re: Problems compiling sqlite-d

2017-01-29 Thread Nestor via Digitalmars-d-learn

On Saturday, 28 January 2017 at 19:01:48 UTC, Adam D. Ruppe wrote:

On Friday, 27 January 2017 at 12:01:30 UTC, Nestor wrote:

Is there any other native D implementation of sqlite reader?


My sqlite.d and database.d from here can do it too:
https://github.com/adamdruppe/arsd

Just download those two files and compile them together with 
your file:


dmd yourfile.d database.d sqlite.d

However, my thing requires the C library, sqlite3, to be 
available already so it might not work out of the box for you 
either.


import arsd.sqlite;
auto db = new Sqlite("filename");
foreach(row; db.query("select * from foo"))
  writeln(row[0], row["name"]);


I have just tried your way and I get some errors:

OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
app.obj(app)
 Error 42: Symbol Undefined 
_D4arsd8database3Row7opIndexMFkAyaiZAya

app.obj(app)
 Error 42: Symbol Undefined 
_D4arsd8database3Row7opIndexMFAyaAyaiZAya

app.obj(app)
 Error 42: Symbol Undefined 
_D4arsd6sqlite6Sqlite6__ctorMFAyaiZC4arsd6sqlite6Sqlite

app.obj(app)
 Error 42: Symbol Undefined _D4arsd6sqlite6Sqlite7__ClassZ
app.obj(app)
 Error 42: Symbol Undefined 
_D4arsd8database8Database5queryMFAyaYC4arsd8database9ResultSet

app.obj(app)
 Error 42: Symbol Undefined _D4arsd6sqlite12__ModuleInfoZ
Error: linker exited with status 163184408


Re: Where do you get implib

2017-01-29 Thread Nestor via Digitalmars-d-learn

On Friday, 4 November 2011 at 16:31:30 UTC, Johannes Pfau wrote:
On http://www.digitalmars.com/download/freecompiler.html 
there's a link to this file: http://ftp.digitalmars.com/bup.zip


I think that's the implib you want?


I just tried implib with latest sqlite library def (windows x86) 
like this, and it crashed (however with the dll it seems to work 
just fine):


implib sqlite3.lib sqlite3.def




Re: D and Oracle Pro*C

2017-01-29 Thread Patrick Schluter via Digitalmars-d-learn

On Sunday, 29 January 2017 at 01:53:01 UTC, KP wrote:

Hi,

Is anyone using Oracle's Pro*C with D?

Thanks,
KP


Not yet, but would like to too.