Simplest way to convert an array into a set

2023-02-13 Thread Matt via Digitalmars-d-learn
Obviously, there is no "set" object in D, but I was wondering 
what the quickest way to remove duplicates from an array would 
be. I was convinced I'd seen a "unique" method somewhere, but 
I've looked through the documentation for std.array, 
std.algorithm AND std.range, and I've either missed it, or my 
memory is playing tricks on me. That, or I'm looking in the wrong 
place entirely, of course


A potential use-case for template mixins?

2023-02-06 Thread Matt via Digitalmars-d-learn
I am (still) writing a 3D graphics engine, and was considering my 
choices for a scene graph.


Originally, I was going to use classes derived from my base 
SceneNode class for the nodes of the graph, but would this 
instead be a better situation to use template mixins instead of 
potentially having to include a lot of information in the base 
class? Or should I go with the traditional "everything is derived 
from the base class"?


I understand if this is light on details, I'll try and provide 
more if necessary, but I'm really not far into the design beyond 
this, and was wondering which would make for an easier path in 
the future


How to write a library

2023-01-21 Thread Matt via Digitalmars-d-learn
I am trying to write a graphics engine for my university capstone 
project, and really wanted to give it a try in D, as both a 
talking point, and because I love the language. I'm using dub to 
build the library, and the demo application that'll use it.


However, I've come across a problem. In C/C++, when you build a 
library, you compile and link the source, then provide the header 
files for the library user to include. I have built the library, 
but what is the D equivalent to header files, and what do I have 
to do to prepare and use my library in another project?


Re: Why does choose not work here

2019-08-01 Thread Matt via Digitalmars-d-learn

On Thursday, 1 August 2019 at 21:12:51 UTC, ag0aep6g wrote:
`choose`'s parameters aren't lazy. So the second argument is 
evaluated even when `previous is null`. That means 
`intervalRange` is called on a null `previous`. And that fails, 
of course, because `intervalRange` can't access `starts` or 
`stops` when `this` is null.


I forgot about lazy parameters. I tried changes myFilters second 
parameter to lazy, but that didn't help. So I guess I'm stuck 
with the "if" version using the ForwardRange interface (version 
2)? Anyone have any other thoughts?


Why does choose not work here

2019-08-01 Thread Matt via Digitalmars-d-learn
I'm having some trouble with a "Program exited with code 
-1073741819" error in some code I'm writing and I would 
appreciate any help/insight.


The problem stems from some incompatibility between the Phobos 
function "choose" and the template function "myFilter" which 
returns a range. The code below is a simplified version of what 
I'm trying to do. myFilter is a dummy/stand-in function that 
highlights the error in my program (i.e. My function is not 
simply a filter; it is much more complicated but unnecessary to 
highlight my problem). In PairedA, sometimes previous will be 
null. I need the uniqIntervals member function to do the right 
thing depending on if previous is null or not. In an ideal 
situation uniqIntervals would return a forward range where the 
work would be done in a lazy fashion.


Version 1 works but is not lazy and does not use choose.
Version 2 works but requires converting the ranges to 
ForwardRange classes, (I'd prefer to not use an unnecessary level 
of indirection).
Version 3 uses the Phobos function filter which works inside 
choose (but there is no Phobos function that can actual do why 
myFilter stands in for. It's just interesting that the Phobos 
function works where myFilter doesn't as they are both template 
functions.
Version 4 does not work when PairedA.previous is null. I'd love 
to understand why.


I'd also love to some one to show me what the best way to do this 
would be. If version 2 is the best I can do, I'll live with it.


Thanks so much, the code is below:

auto myFilter(R1, R2)(R1 a, R2 b)
{
import std.algorithm : filter, canFind;
return a.filter!(c => b.canFind(c));
}

struct A
{
uint[] starts, stops;

import std.range : ForwardRange, inputRangeObject;
import std.typecons : Tuple;
ForwardRange!(Tuple!(uint,uint)) intervalRange() @property
{
import std.algorithm : map;
import std.range : zip;
import std.typecons : tuple;
		return zip(starts,stops).map!(a => 
tuple(a[0],a[1])).inputRangeObject;

}
}

struct PairedA
{
//version 1
// auto uniqIntervals() @property
// {
//  import std.array : array;
//  if (previous is null) return primary.intervalRange.array;
//  return primary.intervalRange
//  .myFilter(previous.intervalRange).array;
// }

//version 2
// import std.range : ForwardRange, inputRangeObject;
// import std.typecons : Tuple;
// ForwardRange!(Tuple!(uint,uint)) uniqIntervals() @property
// {
	// 	if (previous is null) return 
primary.intervalRange.inputRangeObject;

//  return primary.intervalRange
//  .myFilter(previous.intervalRange).inputRangeObject;
// }

//version 3
// auto uniqIntervals() @property
// {
//  import std.range : choose;
//  import std.algorithm : filter, canFind;
//  return choose(previous is null,
//  primary.intervalRange,
//  primary.intervalRange
//  .filter!(a => 
previous.intervalRange.canFind(a)));
// }

