Re: How can I clean array and prevent further reallocation if there's enough space already?

2021-02-07 Thread Jack via Digitalmars-d-learn

On Sunday, 7 February 2021 at 21:55:34 UTC, Adam D. Ruppe wrote:

On Sunday, 7 February 2021 at 21:40:12 UTC, Jack wrote:
I think it would be fine except it assumes the number of items 
of the array to doesn't grow, it rather overwritten new 
elements


from docs:

"Use this only when it is certain there are no elements in use 
beyond the array in the memory block. If there are, those 
elements will be overwritten by appending to this array."


That's referring to a case like this:

int[] a = [1, 2, 3, 4, 5];
int[] b = a[0 .. 3];

Normally, if you were to do

b ~= 6;

it would allocate a new array for b, leaving a alone.

a == [1, 2, 3, 4, 5];
b == [1, 2, 3, 6];

a.ptr != b.ptr because b got reallocated.



If you assumeSafeAppended there though, the b ~= 6 would reuse 
the remainder of the block.


b.assumeSafeAppend();
b ~= 6;

a == [1, 2, 3, 6, 5];
b == [1, 2, 3, 6];

a.ptr == b.ptr; // assumeSafeAppend meant no reallocation




So the "elements in use beyond the array in the memory block" 
are referring to the variable a in the example still having [4, 
5] at the end. Since that 4 gets overwritten by the 6, if you 
weren't prepared for this, it can be a surprising bug.


The docs also say this is "undefined behavior" simply because 
the 4 won't *always* get overwritten by the 6. well, in this 
case the 4 is always overwritten by the 6, but say I wanted to 
append [6, 7, 8], then it might reallocate anyway because the 
memory block is only big enough for two new elements, not 
three. If that happened, that 4 would stay put.


So the array is allowed to grow as much as it wants, just in 
some cases it will overwrite existing data and in other cases 
it will realloc a new slice to make room.


got it, thank you very much!


Re: How can I clean array and prevent further reallocation if there's enough space already?

2021-02-07 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 7 February 2021 at 21:40:12 UTC, Jack wrote:
I think it would be fine except it assumes the number of items 
of the array to doesn't grow, it rather overwritten new elements


from docs:

"Use this only when it is certain there are no elements in use 
beyond the array in the memory block. If there are, those 
elements will be overwritten by appending to this array."


That's referring to a case like this:

int[] a = [1, 2, 3, 4, 5];
int[] b = a[0 .. 3];

Normally, if you were to do

b ~= 6;

it would allocate a new array for b, leaving a alone.

a == [1, 2, 3, 4, 5];
b == [1, 2, 3, 6];

a.ptr != b.ptr because b got reallocated.



If you assumeSafeAppended there though, the b ~= 6 would reuse 
the remainder of the block.


b.assumeSafeAppend();
b ~= 6;

a == [1, 2, 3, 6, 5];
b == [1, 2, 3, 6];

a.ptr == b.ptr; // assumeSafeAppend meant no reallocation




So the "elements in use beyond the array in the memory block" are 
referring to the variable a in the example still having [4, 5] at 
the end. Since that 4 gets overwritten by the 6, if you weren't 
prepared for this, it can be a surprising bug.


The docs also say this is "undefined behavior" simply because the 
4 won't *always* get overwritten by the 6. well, in this case the 
4 is always overwritten by the 6, but say I wanted to append [6, 
7, 8], then it might reallocate anyway because the memory block 
is only big enough for two new elements, not three. If that 
happened, that 4 would stay put.


So the array is allowed to grow as much as it wants, just in some 
cases it will overwrite existing data and in other cases it will 
realloc a new slice to make room.


Re: How can I clean array and prevent further reallocation if there's enough space already?

2021-02-07 Thread Jack via Digitalmars-d-learn

On Sunday, 7 February 2021 at 21:34:22 UTC, Adam D. Ruppe wrote:

On Sunday, 7 February 2021 at 21:31:11 UTC, Jack wrote:
assumeSafeAppend() wouldn't work in this case because I don't 
know the number of items that is going to be added to the 
array.


I don't think that matters. assumeSafeAppend seems appropriate 
for your need.


I think it would be fine except it assumes the number of items of 
the array to doesn't grow, it rather overwritten new elements


from docs:

"Use this only when it is certain there are no elements in use 
beyond the array in the memory block. If there are, those 
elements will be overwritten by appending to this array."




Re: How can I clean array and prevent further reallocation if there's enough space already?

2021-02-07 Thread Adam D. Ruppe via Digitalmars-d-learn

On Sunday, 7 February 2021 at 21:31:11 UTC, Jack wrote:
assumeSafeAppend() wouldn't work in this case because I don't 
know the number of items that is going to be added to the array.


I don't think that matters. assumeSafeAppend seems appropriate 
for your need.


How can I clean array and prevent further reallocation if there's enough space already?

2021-02-07 Thread Jack via Digitalmars-d-learn

How can I do that? I though something like this:

auto arr = [1, 2, 3, 4];
arr = arr[0 .. 0];
arr ~= 6; // does this cause reallocation?

assumeSafeAppend() wouldn't work in this case because I don't 
know the number of items that is going to be added to the array. 
I thought into setting the length property to 0. Does this free 
the memory block of the array? if it did reuse the existing 
memory block in further appends, would do what I want


Re: Is there a generic type such as void* or C#'s object?

2021-02-07 Thread Jack via Digitalmars-d-learn

Thanks for your answers guys!


GC.addRange in pure function

2021-02-07 Thread vitamin via Digitalmars-d-learn
Why using 'new' is allowed in pure functions but calling 
GC.addRange or GC.removeRange isn't allowed?


Need help for opencvd git submoduling

2021-02-07 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
I am wrapping opencv::cuda modules based on c/c++ wrapper files 
of gocv. This might be more of a git question. Can one link a 
folder from a repo to be a subfolder of another git repo?

Do I have any option other than submoduling the entire Go repo?

I want "https://github.com/hybridgroup/gocv/tree/release/cuda"; to 
be "https://github.com/aferust/opencvd/tree/master/c/cuda";


Re: Vibe.d diet template help

2021-02-07 Thread Arjan via Digitalmars-d-learn

On Sunday, 7 February 2021 at 00:05:51 UTC, Tim wrote:

Hi all,

I'm trying to render a diet template out to a WebSocket as a 
string to be inserted into a specific portion of the currently 
served page. Does anyone know how to go about this?


Is the websocket really needed? Otherwise a plain XHR would be 
advised which would Just implie another handler.