Re: Removing elements from dynamic arrays?

2022-04-05 Thread Salih Dincer via Digitalmars-d-learn
On Tuesday, 5 April 2022 at 14:10:44 UTC, Steven Schveighoffer 
wrote:

[...]
I'd implement it probably like this (for D2):

```d
auto drop(T)(ref T[] arr, T which)
{
   import std.algorithm, std.range;
   auto f = arr.find(which);
   debug if(f.empty) throw ...;
   auto result = arr.front;
   arr = arr.remove([0] - [0]); // god I hate this
   return result;
}
```


First, you're neglecting the loop.  Second, the returned value is 
the first element of the array. It should be as follows:


```d
auto drop2(T)(ref T[] arr, T which)
{
  //auto f = arr.find(which);
  auto f = arr.find!(a => a == which);

  //debug if(f.empty) throw ...;
  auto result = f.front; // arr.front;

  //arr = arr.remove([0] - [0]);
  arr = remove!(a => a == which)(arr);

  return result;
}
```
We do not know the wishes of the programmer who made the 
development. But it is certain that he scanned until the end of 
the array.  So it doesn't make sense for the resulting output to 
be equal to the ```which```. In summary, one line is enough:


```d
return remove!(a => a == which)(arr);
```
SDB@



Re: Removing elements from dynamic arrays?

2022-04-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/5/22 11:43 AM, Paul Backus wrote:

On Tuesday, 5 April 2022 at 14:10:44 UTC, Steven Schveighoffer wrote:

I'd implement it probably like this (for D2):

```d
auto drop(T)(ref T[] arr, T which)
{
   import std.algorithm, std.range;
   auto f = arr.find(which);
   debug if(f.empty) throw ...;
   auto result = arr.front;
   arr = arr.remove([0] - [0]); // god I hate this
   return result;
}
```


I think you can get rid of the ugly pointer arithmetic using `countUntil`:

```d
auto drop(T)(ref T[] arr, T which)
{
     import std.algorithm, std.range, std.exception;

     auto i = arr.countUntil(which);
     debug enforce(i < arr.length, "Not found");
     auto result = arr[i];
     arr = arr.remove(i);
     return result;
}
```


Yeah, or use enumerate. But it's painful:

```d
auto f = arr.enumerate.find!((v, w) => v[1] == w)(which);
auto result = f.front[1];
arr = arr.remove(result[0]);
return result;
```

I have a lib somewhere which isn't complete that allows remembering 
indexes for elements so you can tease out the original index, but it 
breaks when you use it on strings (of course).


-Steve


Re: Removing elements from dynamic arrays?

2022-04-05 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 5 April 2022 at 14:10:44 UTC, Steven Schveighoffer 
wrote:

I'd implement it probably like this (for D2):

```d
auto drop(T)(ref T[] arr, T which)
{
   import std.algorithm, std.range;
   auto f = arr.find(which);
   debug if(f.empty) throw ...;
   auto result = arr.front;
   arr = arr.remove([0] - [0]); // god I hate this
   return result;
}
```


I think you can get rid of the ugly pointer arithmetic using 
`countUntil`:


```d
auto drop(T)(ref T[] arr, T which)
{
import std.algorithm, std.range, std.exception;

auto i = arr.countUntil(which);
debug enforce(i < arr.length, "Not found");
auto result = arr[i];
arr = arr.remove(i);
return result;
}
```


Re: Removing elements from dynamic arrays?

2022-04-05 Thread Steven Schveighoffer via Digitalmars-d-learn

On 4/4/22 7:15 PM, Enjoys Math wrote:

https://forum.dlang.org/post/eih04u$1463$1...@digitaldaemon.com

A version of the code that takes `T which` as a parameter instead of 
`int index`.


```
// remove an item from an array
template drop(T)
{
   T drop( inout T[] arr, T which )


Note to reader: this is D1 code, so `inout` was equivalent to `ref`.


   {
     int i;
     T result;

     for (i=0; i < arr.length; i++)
     {
     if (arr[i] == which)
     {
     result = arr[i];
     break;
     }
     }

     debug if ( which >= arr.length)


I think this is a bug, it should be `i` you are testing.

     throw new Exception(str.format("Attempt to drop the %s of value 
%s from an array, but it was not found.", typeid(T), which));

     }


this looks like a stray brace?



     for (; i < arr.length; i++)
     {
     arr[i] = arr[i + 1];
     }
     arr.length = arr.length - 1; >
     return result;
   }
}
```

It has worse case complexity O(2n) = O(n) whereas the other one can run 
in half as long minimally and sometimes just as long (but still O(n)), 
but usually one needs to linear search for the entry first.


No, it's O(n) straight up, you are only iterating the entire array once.

As others mentioned, std.algorithm.remove can do this now. I'm not 
entirely sure of the benefit of returning the thing you tried to remove, 
though I suppose if the parameter defines equality with some extra data 
that isn't considered, maybe that does help you see what was removed?