//version 4
auto uniqIntervals() @property
{
import std.range : choose;
import std.algorithm : filter, canFind;
return choose(previous is null,
primary.intervalRange,
primary.intervalRange
.myFilter(previous.intervalRange));
}

A primary;
A* previous;
}

unittest
{
uint[] startsA = [1,100,1000,1];
uint[] stopsA = [2,200,2000,2];
uint[] startsB = [1,100];
uint[] stopsB = [2,200];

auto a1 = A(startsA, stopsA);
auto a2 = A(startsB, stopsB);

auto p = PairedA(a1, );
auto p2 = PairedA(a1, null);

import std.stdio : writeln;
writeln(p.uniqIntervals);//always works
	writeln(p2.uniqIntervals);//Program exited with code -1073741819 
for version 4

}


Re: Why in Phobos is empty() sometimes const and sometimes not

2019-07-30 Thread Matt via Digitalmars-d-learn

On Tuesday, 30 July 2019 at 05:30:30 UTC, Jonathan M Davis wrote:
In principle, it's good to use const when you know that data 
isn't going to change, but that gets far more complicated when 
you're dealing with generic code or even with classes, since as 
soon as you use const, everything used with that template then 
needs to work with const, or in the case of classes, every 
derived class has to use const for their override of that 
function. Sometimes, that's fine, but that's usually when 
you're either requiring that const always work, or you're in 
control all of the code involved, so you know that you're not 
going to have to deal with issues like caching. It's issues 
like this that led us to decide a while ago that putting 
functions on Object was a mistake, since it locked all classes 
into a particular set of attributes (and even if we changed 
which attributes those were, it would still cause problems). 
The ProtoObject DIP (which would add a base class for Object 
that didn't have anything on it) will hopefully fix that, but 
that still hasn't been finalized yet.


In the case of ranges, on top of the general issues with const 
and generic code, their API just isn't designed with const in 
mind. Fundamentally, you need to be able to mutate a range to 
iterate through it. It would be different if we'd gone with 
more of a functional-style, head/tail solution where you have a 
function like head to get the first element, and a function 
like tail to return a range with the first element popped off, 
but for better or worse, that's not the direction we went. 
However, even if we had, issues like caching or delayed 
calculation would still come into play, and if you require that 
const work on something like empty, that prevents certain 
classes of solutions. Of course, on the flip side, without 
const, you don't know for sure that unwanted mutation isn't 
happening, but what would really be needed would be some kind 
of "logical" const rather than the full-on const we currently 
have, and that would be very difficult to implement. C++'s 
const _sort_ of allows that, because it has so many loopholes, 
but on the flip side, you lose most of the guarantees, and it 
mostly just becomes documentation of intent rather than 
actually enforcing logical constness. In practice, I find that 
D's const tends to not be terribly useful in generic code, but 
it's far more of a problem with libraries that are publicly 
available than with code where you control everything and can 
change stuff when you need to. This article I wrote goes into 
further detail about the issues with const in general:


http://jmdavisprog.com/articles/why-const-sucks.html

The situation with ranges would be improved if we had some kind 
of const or inout inference for templated code like we do with 
other attributes, but I don't know quite how that would work or 
what the downsides would be.


- Jonathan M Davis


That was a great article you wrote. Const has been one of the 
more difficult concepts for me to grasp when I moved from python 
to C++ and then to D. I also never understood immutable and the 
difference with const. Your article makes that really clear. 
Thanks for sharing.


Matt


Re: Why in Phobos is empty() sometimes const and sometimes not

2019-07-29 Thread Matt via Digitalmars-d-learn

On Monday, 29 July 2019 at 19:38:34 UTC, Jonathan M Davis wrote:
On Monday, July 29, 2019 11:32:58 AM MDT Matt via 
Digitalmars-d-learn wrote:






Because const ranges are basically useless, there really isn't 
much point in putting const on any range functions even if it 
would work for that particular range, and if a range is a 
wrapper range, the only way that it could do it would be if it 
used static if to make the code differ depending on whether the 
range it's wrapping will work if that function is const, which 
essentially means duplicating a bunch of code for little to no 
benefit.


- Jonathan M Davis


This was super helpful, thanks so much. I thought it was good 
practice to label member functions const if they didn't/couldn't 
modify any data. Now I see the reality is different for ranges. 
Not worrying about const for these seems simpler than code 
repetition.


Thanks again.



Why in Phobos is empty() sometimes const and sometimes not

2019-07-29 Thread Matt via Digitalmars-d-learn
I've noticed that for some ranges in Phobos empty is marked const 
(e.g. iota) but for other ranges (e.g. multiwayMerge) it is not 
const. Is there a reason why? Isn't empty guaranteed not to alter 
the data of the range and so should be const?


This is causing me considerable headaches as I try to write my 
own ranges that accept other ranges and have it all work for the 
general case. Any advice would be welcome.


Re: D generates large assembly for simple function

2018-01-27 Thread Matt via Digitalmars-d-learn

Godbolt link: https://godbolt.org/g/t5S976


D generates large assembly for simple function

2018-01-27 Thread Matt via Digitalmars-d-learn
Playing around with Godbolt, D seems to generate an embarassing 
amount of assembly for a simple function (50ish for squaring an 
int vs 4 for C++ and 7 for Rust). Even Go compiles to less 
assembly.


Is there something I'm missing?


Randomisation of array order

2015-08-02 Thread Matt via Digitalmars-d-learn
I was planning to use a dynamic array of indices to represent a 
deck of cards, and was wondering if there was any easy way to 
shuffle the arrays contents? I checked the library docs, but 
came to the conclusion that sorting arrays is a much more common 
operation :)


