Re: How to use sets in D?

2022-02-08 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Feb 08, 2022 at 10:02:30PM +, Paul Backus via Digitalmars-d-learn 
wrote:
[...]
> It also works if you use parentheses:
> 
> mySet[...] = (void[0]).init;

OT1H, nice that works! ... but OTOH, ugh, it looks so ugly. :-P  I think
I'll just stick with the alias.


T

-- 
Who told you to swim in Crocodile Lake without life insurance??


Re: How to verify DMD download with GPG?

2022-02-08 Thread Ola Fosheim Grøstad via Digitalmars-d-learn

On Tuesday, 8 February 2022 at 20:15:50 UTC, forkit wrote:

On what basis would you trust the key? Think about it ;-)


Oh well, seems like the keyring has nothing to do with trust. 
This model with no certification authority is annoying. Now I 
remember why I never bother with PGP.




Re: How to use sets in D?

2022-02-08 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 8 February 2022 at 21:58:49 UTC, H. S. Teoh wrote:
On Tue, Feb 08, 2022 at 09:47:13PM +, Paul Backus via 
Digitalmars-d-learn wrote: [...]
The `alias` and the `enum` just make the code a little nicer 
to read by letting you write `Unit` instead of `void[0]` and 
`unit` instead of `void[0].init`. You could get rid of them 
and the code would work exactly the same way; it'd just be a 
little bit uglier:


void[0][E] mySet;

mySet[...] = void[0].init;

[...]

Unfortunately, this doesn't work due to a syntax restriction 
(the parser isn't expecting a type name after the `=`, and will 
raise a syntax error). So the alias is in fact necessary.


Ah, yeah, I forgot about that bug.

It also works if you use parentheses:

mySet[...] = (void[0]).init;


Re: How to use sets in D?

2022-02-08 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Feb 08, 2022 at 09:47:13PM +, Paul Backus via Digitalmars-d-learn 
wrote:
[...]
> The `alias` and the `enum` just make the code a little nicer to read
> by letting you write `Unit` instead of `void[0]` and `unit` instead of
> `void[0].init`. You could get rid of them and the code would work
> exactly the same way; it'd just be a little bit uglier:
> 
> void[0][E] mySet;
> 
> mySet[...] = void[0].init;
[...]

Unfortunately, this doesn't work due to a syntax restriction (the parser
isn't expecting a type name after the `=`, and will raise a syntax
error). So the alias is in fact necessary.


T

-- 
Fact is stranger than fiction.


Re: How to use sets in D?

2022-02-08 Thread Paul Backus via Digitalmars-d-learn

On Tuesday, 8 February 2022 at 21:08:47 UTC, tastyminerals wrote:

https://forum.dlang.org/post/mailman.1072.1581112984.31109.digitalmars-d-le...@puremagic.com

On Friday, 7 February 2020 at 22:03:00 UTC, H. S. Teoh wrote:
On Fri, Feb 07, 2020 at 07:37:08PM +, mark via 
Digitalmars-d-learn wrote:

[...]


bool[E] works just fine.

The bool does take up 1 byte, though, so if you're *really* 
want to optimize that away, you could do this:


alias Unit = void[0];
enum unit = Unit.init;

// Look, ma! A bona fide set!
Unit[E] mySet;

mySet[...] = unit;
mySet.remove(...);
... // etc.

[...]


Can you please explain what does `bool[E]` mean? And how does 
the code with aliasing void[0] and then using enum even work?


`bool[E]` means an [associative array][1] with keys of type `E` 
and values of type `bool`. Since a `bool` value takes up 1 byte 
in memory, using a `bool[E]` associative array to store a set of 
`E` values means that for every value in your set, you will have 
to allocate an extra 1 byte for the `bool` in addition to the 
bytes required for the `E` itself.


In most cases, 1 extra byte is not going to matter very much, so 
that's not a big deal. But if you really, really want to avoid 
allocating any extra bytes, you can use `void[0]` in place of 
`bool`, since a `void[0]` takes up 0 bytes in memory. (The `void` 
part has no special significance here--you could also use 
`int[0]` or `char[0]`, for example.)


