Re: InsertBefore in DList of structs

2019-03-05 Thread r-const-dev via Digitalmars-d-learn

On Tuesday, 5 March 2019 at 08:39:56 UTC, drug wrote:

On 05.03.2019 2:01, r-const-dev wrote:

[...]
dataPoints is an aggregate type variable, not a range and slice 
operator opSlice/[] returns a range of this aggregate type. I 
have no appropriate links unfortunately but these can be useful

https://dlang.org/phobos/std_container_dlist.html#.DList.opSlice - this 
operator returns a range
https://dlang.org/phobos/std_container_dlist.html#.DList.Range 
- this is

 range that allows to iterate over DList elements
http://ddili.org/ders/d.en/ranges.html - intro to ranges

[...]

`find` returns a range that starts from the key
`until` returns a range of all elements before the key
https://run.dlang.io/is/1kpOUx

[...]


Thank you for clarifications, it makes sense to me now.


Re: InsertBefore in DList of structs

2019-03-04 Thread r-const-dev via Digitalmars-d-learn

On Monday, 4 March 2019 at 10:09:19 UTC, drug wrote:

On 04.03.2019 13:03, drug wrote:
insertStable needs DList.Range, not Until!... one. You can do 
something like this https://run.dlang.io/is/A2vZjW
Oops, it was wrong example, I'd recommend this way - 
https://run.dlang.io/is/ugPL8j

```
import std.algorithm, std.container, std.stdio;

struct DataPoint {
immutable ulong time;
ulong value;
}

void main() {
auto dataPoints = DList!DataPoint([
DataPoint(10_000_000, 1),
DataPoint(30_000_000, 3),
DataPoint(40_000_000, 4),
]);

auto time = 30_000_000;
auto r = findSplit!((a, b) => a.time == b)(dataPoints[], 
[time]);


auto dp = dataPoints[].find(r[1].front);
writeln("before: ", dataPoints[]);
dataPoints.insertBefore(dp, DataPoint(20_000_000, 2));
writeln("after: ", dataPoints[]);
}

```


Thanks, seems that using dataPoints[] makes dataPoints usable as 
an range. How can I learn more about [] operator? I'm not sure I 
understand how is this documented in DList.


Find does the job! What's the difference between find and until?


InsertBefore in DList of structs

2019-03-04 Thread r-const-dev via Digitalmars-d-learn
I have a DList of structs, DataPoint, ordered by a struct field, 
time. I'm trying to insert a new entry preserving order, so I'm 
trying to use `until` to find the insertion point and 
`insertBefore` with the result.


struct DataPoint {
immutable ulong time;
ulong value;
}

void main() {
DList!DataPoint dataPoints;
void incrementAt(ulong time) {
auto dataPoint = until!(dp => dp.time >= 
time)(dataPoints);

if (dataPoint.time == time) {
++dataPoint.value;
} else {
dataPoints.insertBefore(dataPoint, DataPoint(time, 
1));

}
}
}

But I'm getting:

/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/searching.d(4898): Error: 
template instance `onlineapp.main.incrementAt.Until!(__lambda2, 
DList!(DataPoint), void)` does not match template declaration Until(alias pred, 
Range, Sentinel) if (isInputRange!Range)
onlineapp.d(14): Error: template instance 
`onlineapp.main.incrementAt.until!((dp) => dp.time >= time, 
DList!(DataPoint))` error instantiating


Re: Shared with synchronized

2019-03-03 Thread r-const-dev via Digitalmars-d-learn

On Sunday, 3 March 2019 at 22:35:54 UTC, r-const-dev wrote:
I'm trying to implement a thread safe class, guarding data 
access with synchronized and atomicOp.


Inside the class I'm using non-shared fields, such as Nullable, 
but I guarantee the thread safety using synchronized. How can I 
tell the compiler to allow using non-shared fields/methods 
inside synchronized?


Here is my example:

import core.atomic : atomicOp;
import std.typecons : Nullable;

class SharedObject {
private Object lock = new Object();
private Nullable!int sharedValue;
private int changeCount = 0;
synchronized void modifyValue(int newValue) {
sharedValue = newValue;
atomicOp!("+=")(changeCount, 1);
}
}

void main()
{
shared SharedObject data = new shared SharedObject();
data.modifyValue(3);
}

I get the error:
onlineapp.d(9): Error: template 
std.typecons.Nullable!int.Nullable.opAssign cannot deduce 
function from argument types !()(int) shared, candidates are:

/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(2884):
   std.typecons.Nullable!int.Nullable.opAssign()(T value)


Found a solution, hope it is a recommended one: cast `this` to 
non-shared and invoke an "unsafe" method. Inside everything is 
allowed:




import std.typecons : Nullable;

class SharedObject {
private Nullable!int sharedValue;
private int changeCount = 0;
synchronized void modifyValue(int newValue) {
(cast(SharedObject)this).unsafeModifyValue(newValue);
}
private void unsafeModifyValue(int newValue) {
sharedValue = newValue;
++changeCount;
}
}

void main()
{
shared SharedObject data = new shared SharedObject();
data.modifyValue(3);
}



Shared with synchronized

2019-03-03 Thread r-const-dev via Digitalmars-d-learn
I'm trying to implement a thread safe class, guarding data access 
with synchronized and atomicOp.


Inside the class I'm using non-shared fields, such as Nullable, 
but I guarantee the thread safety using synchronized. How can I 
tell the compiler to allow using non-shared fields/methods inside 
synchronized?


Here is my example:

import core.atomic : atomicOp;
import std.typecons : Nullable;

class SharedObject {
private Object lock = new Object();
private Nullable!int sharedValue;
private int changeCount = 0;
synchronized void modifyValue(int newValue) {
sharedValue = newValue;
atomicOp!("+=")(changeCount, 1);
}
}

void main()
{
shared SharedObject data = new shared SharedObject();
data.modifyValue(3);
}

I get the error:
onlineapp.d(9): Error: template 
std.typecons.Nullable!int.Nullable.opAssign cannot deduce 
function from argument types !()(int) shared, candidates are:
/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(2884): 
   std.typecons.Nullable!int.Nullable.opAssign()(T value)


Process Information

2019-02-23 Thread r-const-dev via Digitalmars-d-learn

Is there a way to get information about the current process?
Memory usage, CPU usage, PID.