If anyone has a suggestion on a good way to implement this, I'd 
appreciate it. I don't need you to code it for me ( although I 
wouldn't turn it down if you've already done it), just a 
suggestion of what to do would be appreciated


Re: Randomisation of array order

2015-08-02 Thread Matt via Digitalmars-d-learn

...And then I realised that I hadn't looked inside std.random.

Question solved, because I am a dumbass.


Removing structs from assoc array while iterating over it

2015-03-16 Thread Matt via Digitalmars-d-learn
I am creating a simple SDL_Texture manager, and was wondering if 
the following code would work:


---
// 'list' is am assoc array of a struct containing a pointer and 
ref counter.

// max_list_length is set to 20 if it's ever found to be 0

// compact list
if(list.length == max_list_length){
string to_remove;

foreach(string st, entry; list){
if(to_remove.length){
list.remove(to_remove);
to_remove = ;
}
if(entry.ref_count == 0){
to_remove = st;
}
}

if(to_remove.length){
list.remove(to_remove);
to_remove = ;
}
}

---

I previously kept track of the keys of all entries found to have 
a ref_count of 0, and ran over those to remove them in a while 
loop, but was wondering if this would work, only needing to run 
over the list once. If there's a faster, cleaner way to do this, 
please do explain.


Many thanks in advance.


Re: embedding Pyd in Windows program

2015-03-14 Thread Matt via Digitalmars-d-learn

On Saturday, 14 March 2015 at 00:28:59 UTC, Ellery Newcomer wrote:

On Friday, 13 March 2015 at 19:05:59 UTC, Matt wrote:

example code, see if I can figure it out, but if you can 
advise, that would be fantastic. Thank you for all the help 
so far, it's really been appreciated


My penitence for not putting this information on readthedocs.



I've tried the hello.d example from GitHub, and get the error 
message:
  Error: template instance py_eval!string template 'py_eval' 
is not defined


Unfortunately, that's all I seem to get. Is py_eval not 
deprecated in 0.9.4?


hmm. that's odd. are you trying to build with dub? the only 
examples that build with dub are in examples/dub. the rest use 
distutils.


Yeah, dub is what I'm using. Actually, I made a mistake regarding 
the py_eval. I'm including the pyd modules in a module other than 
the one that has my main() function, so they weren't visible. I 
feel like a proper idiot for that one..


So I set up a separate project to test the samples exactly. The 
simple_embedded sample seems to work no problem.