The `alias` and the `enum` just make the code a little nicer to 
read by letting you write `Unit` instead of `void[0]` and `unit` 
instead of `void[0].init`. You could get rid of them and the code 
would work exactly the same way; it'd just be a little bit uglier:


void[0][E] mySet;

mySet[...] = void[0].init;
mySet.remove(...);
// etc.

The name "unit" comes from the concept of a [unit type][2] in 
theoretical computer science.


[1]: https://dlang.org/spec/hash-map.html
[2]: https://en.wikipedia.org/wiki/Unit_type


Re: How to use sets in D?

2022-02-08 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Feb 08, 2022 at 09:08:47PM +, tastyminerals via Digitalmars-d-learn 
wrote:
[...]
> > bool[E] works just fine.
> > 
> > The bool does take up 1 byte, though, so if you're *really* want to
> > optimize that away, you could do this:
> > 
> > alias Unit = void[0];
> > enum unit = Unit.init;
> > 
> > // Look, ma! A bona fide set!
> > Unit[E] mySet;
> > 
> > mySet[...] = unit;
> > mySet.remove(...);
> > ... // etc.
> > 
> > Or you can wrap void[0][E] in a nice user-defined type that gives
> > nice set-like syntax.  But IMO, this is all overkill, and adds
> > needless complexity. Just use bool[E] or std.container.rbtree. :-D
[...]
> Can you please explain what does `bool[E]` mean?

E is whatever key type you want to use. String, or integer ID, or
whatever you want to put in your set.


> And how does the code with aliasing void[0] and then using enum even
> work?

The alias is a workaround for a syntactic limitation in D, because you
cannot write `mySet[someKey] = void[0].init;` without a syntax error. So
we use the alias to give `void[0]` a name that we can refer to in
assignment statements, so that we can assign its values to the AA.

As for `void[0]` itself, it's just a zero-element static array of an
indeterminate type. The whole point is to create something that occupies
0 bytes. The actual type doesn't actually matter; you could have used
int[0] and it'd work too.  (You can't use an empty struct because empty
structs have size 1.)  By using a zero-sized value type for the AA, we
effectively turn it into a set: it only stores keys.  Well, technically
it stores values of zero size too, but since it's zero-sized, it's as if
the value isn't there, and it incurs zero memory overhead. (Whereas an
empty struct would likely occupy an entire machine word, rounded up from
size 1.)

But as I said, this is overkill for something so trivial. Using
`bool[E]` or an RBTree works just fine.


T

-- 
Javascript is what you use to allow third party programs you don't know 
anything about and doing you know not what to run on your computer. -- Charles 
Hixson


How to use sets in D?

2022-02-08 Thread tastyminerals via Digitalmars-d-learn

https://forum.dlang.org/post/mailman.1072.1581112984.31109.digitalmars-d-le...@puremagic.com

On Friday, 7 February 2020 at 22:03:00 UTC, H. S. Teoh wrote:
On Fri, Feb 07, 2020 at 07:37:08PM +, mark via 
Digitalmars-d-learn wrote:

[...]


bool[E] works just fine.

The bool does take up 1 byte, though, so if you're *really* 
want to optimize that away, you could do this:


alias Unit = void[0];
enum unit = Unit.init;

// Look, ma! A bona fide set!
Unit[E] mySet;

mySet[...] = unit;
mySet.remove(...);
... // etc.

Or you can wrap void[0][E] in a nice user-defined type that 
gives nice set-like syntax.  But IMO, this is all overkill, and 
adds needless complexity. Just use bool[E] or 
std.container.rbtree. :-D



T


Can you please explain what does `bool[E]` mean? And how does the 
code with aliasing void[0] and then using enum even work?


Re: How to verify DMD download with GPG?

2022-02-08 Thread forkit via Digitalmars-d-learn
On Tuesday, 8 February 2022 at 10:17:19 UTC, Ola Fosheim Grøstad 
wrote:
I don't use GPG often, so I probably did something wrong, and 
failed to get a trusted verification. I do like the idea that a 
hacker cannot change the signature file if gaining access to 
the web/file hosts, but how to verify it in secure way?