I'd implement it probably like this (for D2):

```d
auto drop(T)(ref T[] arr, T which)
{
   import std.algorithm, std.range;
   auto f = arr.find(which);
   debug if(f.empty) throw ...;
   auto result = arr.front;
   arr = arr.remove([0] - [0]); // god I hate this
   return result;
}
```

Still O(n).

-Steve


Re: Check if Key exists in Associative Array using D language.

2022-04-05 Thread BoQsc via Digitalmars-d-learn

On Tuesday, 5 April 2022 at 11:53:19 UTC, Dennis wrote:

On Tuesday, 5 April 2022 at 11:26:27 UTC, BoQsc wrote:
I'd like to know if there is similar function: that can check 
if a **key** inside a [Associative Array][2] can be found.


You can use the `in` operator for that:
https://dlang.org/spec/hash-map.html#testing_membership


This is brilliant, thank you.


Re: Check if Key exists in Associative Array using D language.

2022-04-05 Thread Dennis via Digitalmars-d-learn

On Tuesday, 5 April 2022 at 11:26:27 UTC, BoQsc wrote:
I'd like to know if there is similar function: that can check 
if a **key** inside a [Associative Array][2] can be found.


You can use the `in` operator for that:
https://dlang.org/spec/hash-map.html#testing_membership


Check if Key exists in Associative Array using D language.

2022-04-05 Thread BoQsc via Digitalmars-d-learn
I've found [std.algorithm: canFind][1] to be useful on a 
**regular arrays**.


I'd like to know if there is similar function: that can check if 
a **key** inside a [Associative Array][2] can be found.


[1]:https://dlang.org/phobos/std_algorithm_searching.html#.canFind
[2]:https://dlang.org/spec/hash-map.html


Re: How to use an existing D package in Visual D?

2022-04-05 Thread realhet via Digitalmars-d-learn

On Tuesday, 5 April 2022 at 09:57:29 UTC, Mike Parker wrote:

On Tuesday, 5 April 2022 at 09:26:54 UTC, realhet wrote:
You should compile the existing package as a library, then add 
the library file to the linker settings in VisualD.


Thank You for the fast help!

Currently I have my own build system which works similar to 
Delphi(native target):


You specify the main module and the package paths. Press F9. And 
the it AUTOMATICALLY discovers all the referenced module 
hierarchy and compiles it then finally producing an .exe.
It also does compiled .obj file caching, so only the modules I 
changed and the modules that using the changed modules will be 
recompiled.
I can do this module caching for myself, since the LDC compiler 
has the -allinstances flag.
I switched from LDC 1.20 to 1.28 and I love the new internal 
linker. It will eliminate a lot of MS linker specific hacking 
from my build tool. I can just give LDC a bunch of sources, 
objects, and libs. And it does all the job intelligently.


I'm talking about this if you want to peek: 
https://github.com/realhet/hetlib/blob/master/het/HLDC/BuildSys.d
It basically launches an LDC2 instance for each of the changed 
modules and calls the linker when all finished. A debug compile 
takes 15 sec for a 40KLOC project(hierarchically including a few 
packages too) if I only change the main D file.


Is this fast and light weight approach exists at all in 
VisualStudio?
I heard something about "incremental build" in VisualStudio, but 
I think even if it is working with D, it can't step over the 
static library boundaries that you mentioned I have top use for 
the packages.


Re: How to use an existing D package in Visual D?

2022-04-05 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 5 April 2022 at 09:26:54 UTC, realhet wrote:

Hello,

I have all my D packages in the c:\D\libs\ directory.
I added this path to the PropertyPages/Compiler/Additional 
Import Paths field.


In the project source file I imported a module from my package 
using "import het.utils;"

Also used a function from it.

The syntax highlighter worked good, and the compiler was able 
to build main.obj file.


But I can't find the het.utils.obj file nowhere. Also the 
linker is unable to link the things in it.


My question is that how can I add an external package the right 
way to my project in Visual D?


I'm planning to use that package from other projects too, so I 
want to keep it in one place on my HDD.


Thank You in advance!


Importing modules is only for showing the compiler which symbols 
are available for you to use. By default, it does not cause them 
to be compiled or linked. You should compile the existing package 
as a library, then add the library file to the linker settings in 
VisualD.


How to use an existing D package in Visual D?

2022-04-05 Thread realhet via Digitalmars-d-learn

Hello,

I have all my D packages in the c:\D\libs\ directory.
I added this path to the PropertyPages/Compiler/Additional Import 
Paths field.


In the project source file I imported a module from my package 
using "import het.utils;"

Also used a function from it.

The syntax highlighter worked good, and the compiler was able to 
build main.obj file.


But I can't find the het.utils.obj file nowhere. Also the linker 
is unable to link the things in it.


My question is that how can I add an external package the right 
way to my project in Visual D?


I'm planning to use that package from other projects too, so I 
want to keep it in one place on my HDD.


Thank You in advance!