I haven't gotten round to trying the shared_embedded, yet, but I 
will do at some point.


Does pyd have any documentation, or are we working from the 
source for understanding the functions?


Re: embedding Pyd in Windows program

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 16:30:07 UTC, Matt wrote:


Thank you, adding the subConfigurations section to dub.json 
seems to allow the program to compile and run successfully.


However, to test the code, I first tried in the main program:

---

py_eval!string(import sys\nprint(sys.path));

---

and it didn't print anything. No error, the program just ran 
and closed.
Then, I removed that and added print(sys.path) to my site.py 
as you suggested (the sys module is already imported), and 
still the program runs without printing anything, so I'm a 
little confused by that. I'll probably try running some of the 
example code, see if I can figure it out, but if you can 
advise, that would be fantastic. Thank you for all the help so 
far, it's really been appreciated


I've tried the hello.d example from GitHub, and get the error 
message:
   Error: template instance py_eval!string template 'py_eval' is 
not defined


Unfortunately, that's all I seem to get. Is py_eval not 
deprecated in 0.9.4?


Re: OutputDebugString()

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 21:12:52 UTC, Robert M. Münch wrote:
Hi, I want to use the Windows OutputDebugString() which is not 
defined anywhere.


How do I declare such missing Windows API functions myself? And 
with which libaries do I then need to link? Does DMD contain 
all necessary Windows link libs or am I supposed to get them 
myself?


So, it's not clear what to do if a Windows API function is 
missing.


And, how can I contribute new declared ones to get them 
integrated into the standard distribution?


I'm pretty sure the correct way to do it is as follows:

---

import core.sys.windows.windows;

extern (Windows) OutputDebugString( LPCTSTR );
/// Add other externs here

---


Re: embedding Pyd in Windows program

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 01:40:34 UTC, Ellery Newcomer wrote:

On 03/11/2015 07:59 PM, Matt wrote:


Right, copying site.py into my program's working dir sorts out 
the

missing module error, but I now get a syntax error:

file=sys.stderr)
^
SyntaxError: invalid syntax
Error executing command run: Program exited with code 1

I googled this, and apparently it's caused by an older 
interpreter. The

only files I have transferred over are:
  python3.DLL
  python3.LIB
  python34.LIB

Does anyone have any idea what I might be doing wrong, or how 
I can fix

this?


I'm guessing your python path is screwed up. In your embedded 
python,


import sys
print (sys.path)

make sure everything is pointing where it should.

Failing that,

are you using the correct dub configuration? without checking, 
I believe it defaults to python 2.7.


Failing that,

you transferred python3.dll from somewhere to somewhere? I 
believe pyd links to \windows\system32\python3.dll or some 
such. At least, I think that's where those lib files point to. 
not sure. If that is an issue and you want to use your own lib 
files, you will need to generate OMF files from them.


I used the ~0.9.4 branch in dub, and I'm not sure how to 
change configuration. Do you mean I should be using ~master 
or ~develop?


as for the transferring the python3.dll, I downloaded and 
installed a fresh copy of the latest Python build, version 3.4, 
and copied the libraries from there. Even so, I had to add 
PYD_PACKAGE_DIR manually to get it to work.


I'm not sure I'm going to be able to run the python snippet you 
sent, since my program, which currently opens a window for three 
seconds before closing again, doesn't even get that far, and 
chokes when I run py_init().


Re: embedding Pyd in Windows program

2015-03-13 Thread Matt via Digitalmars-d-learn

On Friday, 13 March 2015 at 14:21:15 UTC, Ellery Newcomer wrote:

On Friday, 13 March 2015 at 09:38:45 UTC, Matt wrote:


I used the ~0.9.4 branch in dub, and I'm not sure how to 
change configuration. Do you mean I should be using 
~master or ~develop?


nope. configurations are a different thing.

you can set them in your project's dub.json. example:

https://github.com/ariovistus/pyd/blob/master/examples/dub/simple_embedded/dub.json

for your project, I think you'd want

subConfigurations: {
  pyd: python34,
}



as for the transferring the python3.dll, I downloaded and 
installed a fresh copy of the latest Python build, version 
3.4, and copied the libraries from there. Even so, I had to 
add PYD_PACKAGE_DIR manually to get it to work.


PYD_PACKAGE_DIR is necessary for linking the OMF files found in 
infrastructure/windows. hmm. why did it not Just Work?