I also did not find the key listed here:

https://dlang.org/download.html


there are two parts to this gpg output:

(1)
"Good signature.." - ok. you can be sure the file is correctly 
signed.


(2)
"WARNING: This key is not certified with a trusted .." - ok. You 
have not fully trusted the key, that's fine, and makes sense, 
since you just downloaded the key, and the key itself might have 
been tampered with .. in which case you have a good signature 
from a fraudulent key.


On what basis would you trust the key? Think about it ;-)

btw. the key is listed there - not sure what you mean.



Re: Is this Windows Win32 fileapi.h header accessible in D language?

2022-02-08 Thread BoQsc via Digitalmars-d-learn

On Tuesday, 8 February 2022 at 18:21:46 UTC, duser wrote:

On Tuesday, 8 February 2022 at 16:10:19 UTC, BoQsc wrote:
Unsure where to start, so I decided to ask here how to get use 
of this win32 header.


https://docs.microsoft.com/en-us/windows/win32/api/fileapi/


the specific module containing that is 
`core.sys.windows.winbase`


my trick to find these is to use github search with the 
function you want:


  https://github.com/dlang/druntime/search?q=AreFileApisANSI


This is great.
Thanks for now.


Re: Is this Windows Win32 fileapi.h header accessible in D language?

2022-02-08 Thread duser via Digitalmars-d-learn

On Tuesday, 8 February 2022 at 16:10:19 UTC, BoQsc wrote:
Unsure where to start, so I decided to ask here how to get use 
of this win32 header.


https://docs.microsoft.com/en-us/windows/win32/api/fileapi/


the specific module containing that is `core.sys.windows.winbase`

my trick to find these is to use github search with the function 
you want:


  https://github.com/dlang/druntime/search?q=AreFileApisANSI


Re: Is this Windows Win32 fileapi.h header accessible in D language?

2022-02-08 Thread Adam D Ruppe via Digitalmars-d-learn

On Tuesday, 8 February 2022 at 16:10:19 UTC, BoQsc wrote:
Unsure where to start, so I decided to ask here how to get use 
of this win32 header.


https://docs.microsoft.com/en-us/windows/win32/api/fileapi/


There is this for all the upstream things:
https://github.com/rumbu13/windows-d


But for those functions, you can get through `import 
core.sys.windows.windows;` i think the name "fileapi.h" is a bit 
new but the functions aren't.


Is this Windows Win32 fileapi.h header accessible in D language?

2022-02-08 Thread BoQsc via Digitalmars-d-learn
Unsure where to start, so I decided to ask here how to get use of 
this win32 header.


https://docs.microsoft.com/en-us/windows/win32/api/fileapi/


How to verify DMD download with GPG?

2022-02-08 Thread Ola Fosheim Grøstad via Digitalmars-d-learn
I don't use GPG often, so I probably did something wrong, and 
failed to get a trusted verification. I do like the idea that a 
hacker cannot change the signature file if gaining access to the 
web/file hosts, but how to verify it in secure way?


I did this:

```
/opt/local/bin/gpg --keyring ./d-keyring.gpg --verify 
dmd.2.098.1.osx.tar.xz.sig dmd.2.098.1.osx.tar.xz

gpg: Signature made søn 19 des 22:35:47 2021 CET
gpg:using RSA key 
3AAF1A18E61F6FAA3B7193E4DB8C5218B9329CF8

gpg: Good signature from "Martin Nowak " [unknown]
gpg: aka "Martin Nowak 
" [unknown]
gpg: aka "Martin Nowak " 
[unknown]

gpg: WARNING: This key is not certified with a trusted signature!
gpg:  There is no indication that the signature belongs 
to the owner.
Primary key fingerprint: F46A 10D0 AB44 C3D1 5DD6  5797 BCDD 73FF 
C3EB 6146
 Subkey fingerprint: 3AAF 1A18 E61F 6FAA 3B71  93E4 DB8C 5218 
B932 9CF8

```

I also did not find the key listed here:

https://dlang.org/download.html