I'm not sure I'm going to be able to run the python snippet 
you sent, since my program, which currently opens a window for 
three seconds before closing again, doesn't even get that far, 
and chokes when I run py_init().


you can put it in the top of site.py


Thank you, adding the subConfigurations section to dub.json 
seems to allow the program to compile and run successfully.


However, to test the code, I first tried in the main program:

---

py_eval!string(import sys\nprint(sys.path));

---

and it didn't print anything. No error, the program just ran and 
closed.
Then, I removed that and added print(sys.path) to my site.py as 
you suggested (the sys module is already imported), and still the 
program runs without printing anything, so I'm a little confused 
by that. I'll probably try running some of the example code, see 
if I can figure it out, but if you can advise, that would be 
fantastic. Thank you for all the help so far, it's really been 
appreciated


Re: embedding Pyd in Windows program

2015-03-11 Thread Matt via Digitalmars-d-learn

On Wednesday, 11 March 2015 at 19:32:05 UTC, Matt wrote:
I'm trying to build a simple platformer using SDL2 and python 
to script entities, but I'm struggling to include Pyd. I'm 
using DMD v2.066.1, with dub as the package manager, and 
derelict for the SDL2 bindings.


Now, when I add Pyd to my dub.json everything works fine, but 
as soon as I add py_init(), my code compiles and links fine, 
but I get ImportError: No module named site. Have I done 
something wrong? Can anyone shed some light on this? Or am I 
asking in the wrong place?


Any help would be very much appreciated, and I can provide code 
on request if that helps.


Well, apparently I didn't check the Python side of things. site 
is a python module that is loaded by the interpreter, meaning my 
python environment variables apparently haven't been set 
properly, despite the fact that my standalone interpreter can 
find them with no problem. I'm going to keep plugging away at 
this, see if there's any way to sort this out.


embedding Pyd in Windows program

2015-03-11 Thread Matt via Digitalmars-d-learn
I'm trying to build a simple platformer using SDL2 and python to 
script entities, but I'm struggling to include Pyd. I'm using DMD 
v2.066.1, with dub as the package manager, and derelict for the 
SDL2 bindings.


Now, when I add Pyd to my dub.json everything works fine, but as 
soon as I add py_init(), my code compiles and links fine, but I 
get ImportError: No module named site. Have I done something 
wrong? Can anyone shed some light on this? Or am I asking in the 
wrong place?


Any help would be very much appreciated, and I can provide code 
on request if that helps.


Re: embedding Pyd in Windows program

2015-03-11 Thread Matt via Digitalmars-d-learn

On Wednesday, 11 March 2015 at 21:45:20 UTC, Matt wrote:

On Wednesday, 11 March 2015 at 19:32:05 UTC, Matt wrote:
I'm trying to build a simple platformer using SDL2 and python 
to script entities, but I'm struggling to include Pyd. I'm 
using DMD v2.066.1, with dub as the package manager, and 
derelict for the SDL2 bindings.


Now, when I add Pyd to my dub.json everything works fine, but 
as soon as I add py_init(), my code compiles and links fine, 
but I get ImportError: No module named site. Have I done 
something wrong? Can anyone shed some light on this? Or am I 
asking in the wrong place?


Any help would be very much appreciated, and I can provide 
code on request if that helps.


Well, apparently I didn't check the Python side of things. 
site is a python module that is loaded by the interpreter, 
meaning my python environment variables apparently haven't been 
set properly, despite the fact that my standalone interpreter 
can find them with no problem. I'm going to keep plugging away 
at this, see if there's any way to sort this out.


Right, copying site.py into my program's working dir sorts out 
the missing module error, but I now get a syntax error:


file=sys.stderr)
^
SyntaxError: invalid syntax
Error executing command run: Program exited with code 1

I googled this, and apparently it's caused by an older 
interpreter. The only files I have transferred over are:

  python3.DLL
  python3.LIB
  python34.LIB

Does anyone have any idea what I might be doing wrong, or how I 
can fix this?


Re: Example usage of the core.sync classes

2015-01-03 Thread Matt via Digitalmars-d-learn

On Friday, 2 January 2015 at 18:52:11 UTC, Benjamin Thaut wrote:

Am 02.01.2015 um 19:45 schrieb Vlad Levenfeld:


My personal favorite method is to use the primitives in 
core.atomic with
a double or triple buffer. To double buffer, keep two buffers 
in an
array (data[][] or something) and an integer index that points 
to the
active buffer, then use atomicStore to update active buffer 
index when

you want to swap.
Triple buffering can improve runtime performance at the cost 
of more
memory, and in this case you will need two indices and two 
swap methods,
one for the producer and another for the consumer. I use cas 
with a

timed sleep to update the indices in this case.

Take it with a grain of salt, I'm far from a pro, but this has 
worked
well for me in the past. I can post some example code if you 
need it.


I have to agree with Vlad here. The usual way is to do double 
buffering. So you have two buffers which hold all the data that 
the physics simulation passes to the game logic. While the 
physics simulation is computing the next frame, the game logic 
can use the results from the last frame. The only point where 
sinchronisation is neccessary is when swapping the buffers. You 
have to ensure that both the game logic and the physics thread 
are done with their current buffer and then sawp them. The only 
downside of this approach is, that you will get a small delay 
(usually the time taken for 1 frame) into the data you pass 
this way. Sometimes this approach is called the exractor 
pattern. I use it to pass data from the game simulation to the 
renderer, so the renderer can render in paralell with the game 
simulation computing the next frame. You can find a example of 
my double buffered solution here:

https://github.com/Ingrater/Spacecraft/blob/master/game/sources/renderer/extractor.d

I had tripple buffering up and running at some point, but I 
went back to double buffering, because tripple buffering can 
cause micro lags and you don't want that.


Kind Regards
Benjamin Thaut


Right, I've been looking at core.atomic, but it has very little 
documentation, and it's territory I haven't explored, yet. Any 
chance of some pointers along the way?


Re: Example usage of the core.sync classes

2015-01-03 Thread Matt via Digitalmars-d-learn

On Saturday, 3 January 2015 at 22:10:49 UTC, Vlad Levenfeld wrote:

On Saturday, 3 January 2015 at 15:44:16 UTC, Matt wrote:
Right, I've been looking at core.atomic, but it has very 
little documentation, and it's territory I haven't explored, 
yet. Any chance of some pointers along the way?


Could you be more specific about what you need help 
understanding? You can implement a very simple double buffer 
like so:


  synchronized final class DoubleBuffer (T) {
T[][2] data;
uint write_index;

shared void swap () {
  atomicStore (write_index, (write_index + 1) % 2);
  (cast()this).buffer[write_index].clear;
}

shared @property write_buffer () {
  return (cast()this).buffer[write_index][];
}
shared @property read_buffer () {
  return (cast()this).buffer[(write_index + 1) % 2][];
}
  }

Just append to write_buffer, call swap, then read from 
read_buffer. Make sure you declare the instance shared. 
Benjamin's post links to a more robust and realistic 
implementation.


What I mean is that I don't understand what atomicStore, 
atomicLoad, etc. actually DO, although in the case of the two 
mentioned, I can hazard a pretty good guess. The documentation 
doesn't exist to tell me how to use the functions found in the 
module. I've never used any atomic functions in any language 
before.


However, thank you for the simple double buffered example, I do 
appreciate it. How would another thread gain access to the 
instance in use, though? Am I able to pass references to 
instances of this class via events? 'synchronized' is also 
something I'm clueless about, and again the D documentation 
doesn't help, as the section in the class documentation basically 
says synchronized classes are made of synchronized functions, 
without explaining what a synchronized function IS. The function 
section makes no mention of the synchronized keyword, either.


Sorry, it feels like there's a load of stuff in the library and 
language to make multithreading easier, but with little to no 
explanation, so it feels like you're expected to already know it, 
unless I'm looking in the wrong places for my explanations. If I 
am, please do give me a kick, and point me in the right direction.


Example usage of the core.sync classes

2015-01-02 Thread Matt via Digitalmars-d-learn
I'm trying to write a small 3D engine, and wanted to place the 
physics in a separate thread to the graphics, using events, 
possibly std.concurrency, to communicate between them.


How, then, do I pass large amounts of data between threads? I'm 
thinking the physics world state (matrix for each object, object 
heirarchies, etc) being passed to the graphics thread for 
rendering.


I'd assumed that I would use Mutex, or ReadWriteMutex, but I have 
no idea how to build code using these classes, if this is even 
the right way to go about this.


I would really appreciate any pointers you can give.


Many thanks


Exception thrown while trying to read file

2014-10-03 Thread Matt via Digitalmars-d-learn
I am building a PE-COFF file reader, just for education purposes, 
and I keep getting a ConvException come up, stating:


---

  Unexpected '

---

Now, I have no idea how I'm getting this. The code at that point 
looks like the following:


---

  // look for the identifier
  // this gives us the offset to the magic number
  uint offs;
  file.seek(0x3c, SEEK_SET);

  file.readf(%d, offs); // this is the problem line

  writefln(magic number offset: %d, offs);
  file.seek(offs, SEEK_SET);

---

With the exception being raised on the writefln. I have the same 
trouble with


  writeln(magic number offset: , offs);

and I'm not sure how to go about fixing it. About the only clue I 
can come up with, is that if I were to look at the section of 
code in the hex editor, the number at that point generates a ', 
the offending character, when represented as a character array.


Does anyone else see whatever it is that I'm doing wrong?


Re: Exception thrown while trying to read file

2014-10-03 Thread Matt via Digitalmars-d-learn

On Friday, 3 October 2014 at 13:48:41 UTC, thedeemon wrote:

On Friday, 3 October 2014 at 11:30:02 UTC, Matt wrote:

I am building a PE-COFF file reader
 file.seek(0x3c, SEEK_SET);
 file.readf(%d, offs); // this is the problem line
Does anyone else see whatever it is that I'm doing wrong?


readf is for reading text, it expects to see some digits. 
You're reading a binary file, not a text, so no ASCII digits 
are found in that place. Use rawRead instead of readf.


much obliged, many thanks


Loading Symbols from Loaded Libraries

2014-09-01 Thread Matt via Digitalmars-d-learn
If I were to use Runtime.loadLibrary(), it claims to merge any D 
GC in the shared lib with the current process' GC.


Do I need to version away symbol loading code for each platform?
Or is there already code in Phobos that allows us to do this 
independent of platform?


mismatched function return type inference of string and double

2014-08-03 Thread matt via Digitalmars-d-learn
So I just started learning D this weekend, and I'm trying to 
understand why the following example will not work... Essentially 
what I am trying to do is cast the templated child stored in the 
parent container as the correct form, return some T value, and 
use auto to return the correct value type.. It seems from the 
errors I get regarding mismatched return type inference of string 
and double, that the compiler, for whatever reason, is assuming 
auto is a double?


int main(string[] argv)
{
Parent[] Array;
Array ~= new Child!(double)(3.0);
Array ~= new Child!(string)(text);
foreach(item;Array)
{
auto val = value(item,item.type);
//writeln(std.conv.to!string(value(item,item.type)));
}
return 0;
}
auto value(Parent item, int type)
{
if(item.type == 1)
{
return Fun!double(cast(Child!double)item);
}
else if(item.type==2)
return Fun!string(cast(Child!string)item);
else
return 0;
}
auto Fun(T)(Child!T c)
{
return c.Value;
}

public class Parent
{
int type=-1;
}
public class Child(T) : Parent
{
T value;
public this(T value)
{
this.value=value;
SetType();
}
private void SetType()
{
if(typeid(T) == typeid(int))
type=0;
if(typeid(T) == typeid(double))
type=1;
if(typeid(T) == typeid(string))
type=2;
}
@property public T  Value(){return value;}
}


Re: Source File and Position of User Defined Type

2014-06-11 Thread Matt via Digitalmars-d-learn

On Tuesday, 10 June 2014 at 20:58:41 UTC, Nordlöw wrote:
Is there a way to, programatically (trait), lookup the source 
file and position of a user defined type either dynamically or, 
even better, statically?


I don't know about the source file, per se, but std.traits has 
the fullQualifiedName!() template, which you could go on to parse 
for the package and module of a type. Or just use the packageName 
and moduleName templates instead.


Re: Basic dynamic array question. Use of new versus no new.

2014-06-10 Thread Matt via Digitalmars-d-learn

On Wednesday, 11 June 2014 at 02:30:01 UTC, WhatMeWorry wrote:

In Mr. Cehreli's book it says

Additionally, the length of dynamic arrays can be changed by 
assigning a value to this property:


int[] array; // initially empty
array.length = 5; // now has 5 elements

while in Mr. Alexandrescu's book, it says

To create a dynamic array, use a new expression (§ 2.3.6.1 on 
page 51) as follows:


int[] array = new int[20]; // Create an array of 20 integers


Could someone please compare and contrast the two syntaxes. I 
presume the new command places the 2nd array in heap memory.


I would have read the second as creating a static array of 20 
ints in heap memory, then assigning a dynamic array to point to 
it. The first one definitely does what you'd expect.


Wasn't Andres book based on D1.0? Or has he done another since 
then?


Re: problem with Access Violation, and I'm not sure where

2014-06-10 Thread Matt via Digitalmars-d-learn

On Wednesday, 11 June 2014 at 02:59:40 UTC, Matt wrote:
I previously asked this over in the DigitalMars.D 
board(http://forum.dlang.org/thread/huftyrtbomaimuqkm...@forum.dlang.org#post-hrqvqlzzbkgafvjdtjnb:40forum.dlang.org), 
but it was suggested I ask it over here instead.


I have the following code and, despite trying to pinpoint it 
with try-catch blocks, I keep getting an Access Violation, and 
I'm really not sure where.


I'm using dub to build, but I've tried manually building as 
well, and I just get the same problem comes up.


The only module in the program is as follows (with the wrapper 
modules ripped out, so it's straight calls);


---

module app;

// own modules
import derelict.sdl2.sdl;

// standard library modules
import std.json;
import std.file;
import std.conv;
import std.stdio;
import std.datetime;

void main (string[] args){
DerelictSDL2.load();
	if (!DerelictSDL2.isLoaded) assert(0, DerelictSDL2 failed to 
load!);

SDL_Init( SDL_INIT_EVERYTHING );

scope(exit) SDL_Quit();

SDL_Window* window;
JSONValue cfg;

try{
cfg = loadConfig;

auto width = to!int (cfg[window][width].integer);
auto height = to!int (cfg[window][height].integer);

		window = SDL_CreateWindow (cfg[window][caption].str.ptr, 
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, 
height, SDL_WINDOW_SHOWN);

}
catch(Error e){
writeln (error occurred before timing variable setup);
return;
}

// set up timing variables
	auto duration = TickDuration 
(cfg[graphics][ticks].integer);


auto current_tick = Clock.currAppTick;
auto target_tick = current_tick + duration;
auto target_margin = target_tick + (duration/2);

// main loop
try{
int*keys;
ubyte*  numkeys;

while (true){
SDL_PumpEvents();
numkeys = SDL_GetKeyboardState (keys);

if (keys[SDL_SCANCODE_LALT]  keys[SDL_SCANCODE_F4]){
SDL_DestroyWindow (window);
break;
}

current_tick = Clock.currAppTick;
if (current_tick = target_tick){
// this way, it will wait for the next frame, if there is 
less than half the time to the next frame left

if (current_tick  target_margin){
// update graphics and physics
}
// prep for next tick
target_tick += duration;
target_margin += duration;
}

}
}
catch(Error e){
writeln (error occurred during main loop);
}
}

---

If anyone can help me with this, I would very much appreciate 
it.


Apparently, it's in another part of my code, and I was doing 
something extremely silly. Ah well, live and learn.


Re: storing pointers in Variants

2014-04-27 Thread Matt via Digitalmars-d-learn

On Sunday, 27 April 2014 at 00:48:53 UTC, Ali Çehreli wrote:


I think the following is a start:

import std.variant;

class MyClass
{
Variant[string] store;

void attach(T)(string key, ref T var)
{
store[key] = var;
}

void set(T)(string key, T value)
{
*store[key].get!(T*) = value;
}

T get(T)(string key)
{
return *store[key].get!(T*)();
}
}

void main()
{
int sample = 42;

auto myObj = new MyClass;
myObj.attach(othername, sample);
myObj.set(othername, 69);

assert(myObj.get!int(othername) == 69);
assert(sample == 69);
}

Ali



Much obliged. Just working on preventing type errors, now. I'll 
let you know how it goes


storing pointers in Variants

2014-04-26 Thread Matt via Digitalmars-d-learn

I am trying to create a class that can do the following:

---

int sample = 42;

// myObj is an instance of the class I'm writing
myObj.attach (othername, sample);

myObj.set (othername, 69);

writeln (myObj.get (othername), \t, sample); // should 
produce '69  69'


---

Currently, my class appends to an array of Variants, and can 
store data internally in this array. Ref parameters don't work 
with variants, and pointers cause exceptions to be thrown. What 
would be the best way to go about this?


I had momentarily considered using a struct that stores a void 
pointer to the data, along with type info, and using this to 
replace the Variant, but I like the ability to change the type at 
runtime.


Any advice would be much appreciated.