Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
4acf163b42 | |||
e1fd528607 | |||
1c57368f43 | |||
|
1c5e18b92e | ||
|
07b388eecb | ||
|
20ae6465d6 | ||
728eaf88fb | |||
90797a48be | |||
5453c646f6 | |||
7dd4c44140 | |||
81b4fb88f5 | |||
db607f7602 | |||
cfcb1e727a | |||
c7bfbe0657 | |||
0155039071 | |||
c15a8993ec | |||
be8fcb3e1c | |||
d9fda61fe1 | |||
4f48544297 | |||
938a1bb5b4 | |||
b62cbb0647 | |||
2c21dc3429 | |||
f0d8c616bb | |||
60b0562311 | |||
92284c8541 | |||
0fcc83d00e | |||
f27f62b80a | |||
a227b58407 | |||
3ce5e8153c |
68
.travis.yml
68
.travis.yml
@ -1,68 +0,0 @@
|
||||
sudo: false
|
||||
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
|
||||
language: d
|
||||
|
||||
d:
|
||||
- dmd-2.091.1
|
||||
|
||||
env:
|
||||
global:
|
||||
- LATEST=2.091.1
|
||||
|
||||
matrix:
|
||||
- ARCH=x86_64
|
||||
- ARCH=x86
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- name: D-Scanner
|
||||
d: dmd-$LATEST
|
||||
env: DSCANNER=0.7.0
|
||||
os: linux
|
||||
- name: DDoc
|
||||
d: dmd-$LATEST
|
||||
env: DDOC=true
|
||||
os: linux
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- gcc-multilib
|
||||
|
||||
before_script:
|
||||
- if [ "`$DC --version | head -n 1 | grep v$LATEST`" ] &&
|
||||
[ -z "$DSCANNER$DDOC" ]; then
|
||||
export UNITTEST="unittest-cov";
|
||||
fi
|
||||
|
||||
script:
|
||||
- set -e;
|
||||
if [ -n "$DDOC" ]; then
|
||||
dub build :meta -b ddox --compiler=$DC;
|
||||
dub build :sys -b ddox --compiler=$DC;
|
||||
dub build :os -b ddox --compiler=$DC;
|
||||
dub build :encoding -b ddox --compiler=$DC;
|
||||
dub build :middle -b ddox --compiler=$DC;
|
||||
dub build :test -b ddox --compiler=$DC;
|
||||
dub build -b ddox --compiler=$DC;
|
||||
elif [ -z "$DSCANNER" ]; then
|
||||
dub test :meta -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
dub test :sys -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
dub test :os -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
dub test :encoding -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
dub test :middle -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
dub test :test -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
dub test -b ${UNITTEST:-unittest} --arch=$ARCH --compiler=$DC;
|
||||
else
|
||||
dub fetch dscanner --version=$DSCANNER;
|
||||
|
||||
FILES=$(find */tanya -type f);
|
||||
dub run dscanner -- --styleCheck $FILES;
|
||||
fi
|
||||
|
||||
after_success:
|
||||
- test "$UNITTEST" && bash <(curl -s https://codecov.io/bash) || true
|
@ -1,5 +0,0 @@
|
||||
# Contributor Code of Conduct
|
||||
|
||||
This project adheres to No Code of Conduct. We are all adults. We accept anyone's contributions. Nothing else matters.
|
||||
|
||||
For more information please visit the [No Code of Conduct](https://github.com/domgetter/NCoC) homepage.
|
107
CONTRIBUTING.md
107
CONTRIBUTING.md
@ -1,107 +0,0 @@
|
||||
|
||||
# Contributing
|
||||
|
||||
Tanya is a project in active development, therefore any help is appreciated. Thank you for considering contributing
|
||||
to it, feel welcome.
|
||||
These guidelines describe ways to get started.
|
||||
|
||||
|
||||
## Ways to get involved
|
||||
|
||||
* **Reporting a problem**: [Report](https://github.com/caraus-ecms/tanya/issues) bugs and usage problems you
|
||||
encounter.
|
||||
* **Fixing issues**: [The bug tracker](https://github.com/caraus-ecms/tanya/issues) contains a list of issues you
|
||||
can work on.
|
||||
* **Documentation**: You can improve API documentation by correcting grammar errors, completing existing texts and
|
||||
writing new ones, or providing usage examples.
|
||||
* **Testing**: Test coverage is important for a library. Writing tests is not only helpful, but is also a great way
|
||||
to get a feel for how tanya works.
|
||||
* **Adding new features**: Tanya is a growing library. If you think some feature is missing, you can suggest
|
||||
and implement this.
|
||||
|
||||
|
||||
## Opening an issue
|
||||
|
||||
If you have found a bug, an error, have some question, or suggestion,
|
||||
[Open an issue](https://github.com/caraus-ecms/tanya/issues). I'll try to answer as soon as I can. There is also a
|
||||
list of open issues that mirror the current development process and progress. If you're looking for a challenge, just
|
||||
pick an issue you are interested in and start working on it. Fill free to comment on the issue to get more
|
||||
information.
|
||||
|
||||
You can also look at the [milestones](https://github.com/Dlackware/gnome/milestones) to see what is planned for a
|
||||
specific release.
|
||||
|
||||
|
||||
## Contribution process
|
||||
|
||||
### Creating a pull request
|
||||
|
||||
I accept GitHub pull requests. Creating a pull request is like sending a patch with the suggested change.
|
||||
First you have to [fork](https://guides.github.com/activities/forking/) the repository. Clone your fork locally
|
||||
with `git clone` and create a new branch where you want to work. For example:
|
||||
|
||||
```shell
|
||||
git checkout -b bugfix-x
|
||||
```
|
||||
Commit your changes to your fork:
|
||||
|
||||
```shell
|
||||
git commit -m "Fix X"
|
||||
git push -u origin bugfix-x
|
||||
```
|
||||
|
||||
After that if you visit your fork on GitHub, GitHub will suggest to create pull request. Just follow the steps
|
||||
described on GitHub to finish the process. See
|
||||
[Using Pull Requests](https://help.github.com/articles/about-pull-requests/) for more information.
|
||||
|
||||
Please ensure that your fork is even with the upstream (original) repository. If not, you have to rebase your branch
|
||||
on upstream/master before submitting the pull request. See [Syncing a fork](https://help.github.com/articles/syncing-a-fork/) for a
|
||||
step-by-step guide.
|
||||
|
||||
### Fixing a bug
|
||||
|
||||
Add a unit test that demonstrates the bug along with a short description or link to the original bug.
|
||||
|
||||
### Adding new features
|
||||
|
||||
* Use Ddoc to document the feature.
|
||||
* Add some unit tests to prevent bugs.
|
||||
* [Documented D unit tests](https://dlang.org/spec/ddoc.html#using_ddoc_to_generate_examples) go into the documentation and can be used as an usage
|
||||
example. These tests should be readable and not complicated since they demonstrate how the feature is supposed to work.
|
||||
* More advanced tests should be put into a separate not documented unittest block.
|
||||
|
||||
### Writing unit tests
|
||||
|
||||
```d
|
||||
///
|
||||
unittest
|
||||
{
|
||||
// A documented unit test has three slashes in front of it.
|
||||
}
|
||||
|
||||
// Issue ##: https://github.com/caraus-ecms/tanya/issues/##.
|
||||
unittest
|
||||
{
|
||||
// Not documented unit test may still have a description.
|
||||
}
|
||||
```
|
||||
|
||||
### Style guide
|
||||
|
||||
Make sure your changes follow [The D Style](https://dlang.org/dstyle.html) (including
|
||||
[Additional Requirements for Phobos](https://dlang.org/dstyle.html#phobos)).
|
||||
|
||||
You can also use [dscanner](https://github.com/dlang-community/D-Scanner) to test the new code against the
|
||||
most guidlines. The root of this repository contains
|
||||
[dscanner.ini](https://github.com/caraus-ecms/tanya/blob/master/dscanner.ini), configuration file with settings for an
|
||||
automatic style check. Just go to the top-level directory and issue (this assumes `dscanner` is installed in your
|
||||
system):
|
||||
|
||||
```shell
|
||||
dscanner --styleCheck source
|
||||
```
|
||||
|
||||
## Questions and suggestions
|
||||
|
||||
* [Open an issue](https://github.com/caraus-ecms/tanya/issues)
|
||||
* [Send an email](mailto:info@caraus.de)
|
20
README.md
20
README.md
@ -1,11 +1,8 @@
|
||||
# Tanya
|
||||
|
||||
[](https://travis-ci.com/caraus-ecms/tanya)
|
||||
[](https://ci.appveyor.com/project/belka-ew/tanya/branch/master)
|
||||
[](https://codecov.io/gh/caraus-ecms/tanya)
|
||||
[](https://code.dlang.org/packages/tanya)
|
||||
[](https://code.dlang.org/packages/tanya)
|
||||
[](https://raw.githubusercontent.com/caraus-ecms/tanya/master/LICENSE)
|
||||
[](https://opensource.org/licenses/MPL-2.0)
|
||||
|
||||
Tanya is a general purpose library for D programming language.
|
||||
|
||||
@ -15,25 +12,19 @@ Garbage Collector heap. Everything in the library is usable in @nogc code.
|
||||
Tanya provides data structures and utilities to facilitate painless systems
|
||||
programming in D.
|
||||
|
||||
* [API Documentation](https://docs.caraus.io/tanya)
|
||||
* [Contribution guidelines](CONTRIBUTING.md)
|
||||
|
||||
- [API Documentation](https://docs.caraus.tech/tanya)
|
||||
|
||||
## Overview
|
||||
|
||||
Tanya consists of the following packages and (top-level) modules:
|
||||
|
||||
* `algorithm`: Collection of generic algorithms.
|
||||
* `async`: Event loop (epoll, kqueue and IOCP).
|
||||
* `bitmanip`: Bit manipulation.
|
||||
* `container`: Queue, Array, Singly and doubly linked lists, Buffers, UTF-8
|
||||
string, Set, Hash table.
|
||||
* `conv`: This module provides functions for converting between different
|
||||
types.
|
||||
* `encoding`: This package provides tools to work with text encodings.
|
||||
* `format`: Formatting and conversion functions.
|
||||
* `functional`: Functions that manipulate other functions and their argument
|
||||
lists.
|
||||
* `hash`: Hash algorithms.
|
||||
* `math`: Arbitrary precision integer and a set of functions.
|
||||
* `memory`: Tools for manual memory management (allocators, smart pointers).
|
||||
@ -42,9 +33,6 @@ type information at compile-time, to transform from one type to another. It has
|
||||
also different algorithms for iterating, searching and modifying template
|
||||
arguments.
|
||||
* `net`: URL-Parsing, network programming.
|
||||
* `network`: Socket implementation. `network` is currently under rework.
|
||||
After finishing the new socket implementation will land in the `net` package and
|
||||
`network` will be deprecated.
|
||||
* `os`: Platform-independent interfaces to operating system functionality.
|
||||
* `range`: Generic functions and templates for D ranges.
|
||||
* `test`: Test suite for unittest-blocks.
|
||||
@ -174,7 +162,7 @@ parameter is used)
|
||||
|
||||
| DMD | GCC |
|
||||
|:-------:|:---------:|
|
||||
| 2.091.1 | gdc trunk |
|
||||
| 2.100.0 | 12.1 |
|
||||
|
||||
## Further characteristics
|
||||
|
||||
@ -195,4 +183,4 @@ aren't supported
|
||||
## Feedback
|
||||
|
||||
Any feedback about your experience with tanya would be greatly appreciated. Feel free to
|
||||
[contact me](mailto:info@caraus.de).
|
||||
[contact me](mailto:belka@caraus.de).
|
||||
|
54
appveyor.yml
54
appveyor.yml
@ -1,54 +0,0 @@
|
||||
platform: x64
|
||||
os: Visual Studio 2015
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
- DC: dmd
|
||||
DVersion: 2.091.1
|
||||
arch: x64
|
||||
- DC: dmd
|
||||
DVersion: 2.091.1
|
||||
arch: x86
|
||||
|
||||
skip_tags: true
|
||||
|
||||
install:
|
||||
- ps: function SetUpDCompiler
|
||||
{
|
||||
$env:toolchain = "msvc";
|
||||
$version = $env:DVersion;
|
||||
Invoke-WebRequest "http://downloads.dlang.org/releases/2.x/$($version)/dmd.$($version).windows.7z" -OutFile "c:\dmd.7z";
|
||||
echo "finished.";
|
||||
pushd c:\\;
|
||||
7z x dmd.7z > $null;
|
||||
popd;
|
||||
}
|
||||
- ps: SetUpDCompiler
|
||||
|
||||
before_build:
|
||||
- ps: if($env:arch -eq "x86"){
|
||||
$env:compilersetupargs = "x86";
|
||||
$env:Darch = "x86_mscoff";
|
||||
}
|
||||
elseif($env:arch -eq "x64"){
|
||||
$env:compilersetupargs = "amd64";
|
||||
$env:Darch = "x86_64";
|
||||
}
|
||||
- ps: $env:PATH += ";C:\dmd2\windows\bin;";
|
||||
- call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall" %compilersetupargs%
|
||||
|
||||
build_script:
|
||||
- echo dummy build script - dont remove me
|
||||
|
||||
test_script:
|
||||
- echo %Darch%
|
||||
- echo %PATH%
|
||||
- 'dub --version'
|
||||
- '%DC% --version'
|
||||
- dub test :meta -b unittest --arch=%Darch% --compiler=%DC%
|
||||
- dub test :sys -b unittest --arch=%Darch% --compiler=%DC%
|
||||
- dub test :os -b unittest --arch=%Darch% --compiler=%DC%
|
||||
- dub test :encoding -b unittest --arch=%Darch% --compiler=%DC%
|
||||
- dub test :middle -b unittest --arch=%Darch% --compiler=%DC%
|
||||
- dub test :test -b unittest --arch=%Darch% --compiler=%DC%
|
||||
- dub test -b unittest --arch=%Darch% --compiler=%DC%
|
@ -1,14 +0,0 @@
|
||||
rule gas
|
||||
command = gcc -c $in -o $out
|
||||
|
||||
rule archive
|
||||
command = ar rcs $out $in
|
||||
|
||||
build abs.o: gas x64/linux/math/abs.S
|
||||
build log.o: gas x64/linux/math/log.S
|
||||
build equal.o: gas x64/linux/memory/equal.S
|
||||
build fill.o: gas x64/linux/memory/fill.S
|
||||
build copy.o: gas x64/linux/memory/copy.S
|
||||
build syscall.o: gas x64/linux/syscall.S
|
||||
|
||||
build tanya.a: archive syscall.o copy.o fill.o equal.o log.o abs.o
|
@ -1,8 +0,0 @@
|
||||
.text
|
||||
|
||||
.globl thrd_current
|
||||
.type thrd_current, @function
|
||||
|
||||
thrd_current:
|
||||
mov %fs:0, %rax
|
||||
ret
|
@ -1,35 +0,0 @@
|
||||
.text
|
||||
|
||||
|
||||
// fabsf.
|
||||
.globl _D5tanya4math8nbtheory4fabsFNaNbNiNffZf
|
||||
.type _D5tanya4math8nbtheory4fabsFNaNbNiNffZf, @function
|
||||
|
||||
_D5tanya4math8nbtheory4fabsFNaNbNiNffZf:
|
||||
mov $0x7fffffff, %eax
|
||||
movq %rax, %xmm1
|
||||
andpd %xmm1, %xmm0
|
||||
ret
|
||||
|
||||
|
||||
// fabs.
|
||||
.globl _D5tanya4math8nbtheory4fabsFNaNbNiNfdZd
|
||||
.type _D5tanya4math8nbtheory4fabsFNaNbNiNfdZd, @function
|
||||
|
||||
_D5tanya4math8nbtheory4fabsFNaNbNiNfdZd:
|
||||
mov $0x7fffffffffffffff, %rax
|
||||
movq %rax, %xmm1
|
||||
andpd %xmm1, %xmm0
|
||||
ret
|
||||
|
||||
|
||||
// fabsl.
|
||||
.globl _D5tanya4math8nbtheory4fabsFNaNbNiNfeZe
|
||||
.type _D5tanya4math8nbtheory4fabsFNaNbNiNfeZe, @function
|
||||
|
||||
// Load the parameter from the stack onto FP stack, execute 'fabs' instruction
|
||||
// The result is returned in ST0.
|
||||
_D5tanya4math8nbtheory4fabsFNaNbNiNfeZe:
|
||||
fldt 0x8(%rsp)
|
||||
fabs
|
||||
ret
|
@ -1,48 +0,0 @@
|
||||
.text
|
||||
|
||||
|
||||
// logf.
|
||||
.globl _D5tanya4math8nbtheory4logfFNaNbNiNffZf
|
||||
.type _D5tanya4math8nbtheory4logfFNaNbNiNffZf, @function
|
||||
|
||||
_D5tanya4math8nbtheory4logfFNaNbNiNffZf:
|
||||
movss %xmm0, -4(%rsp) // Put the argument onto the stack
|
||||
|
||||
fldln2 // Put lb(e) onto the FPU stack
|
||||
flds -4(%rsp) // Put a float onto the FPU stack
|
||||
fyl2x // %st1 * lb(%st0)
|
||||
|
||||
// The result is on the FPU stack, but returned in %xmm0
|
||||
fstps -4(%rsp)
|
||||
movss -4(%rsp), %xmm0
|
||||
|
||||
ret
|
||||
|
||||
|
||||
// log.
|
||||
.globl _D5tanya4math8nbtheory3logFNaNbNiNfdZd
|
||||
.type _D5tanya4math8nbtheory3logFNaNbNiNfdZd, @function
|
||||
|
||||
_D5tanya4math8nbtheory3logFNaNbNiNfdZd:
|
||||
movsd %xmm0, -8(%rsp) // Put the argument onto the stack
|
||||
|
||||
fldln2 // Put lb(e) onto the FPU stack
|
||||
fldl -8(%rsp) // Put a double onto the FPU stack
|
||||
fyl2x // %st1 * lb(%st0)
|
||||
|
||||
// The result is on the FPU stack, but returned in %xmm0
|
||||
fstpl -8(%rsp)
|
||||
movsd -8(%rsp), %xmm0
|
||||
|
||||
ret
|
||||
|
||||
|
||||
// logl.
|
||||
.globl _D5tanya4math8nbtheory4loglFNaNbNiNfeZe
|
||||
.type _D5tanya4math8nbtheory4loglFNaNbNiNfeZe, @function
|
||||
|
||||
_D5tanya4math8nbtheory4loglFNaNbNiNfeZe:
|
||||
fldln2 // Put lb(e) onto the FPU stack
|
||||
fldt 8(%rsp) // Put the argument onto the FPU stack
|
||||
fyl2x // %st1 * lb(%st0)
|
||||
ret
|
@ -1,67 +0,0 @@
|
||||
.text
|
||||
|
||||
/*
|
||||
* copyMemory.
|
||||
*
|
||||
* rdi - source length
|
||||
* rsi - source data.
|
||||
* rdx - target length.
|
||||
* rcx - target data.
|
||||
*/
|
||||
.globl _D5tanya6memory2op10copyMemoryFNaNbNixAvAvZv
|
||||
.type _D5tanya6memory2op10copyMemoryFNaNbNixAvAvZv, @function
|
||||
|
||||
_D5tanya6memory2op10copyMemoryFNaNbNixAvAvZv:
|
||||
mov %rdi, %rdx
|
||||
mov %rcx, %rdi
|
||||
|
||||
cmp $0x08, %rdx
|
||||
jc aligned_1
|
||||
test $0x07, %edi
|
||||
jz aligned_8
|
||||
|
||||
naligned:
|
||||
movsb
|
||||
dec %rdx
|
||||
test $0x07, %edi
|
||||
jnz naligned
|
||||
|
||||
aligned_8:
|
||||
mov %rdx, %rcx
|
||||
shr $0x03, %rcx
|
||||
rep movsq
|
||||
and $0x07, %edx
|
||||
jz end
|
||||
|
||||
aligned_1:
|
||||
// Write the remaining bytes
|
||||
mov %rdx, %rcx
|
||||
rep movsb
|
||||
|
||||
end:
|
||||
ret
|
||||
|
||||
/*
|
||||
* moveMemory.
|
||||
*
|
||||
* rdi - source length
|
||||
* rsi - source data.
|
||||
* rdx - target length.
|
||||
* rcx - target data.
|
||||
*/
|
||||
.globl _D5tanya6memory2op10moveMemoryFNaNbNixAvAvZv
|
||||
.type _D5tanya6memory2op10moveMemoryFNaNbNixAvAvZv, @function
|
||||
|
||||
_D5tanya6memory2op10moveMemoryFNaNbNixAvAvZv:
|
||||
mov %rdi, %rdx
|
||||
|
||||
lea -1(%rdx, %rsi), %rsi
|
||||
lea -1(%rdx, %rcx), %rdi
|
||||
mov %rdx, %rcx
|
||||
|
||||
std // Set the direction flag
|
||||
|
||||
rep movsb
|
||||
|
||||
cld // Clear the direction flag
|
||||
ret
|
@ -1,59 +0,0 @@
|
||||
.text
|
||||
|
||||
/*
|
||||
* equalMemory.
|
||||
*
|
||||
* rdi - r1 length
|
||||
* rsi - r1 data.
|
||||
* rdx - r2 length.
|
||||
* rcx - r2 data.
|
||||
*/
|
||||
.globl _D5tanya6memory2op11equalMemoryFNaNbNixAvxQdZb
|
||||
.type _D5tanya6memory2op11equalMemoryFNaNbNixAvxQdZb, @function
|
||||
_D5tanya6memory2op11equalMemoryFNaNbNixAvxQdZb:
|
||||
// Compare the lengths
|
||||
cmp %rdx, %rdi
|
||||
jne not_equal
|
||||
|
||||
mov %rcx, %rdi
|
||||
|
||||
// Check if we're aligned
|
||||
cmp $0x08, %rdx
|
||||
jc aligned_1
|
||||
test $0x07, %edi
|
||||
jz aligned_8
|
||||
|
||||
naligned:
|
||||
cmpsb
|
||||
jne not_equal
|
||||
|
||||
dec %rdx
|
||||
test $0x07, %edi
|
||||
jnz naligned
|
||||
|
||||
aligned_8:
|
||||
mov %rdx, %rcx
|
||||
shr $0x03, %rcx
|
||||
|
||||
repe cmpsq
|
||||
jne not_equal
|
||||
|
||||
and $0x07, %edx
|
||||
jz equal
|
||||
|
||||
aligned_1: // Compare the remaining bytes
|
||||
mov %rdx, %rcx
|
||||
cmp $0x0, %rcx
|
||||
|
||||
repe cmpsb
|
||||
jne not_equal
|
||||
|
||||
equal:
|
||||
mov $0x01, %rax // Return 1
|
||||
jmp end
|
||||
|
||||
not_equal:
|
||||
xor %rax, %rax // Return 0
|
||||
|
||||
end:
|
||||
ret
|
@ -1,160 +0,0 @@
|
||||
.text
|
||||
|
||||
/*
|
||||
* fillMemory.
|
||||
*
|
||||
* rdi - length.
|
||||
* rsi - pointer.
|
||||
* rdx - value filled with a byte.
|
||||
*/
|
||||
.globl _D5tanya6memory2op10fillMemoryFNaNbNiAvmZv
|
||||
.type _D5tanya6memory2op10fillMemoryFNaNbNiAvmZv, @function
|
||||
|
||||
_D5tanya6memory2op10fillMemoryFNaNbNiAvmZv:
|
||||
// Check for zero length
|
||||
test %rdi, %rdi
|
||||
jz end
|
||||
|
||||
mov %rdi, %rax
|
||||
mov %rsi, %r8
|
||||
|
||||
movq %rdx, %xmm0
|
||||
movlhps %xmm0, %xmm0
|
||||
|
||||
// Check if the pointer is aligned to a 16-byte boundary
|
||||
and $-0x10, %r8
|
||||
|
||||
// Compute the number of misaligned bytes
|
||||
mov %rsi, %r9
|
||||
sub %r8, %r9
|
||||
|
||||
test %r9, %r9
|
||||
jz aligned
|
||||
|
||||
// Get the number of bytes to be written until we are aligned
|
||||
mov $0x10, %rcx
|
||||
sub %r9, %rcx
|
||||
|
||||
mov %rsi, %r8
|
||||
|
||||
// If the length is less than the number of misaligned bytes,
|
||||
// write one byte at a time and exit
|
||||
cmp %rax, %rcx
|
||||
jg aligned_1
|
||||
|
||||
naligned:
|
||||
mov %dl, (%r8) // Write a byte
|
||||
|
||||
// Advance the pointer. Decrease the total number of bytes
|
||||
// and the misaligned ones
|
||||
inc %r8
|
||||
dec %rcx
|
||||
dec %rax
|
||||
|
||||
// Checks if we are aligned
|
||||
test %rcx, %rcx
|
||||
jnz naligned
|
||||
|
||||
aligned:
|
||||
// Checks if we're done writing bytes
|
||||
test %rax, %rax
|
||||
jz end
|
||||
|
||||
// Write 1 byte at a time
|
||||
cmp $8, %rax
|
||||
jl aligned_1
|
||||
|
||||
// Write 8 bytes at a time
|
||||
cmp $16, %rax
|
||||
jl aligned_8
|
||||
|
||||
// Write 16 bytes at a time
|
||||
cmp $32, %rax
|
||||
jl aligned_16
|
||||
|
||||
// Write 32 bytes at a time
|
||||
cmp $64, %rax
|
||||
jl aligned_32
|
||||
|
||||
aligned_64:
|
||||
movdqa %xmm0, (%r8)
|
||||
movdqa %xmm0, 16(%r8)
|
||||
movdqa %xmm0, 32(%r8)
|
||||
movdqa %xmm0, 48(%r8)
|
||||
|
||||
add $64, %r8
|
||||
sub $64, %rax
|
||||
|
||||
cmp $64, %rax
|
||||
jge aligned_64
|
||||
|
||||
// Checks if we're done writing bytes
|
||||
test %rax, %rax
|
||||
jz end
|
||||
|
||||
// Write 1 byte at a time
|
||||
cmp $8, %rax
|
||||
jl aligned_1
|
||||
|
||||
// Write 8 bytes at a time
|
||||
cmp $16, %rax
|
||||
jl aligned_8
|
||||
|
||||
// Write 16 bytes at a time
|
||||
cmp $32, %rax
|
||||
jl aligned_16
|
||||
|
||||
aligned_32:
|
||||
movdqa %xmm0, (%r8)
|
||||
movdqa %xmm0, 16(%r8)
|
||||
|
||||
add $32, %r8
|
||||
sub $32, %rax
|
||||
|
||||
// Checks if we're done writing bytes
|
||||
test %rax, %rax
|
||||
jz end
|
||||
|
||||
// Write 1 byte at a time
|
||||
cmp $8, %rax
|
||||
jl aligned_1
|
||||
|
||||
// Write 8 bytes at a time
|
||||
cmp $16, %rax
|
||||
jl aligned_8
|
||||
|
||||
aligned_16:
|
||||
movdqa %xmm0, (%r8)
|
||||
|
||||
add $16, %r8
|
||||
sub $16, %rax
|
||||
|
||||
// Checks if we're done writing bytes
|
||||
test %rax, %rax
|
||||
jz end
|
||||
|
||||
// Write 1 byte at a time
|
||||
cmp $8, %rax
|
||||
jl aligned_1
|
||||
|
||||
aligned_8:
|
||||
mov %rdx, (%r8)
|
||||
|
||||
add $8, %r8
|
||||
sub $8, %rax
|
||||
|
||||
// Checks if we're done writing bytes
|
||||
test %rax, %rax
|
||||
jz end
|
||||
|
||||
aligned_1:
|
||||
mov %dl, (%r8)
|
||||
|
||||
inc %r8
|
||||
dec %rax
|
||||
|
||||
test %rax, %rax
|
||||
jnz aligned_1
|
||||
|
||||
end:
|
||||
ret
|
@ -1,65 +0,0 @@
|
||||
/*
|
||||
The kernel uses the following registers:
|
||||
%rdi, %rsi, %rdx, %r8, %r9, %r10
|
||||
|
||||
The number of the syscall is passed in %rax.
|
||||
|
||||
A syscall clobbers:
|
||||
%rax, %rcx, %r11
|
||||
|
||||
The returned value is placed in %rax.
|
||||
*/
|
||||
.text
|
||||
|
||||
// 1 parameter.
|
||||
.globl _D5tanya3sys5linux7syscallQiFNbNillZl
|
||||
.type _D5tanya3sys5linux7syscallQiFNbNillZl, @function
|
||||
|
||||
_D5tanya3sys5linux7syscallQiFNbNillZl:
|
||||
movq %rsi, %rax // Syscall number.
|
||||
|
||||
syscall
|
||||
|
||||
ret
|
||||
|
||||
|
||||
// 2 parameters.
|
||||
.globl _D5tanya3sys5linux7syscallQiFNbNilllZl
|
||||
.type _D5tanya3sys5linux7syscallQiFNbNilllZl, @function
|
||||
|
||||
_D5tanya3sys5linux7syscallQiFNbNilllZl:
|
||||
movq %rdx, %rax
|
||||
|
||||
syscall
|
||||
|
||||
ret
|
||||
|
||||
|
||||
// 3 parameters.
|
||||
.globl _D5tanya3sys5linux7syscallQiFNbNillllZl
|
||||
.type _D5tanya3sys5linux7syscallQiFNbNillllZl, @function
|
||||
|
||||
_D5tanya3sys5linux7syscallQiFNbNillllZl:
|
||||
movq %rcx, %rax
|
||||
|
||||
syscall
|
||||
|
||||
ret
|
||||
|
||||
|
||||
// 6 parameters.
|
||||
.globl _D5tanya3sys5linux7syscallQiFNbNilllllllZl
|
||||
.type _D5tanya3sys5linux7syscallQiFNbNilllllllZl, @function
|
||||
|
||||
_D5tanya3sys5linux7syscallQiFNbNilllllllZl:
|
||||
pushq %rbp
|
||||
movq %rsp, %rbp
|
||||
|
||||
movq 16(%rbp), %rax
|
||||
|
||||
mov %rcx, %r10
|
||||
|
||||
syscall
|
||||
|
||||
leave
|
||||
ret
|
@ -1,3 +0,0 @@
|
||||
ignore:
|
||||
- "source/tanya/async/event/iocp.d"
|
||||
- "source/tanya/async/iocp.d"
|
19
dub.json
19
dub.json
@ -2,7 +2,7 @@
|
||||
"name": "tanya",
|
||||
"description": "@nogc library. Containers, networking, metaprogramming, memory management, utilities",
|
||||
"license": "MPL-2.0",
|
||||
"copyright": "© Eugene Wissner <info@caraus.de>",
|
||||
"copyright": "© Eugene Wissner <belka@caraus.de>",
|
||||
"authors": [
|
||||
"Eugene Wissner"
|
||||
],
|
||||
@ -11,22 +11,15 @@
|
||||
|
||||
"dependencies": {
|
||||
"tanya:meta": "*",
|
||||
"tanya:sys": "*",
|
||||
"tanya:os": "*",
|
||||
"tanya:encoding": "*",
|
||||
"tanya:middle": "*",
|
||||
"tanya:test": "*"
|
||||
},
|
||||
|
||||
"dependencies-linux": {
|
||||
"tanya:test": "*",
|
||||
"mir-linux-kernel": "~>1.0.0"
|
||||
},
|
||||
|
||||
"subPackages": [
|
||||
"./meta",
|
||||
"./sys",
|
||||
"./os",
|
||||
"./encoding",
|
||||
"./middle",
|
||||
"./test"
|
||||
],
|
||||
@ -45,9 +38,7 @@
|
||||
{
|
||||
"name": "native",
|
||||
"targetType": "library",
|
||||
"platforms": ["linux-x86_64-gdc"],
|
||||
"preBuildCommands": ["ninja -C arch"],
|
||||
"lflags": ["arch/tanya.a"],
|
||||
"platforms": ["linux-x86_64"],
|
||||
"versions": ["TanyaNative"]
|
||||
},
|
||||
{
|
||||
@ -64,9 +55,7 @@
|
||||
},
|
||||
{
|
||||
"name": "unittest-native",
|
||||
"platforms": ["linux-x86_64-gdc"],
|
||||
"preBuildCommands": ["ninja -C arch"],
|
||||
"lflags": ["arch/tanya.a"],
|
||||
"platforms": ["linux-x86_64"],
|
||||
"versions": ["TanyaNative"],
|
||||
"importPaths": [
|
||||
"./source",
|
||||
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
"name": "encoding",
|
||||
"description": "This package provides tools to work with text encodings",
|
||||
"targetType": "library",
|
||||
|
||||
"dependencies": {
|
||||
"tanya:meta": "*"
|
||||
},
|
||||
|
||||
"sourcePaths": [
|
||||
"."
|
||||
],
|
||||
"importPaths": [
|
||||
"."
|
||||
],
|
||||
"dflags-dmd": ["-dip1000"]
|
||||
}
|
@ -1,501 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Functions operating on ASCII characters.
|
||||
*
|
||||
* ASCII is $(B A)merican $(B S)tandard $(B C)ode for $(B I)nformation
|
||||
* $(B I)nterchange.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/encoding/tanya/encoding/ascii.d,
|
||||
* tanya/encoding/ascii.d)
|
||||
*/
|
||||
module tanya.encoding.ascii;
|
||||
|
||||
import tanya.meta.trait;
|
||||
|
||||
immutable string fullHexDigits = "0123456789ABCDEFabcdef"; /// 0..9A..Fa..f.
|
||||
immutable string hexDigits = "0123456789ABCDEF"; /// 0..9A..F.
|
||||
immutable string lowerHexDigits = "0123456789abcdef"; /// 0..9a..f.
|
||||
immutable string digits = "0123456789"; /// 0..9.
|
||||
immutable string octalDigits = "01234567"; /// 0..7.
|
||||
|
||||
/// A..Za..z.
|
||||
immutable string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
immutable string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /// A..Z.
|
||||
immutable string lowercase = "abcdefghijklmnopqrstuvwxyz"; /// a..z.
|
||||
|
||||
/**
|
||||
* Whitespace, Horizontal Tab (HT), Line Feed (LF), Carriage Return (CR),
|
||||
* Vertical Tab (VT) or Form Feed (FF).
|
||||
*/
|
||||
immutable string whitespace = "\t\n\v\f\r ";
|
||||
|
||||
/// Letter case specifier.
|
||||
enum LetterCase : bool
|
||||
{
|
||||
upper, /// Uppercase.
|
||||
lower, /// Lowercase.
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for an uppecase alphabetic character.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is an uppercase alphabetic
|
||||
* character, $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isUpper(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c >= 'A') && (c <= 'Z');
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isUpper('A'));
|
||||
assert(isUpper('Z'));
|
||||
assert(isUpper('L'));
|
||||
assert(!isUpper('a'));
|
||||
assert(!isUpper('!'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a lowercase alphabetic character.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a lowercase alphabetic
|
||||
* character, $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isLower(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c >= 'a') && (c <= 'z');
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isLower('a'));
|
||||
assert(isLower('z'));
|
||||
assert(isLower('l'));
|
||||
assert(!isLower('A'));
|
||||
assert(!isLower('!'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for an alphabetic character (upper- or lowercase).
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is an alphabetic character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isAlpha(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return isUpper(c) || isLower(c);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isAlpha('A'));
|
||||
assert(isAlpha('Z'));
|
||||
assert(isAlpha('L'));
|
||||
assert(isAlpha('a'));
|
||||
assert(isAlpha('z'));
|
||||
assert(isAlpha('l'));
|
||||
assert(!isAlpha('!'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a digit.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a digit,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isDigit(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c >= '0') && (c <= '9');
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isDigit('0'));
|
||||
assert(isDigit('1'));
|
||||
assert(isDigit('2'));
|
||||
assert(isDigit('3'));
|
||||
assert(isDigit('4'));
|
||||
assert(isDigit('5'));
|
||||
assert(isDigit('6'));
|
||||
assert(isDigit('7'));
|
||||
assert(isDigit('8'));
|
||||
assert(isDigit('9'));
|
||||
assert(!isDigit('a'));
|
||||
assert(!isDigit('!'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for an alphabetic character (upper- or lowercase) or a digit.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is an alphabetic character or a
|
||||
* digit, $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isAlphaNum(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return isAlpha(c) || isDigit(c);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isAlphaNum('0'));
|
||||
assert(isAlphaNum('1'));
|
||||
assert(isAlphaNum('9'));
|
||||
assert(isAlphaNum('A'));
|
||||
assert(isAlphaNum('Z'));
|
||||
assert(isAlphaNum('L'));
|
||||
assert(isAlphaNum('a'));
|
||||
assert(isAlphaNum('z'));
|
||||
assert(isAlphaNum('l'));
|
||||
assert(!isAlphaNum('!'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a 7-bit ASCII character.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is an ASCII character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isASCII(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return c < 128;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isASCII('0'));
|
||||
assert(isASCII('L'));
|
||||
assert(isASCII('l'));
|
||||
assert(isASCII('!'));
|
||||
assert(!isASCII('©'));
|
||||
assert(!isASCII('§'));
|
||||
assert(!isASCII(char.init)); // 0xFF
|
||||
assert(!isASCII(wchar.init)); // 0xFFFF
|
||||
assert(!isASCII(dchar.init)); // 0xFFFF
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a control character.
|
||||
*
|
||||
* Control characters are non-printable characters. Their ASCII codes are those
|
||||
* between 0x00 (NUL) and 0x1f (US), and 0x7f (DEL).
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a control character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isPrintable), $(D_PSYMBOL isGraphical).
|
||||
*/
|
||||
bool isControl(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c <= 0x1f) || (c == 0x7f);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isControl('\t'));
|
||||
assert(isControl('\0'));
|
||||
assert(isControl('\u007f'));
|
||||
assert(!isControl(' '));
|
||||
assert(!isControl('a'));
|
||||
assert(!isControl(char.init)); // 0xFF
|
||||
assert(!isControl(wchar.init)); // 0xFFFF
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a whitespace character.
|
||||
*
|
||||
* Whitespace characters are:
|
||||
*
|
||||
* $(UL
|
||||
* $(LI Whitespace)
|
||||
* $(LI Horizontal Tab (HT))
|
||||
* $(LI Line Feed (LF))
|
||||
* $(LI Carriage Return (CR))
|
||||
* $(LI Vertical Tab (VT))
|
||||
* $(LI Form Feed (FF))
|
||||
* )
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a whitespace character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL whitespace).
|
||||
*/
|
||||
bool isWhite(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return ((c >= 0x09) && (c <= 0x0d)) || (c == 0x20);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isWhite('\t'));
|
||||
assert(isWhite('\n'));
|
||||
assert(isWhite('\v'));
|
||||
assert(isWhite('\f'));
|
||||
assert(isWhite('\r'));
|
||||
assert(isWhite(' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a graphical character.
|
||||
*
|
||||
* Graphical characters are printable characters but whitespace characters.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a control character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isControl), $(D_PSYMBOL isWhite).
|
||||
*/
|
||||
bool isGraphical(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c > 0x20) && (c < 0x7f);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isGraphical('a'));
|
||||
assert(isGraphical('0'));
|
||||
assert(!isGraphical('\u007f'));
|
||||
assert(!isGraphical('§'));
|
||||
assert(!isGraphical('\n'));
|
||||
assert(!isGraphical(' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a printable character.
|
||||
*
|
||||
* This is the opposite of a control character.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a control character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isControl).
|
||||
*/
|
||||
bool isPrintable(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c >= 0x20) && (c < 0x7f);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isPrintable('a'));
|
||||
assert(isPrintable('0'));
|
||||
assert(!isPrintable('\u007f'));
|
||||
assert(!isPrintable('§'));
|
||||
assert(!isPrintable('\n'));
|
||||
assert(isPrintable(' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a hexadecimal digit.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a hexadecimal digit,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isHexDigit(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return ((c >= '0') && (c <= '9'))
|
||||
|| ((c >= 'a') && (c <= 'f'))
|
||||
|| ((c >= 'A') && (c <= 'F'));
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isHexDigit('0'));
|
||||
assert(isHexDigit('1'));
|
||||
assert(isHexDigit('8'));
|
||||
assert(isHexDigit('9'));
|
||||
assert(isHexDigit('A'));
|
||||
assert(isHexDigit('F'));
|
||||
assert(!isHexDigit('G'));
|
||||
assert(isHexDigit('a'));
|
||||
assert(isHexDigit('f'));
|
||||
assert(!isHexDigit('g'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for an octal character.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is an octal character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isOctalDigit(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return (c >= '0') && (c <= '7');
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isOctalDigit('0'));
|
||||
assert(isOctalDigit('1'));
|
||||
assert(isOctalDigit('2'));
|
||||
assert(isOctalDigit('3'));
|
||||
assert(isOctalDigit('4'));
|
||||
assert(isOctalDigit('5'));
|
||||
assert(isOctalDigit('6'));
|
||||
assert(isOctalDigit('7'));
|
||||
assert(!isOctalDigit('8'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for a octal character.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM c) is a octal character,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool isPunctuation(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return ((c >= 0x21) && (c <= 0x2f))
|
||||
|| ((c >= 0x3a) && (c <= 0x40))
|
||||
|| ((c >= 0x5b) && (c <= 0x60))
|
||||
|| ((c >= 0x7b) && (c <= 0x7e));
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(isPunctuation('!'));
|
||||
assert(isPunctuation(':'));
|
||||
assert(isPunctuation('\\'));
|
||||
assert(isPunctuation('|'));
|
||||
assert(!isPunctuation('0'));
|
||||
assert(!isPunctuation(' '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts $(D_PARAM c) to uppercase.
|
||||
*
|
||||
* If $(D_PARAM c) is not a lowercase character, $(D_PARAM c) is returned
|
||||
* unchanged.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: The lowercase of $(D_PARAM c) if available, just $(D_PARAM c)
|
||||
* otherwise.
|
||||
*/
|
||||
C toUpper(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return isLower(c) ? (cast(C) (c - 32)) : c;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(toUpper('a') == 'A');
|
||||
assert(toUpper('A') == 'A');
|
||||
assert(toUpper('!') == '!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts $(D_PARAM c) to lowercase.
|
||||
*
|
||||
* If $(D_PARAM c) is not an uppercase character, $(D_PARAM c) is returned
|
||||
* unchanged.
|
||||
*
|
||||
* Params:
|
||||
* C = Some character type.
|
||||
* c = Some character.
|
||||
*
|
||||
* Returns: The uppercase of $(D_PARAM c) if available, just $(D_PARAM c)
|
||||
* otherwise.
|
||||
*/
|
||||
C toLower(C)(C c)
|
||||
if (isSomeChar!C)
|
||||
{
|
||||
return isUpper(c) ? (cast(C) (c + 32)) : c;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(toLower('A') == 'a');
|
||||
assert(toLower('a') == 'a');
|
||||
assert(toLower('!') == '!');
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* This package provides tools to work with text encodings.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/encoding/tanya/encoding/package.d,
|
||||
* tanya/encoding/package.d)
|
||||
*/
|
||||
module tanya.encoding;
|
||||
|
||||
public import tanya.encoding.ascii;
|
@ -9,7 +9,7 @@
|
||||
* It contains different algorithms for iterating, searching and modifying
|
||||
* template arguments.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -9,7 +9,7 @@
|
||||
* to transform from one type to another. It has also different algorithms for
|
||||
* iterating, searching and modifying template arguments.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -8,7 +8,7 @@
|
||||
* Templates in this module are used to obtain type information at compile
|
||||
* time.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -70,47 +70,6 @@ enum bool isWideString(T) = is(T : const dchar[]) && !isStaticArray!T;
|
||||
static assert(!isWideString!(dchar[10]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a complex type.
|
||||
*
|
||||
* Complex types are:
|
||||
* $(UL
|
||||
* $(LI cfloat)
|
||||
* $(LI ifloat)
|
||||
* $(LI cdouble)
|
||||
* $(LI idouble)
|
||||
* $(LI creal)
|
||||
* $(LI ireal)
|
||||
* )
|
||||
*
|
||||
* Params:
|
||||
* T = A type.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM T) is a complex type,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
enum bool isComplex(T) = is(Unqual!(OriginalType!T) == cfloat)
|
||||
|| is(Unqual!(OriginalType!T) == ifloat)
|
||||
|| is(Unqual!(OriginalType!T) == cdouble)
|
||||
|| is(Unqual!(OriginalType!T) == idouble)
|
||||
|| is(Unqual!(OriginalType!T) == creal)
|
||||
|| is(Unqual!(OriginalType!T) == ireal);
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(isComplex!cfloat);
|
||||
static assert(isComplex!ifloat);
|
||||
static assert(isComplex!cdouble);
|
||||
static assert(isComplex!idouble);
|
||||
static assert(isComplex!creal);
|
||||
static assert(isComplex!ireal);
|
||||
|
||||
static assert(!isComplex!float);
|
||||
static assert(!isComplex!double);
|
||||
static assert(!isComplex!real);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests whether $(D_PARAM T) is an interface.
|
||||
*
|
||||
@ -353,32 +312,6 @@ enum bool isIntegral(T) = isUnsigned!T
|
||||
static assert(!isIntegral!float);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a numeric (floating point, integral or
|
||||
* complex) type.
|
||||
*
|
||||
* Params:
|
||||
* T = A type.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM T) is a numeric type,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isIntegral!T),
|
||||
* $(D_PSYMBOL isFloatingPoint),
|
||||
* $(D_PSYMBOL isComplex).
|
||||
*/
|
||||
enum bool isNumeric(T) = isIntegral!T || isFloatingPoint!T || isComplex!T;
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
alias F = float;
|
||||
static assert(isNumeric!F);
|
||||
static assert(!isNumeric!bool);
|
||||
static assert(!isNumeric!char);
|
||||
static assert(!isNumeric!wchar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a boolean type, i.e. $(D_KEYWORD bool).
|
||||
*
|
||||
@ -458,67 +391,6 @@ enum bool isSomeChar(T) = is(Unqual!(OriginalType!T) == char)
|
||||
static assert(!isSomeChar!uint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a scalar type.
|
||||
*
|
||||
* Scalar types are numbers, booleans and characters.
|
||||
*
|
||||
* Params:
|
||||
* T = A type.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM T) is a scalar type,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isNumeric),
|
||||
* $(D_PSYMBOL isBoolean),
|
||||
* $(D_PSYMBOL isSomeChar).
|
||||
*/
|
||||
enum bool isScalarType(T) = isNumeric!T || isBoolean!T || isSomeChar!T;
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(isScalarType!int);
|
||||
static assert(!isScalarType!(int[]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a basic type.
|
||||
*
|
||||
* Basic types are scalar types and $(D_KEYWORD void).
|
||||
*
|
||||
* Params:
|
||||
* T = A type.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM T) is a basic type,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isScalarType).
|
||||
*/
|
||||
enum bool isBasicType(T) = isScalarType!T || is(T : void);
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct S
|
||||
{
|
||||
}
|
||||
class C
|
||||
{
|
||||
}
|
||||
enum E : int
|
||||
{
|
||||
i = 0,
|
||||
}
|
||||
|
||||
static assert(isBasicType!void);
|
||||
static assert(isBasicType!(shared void));
|
||||
static assert(isBasicType!E);
|
||||
static assert(!isBasicType!(int*));
|
||||
static assert(!isBasicType!(void function()));
|
||||
static assert(!isBasicType!C);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a pointer type.
|
||||
*
|
||||
@ -676,34 +548,6 @@ template isAssociativeArray(T)
|
||||
static assert(!isAssociativeArray!bool);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is a built-in type.
|
||||
*
|
||||
* Built-in types are all basic types and arrays.
|
||||
*
|
||||
* Params:
|
||||
* T = A type.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM T) is a built-in type,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isBasicType!T),
|
||||
* $(D_PSYMBOL isArray),
|
||||
* $(D_PSYMBOL isAssociativeArray).
|
||||
*/
|
||||
enum bool isBuiltinType(T) = isBasicType!T
|
||||
|| isArray!T
|
||||
|| isAssociativeArray!T;
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(isBuiltinType!int);
|
||||
static assert(isBuiltinType!(int[]));
|
||||
static assert(isBuiltinType!(int[int]));
|
||||
static assert(!isBuiltinType!(int*));
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM T) is an aggregate type.
|
||||
*
|
||||
@ -844,57 +688,6 @@ enum bool isSomeString(T) = isNarrowString!T || isWideString!T;
|
||||
static assert(!isSomeString!(char[10]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the minimum value of type $(D_PARAM T). In contrast to
|
||||
* $(D_INLINECODE T.min) this template works with floating point and complex
|
||||
* types as well.
|
||||
*
|
||||
* Params:
|
||||
* T = Integral, boolean, floating point, complex or character type.
|
||||
*
|
||||
* Returns: The minimum value of $(D_PARAM T).
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isIntegral),
|
||||
* $(D_PSYMBOL isBoolean),
|
||||
* $(D_PSYMBOL isSomeChar),
|
||||
* $(D_PSYMBOL isFloatingPoint),
|
||||
* $(D_PSYMBOL isComplex).
|
||||
*/
|
||||
template mostNegative(T)
|
||||
{
|
||||
static if (isIntegral!T || isBoolean!T || isSomeChar!T)
|
||||
{
|
||||
enum T mostNegative = T.min;
|
||||
}
|
||||
else static if (isFloatingPoint!T || isComplex!T)
|
||||
{
|
||||
enum T mostNegative = -T.max;
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(false, T.stringof ~ " doesn't have the minimum value");
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(mostNegative!char == char.min);
|
||||
static assert(mostNegative!wchar == wchar.min);
|
||||
static assert(mostNegative!dchar == dchar.min);
|
||||
|
||||
static assert(mostNegative!byte == byte.min);
|
||||
static assert(mostNegative!ubyte == ubyte.min);
|
||||
static assert(mostNegative!bool == bool.min);
|
||||
|
||||
static assert(mostNegative!float == -float.max);
|
||||
static assert(mostNegative!double == -double.max);
|
||||
static assert(mostNegative!real == -real.max);
|
||||
|
||||
static assert(mostNegative!ifloat == -ifloat.max);
|
||||
static assert(mostNegative!cfloat == -cfloat.max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the type $(D_PARAM T) is copyable.
|
||||
*
|
||||
@ -2335,7 +2128,7 @@ if (isCallable!F)
|
||||
}
|
||||
else
|
||||
{
|
||||
enum getDefault(T[i .. i + 1] name)
|
||||
auto getDefault(T[i .. i + 1] name)
|
||||
{
|
||||
return name[0];
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
* types. They take some type as argument and return a different type after
|
||||
* perfoming the specified transformation.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -5,8 +5,7 @@
|
||||
|
||||
"dependencies": {
|
||||
"tanya:meta": "*",
|
||||
"tanya:os": "*",
|
||||
"tanya:sys": "*"
|
||||
"tanya:os": "*"
|
||||
},
|
||||
|
||||
"dependencies-linux": {
|
||||
|
BIN
middle/middle-test-library
Executable file
BIN
middle/middle-test-library
Executable file
Binary file not shown.
@ -8,7 +8,7 @@
|
||||
* Allocators are classes encapsulating memory allocation strategy. This allows
|
||||
* to decouple memory management from the algorithms and the data.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -101,7 +101,11 @@ mixin template DefaultAllocator()
|
||||
* Precondition: $(D_INLINECODE allocator_ !is null)
|
||||
*/
|
||||
this(shared Allocator allocator) @nogc nothrow pure @safe
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.allocator_ = allocator;
|
||||
}
|
||||
@ -115,7 +119,11 @@ mixin template DefaultAllocator()
|
||||
* Postcondition: $(D_INLINECODE allocator !is null)
|
||||
*/
|
||||
@property shared(Allocator) allocator() @nogc nothrow pure @safe
|
||||
out (allocator; allocator !is null)
|
||||
out (allocator)
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
if (allocator_ is null)
|
||||
{
|
||||
@ -126,7 +134,11 @@ mixin template DefaultAllocator()
|
||||
|
||||
/// ditto
|
||||
@property shared(Allocator) allocator() const @nogc nothrow pure @trusted
|
||||
out (allocator; allocator !is null)
|
||||
out (allocator)
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
if (allocator_ is null)
|
||||
{
|
||||
@ -162,7 +174,11 @@ private shared(Allocator) getAllocatorInstance() @nogc nothrow
|
||||
* Postcondition: $(D_INLINECODE allocator !is null).
|
||||
*/
|
||||
@property shared(Allocator) defaultAllocator() @nogc nothrow pure @trusted
|
||||
out (allocator; allocator !is null)
|
||||
out (allocator)
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
return (cast(GetPureInstance!Allocator) &getAllocatorInstance)();
|
||||
}
|
||||
@ -176,7 +192,11 @@ out (allocator; allocator !is null)
|
||||
* Precondition: $(D_INLINECODE allocator !is null).
|
||||
*/
|
||||
@property void defaultAllocator(shared(Allocator) allocator) @nogc nothrow @safe
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
.allocator = allocator;
|
||||
}
|
||||
@ -258,7 +278,11 @@ void dispose(T)(shared Allocator allocator, auto ref T p)
|
||||
*/
|
||||
T make(T, A...)(shared Allocator allocator, auto ref A args)
|
||||
if (is(T == class))
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto mem = (() @trusted => allocator.allocate(stateSize!T))();
|
||||
if (mem is null)
|
||||
@ -290,7 +314,11 @@ in (allocator !is null)
|
||||
*/
|
||||
T* make(T, A...)(shared Allocator allocator, auto ref A args)
|
||||
if (!isPolymorphicType!T && !isAssociativeArray!T && !isArray!T)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto mem = (() @trusted => allocator.allocate(stateSize!T))();
|
||||
if (mem is null)
|
||||
@ -327,8 +355,12 @@ in (allocator !is null)
|
||||
* && n <= size_t.max / E.sizeof)
|
||||
*/
|
||||
T make(T : E[], E)(shared Allocator allocator, size_t n)
|
||||
in (allocator !is null)
|
||||
in (n <= size_t.max / E.sizeof)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
assert(n <= size_t.max / E.sizeof);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto ret = allocator.resize!E(null, n);
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Lifetime management functions, types and related exceptions.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2019.
|
||||
* Copyright: Eugene Wissner 2019-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -61,8 +61,15 @@ package(tanya) void destroyAllImpl(R, E)(R p)
|
||||
*/
|
||||
T emplace(T, U, Args...)(void[] memory, U outer, auto ref Args args)
|
||||
if (!isAbstractClass!T && isInnerClass!T && is(typeof(T.outer) == U))
|
||||
in (memory.length >= stateSize!T)
|
||||
out (result; memory.ptr is (() @trusted => cast(void*) result)())
|
||||
in
|
||||
{
|
||||
assert(memory.length >= stateSize!T);
|
||||
}
|
||||
out (result)
|
||||
{
|
||||
assert(memory.ptr is (() @trusted => cast(void*) result)());
|
||||
}
|
||||
do
|
||||
{
|
||||
import tanya.memory.op : copy;
|
||||
|
||||
@ -82,8 +89,15 @@ out (result; memory.ptr is (() @trusted => cast(void*) result)())
|
||||
/// ditto
|
||||
T emplace(T, Args...)(void[] memory, auto ref Args args)
|
||||
if (is(T == class) && !isAbstractClass!T && !isInnerClass!T)
|
||||
in (memory.length == stateSize!T)
|
||||
out (result; memory.ptr is (() @trusted => cast(void*) result)())
|
||||
in
|
||||
{
|
||||
assert(memory.length == stateSize!T);
|
||||
}
|
||||
out (result)
|
||||
{
|
||||
assert(memory.ptr is (() @trusted => cast(void*) result)());
|
||||
}
|
||||
do
|
||||
{
|
||||
import tanya.memory.op : copy;
|
||||
|
||||
@ -128,8 +142,15 @@ out (result; memory.ptr is (() @trusted => cast(void*) result)())
|
||||
/// ditto
|
||||
T* emplace(T, Args...)(void[] memory, auto ref Args args)
|
||||
if (!isAggregateType!T && (Args.length <= 1))
|
||||
in (memory.length >= T.sizeof)
|
||||
out (result; memory.ptr is result)
|
||||
in
|
||||
{
|
||||
assert(memory.length >= T.sizeof);
|
||||
}
|
||||
out (result)
|
||||
{
|
||||
assert(memory.ptr is result);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto result = (() @trusted => cast(T*) memory.ptr)();
|
||||
static if (Args.length == 1)
|
||||
@ -166,8 +187,15 @@ private void initializeOne(T)(ref void[] memory, ref T* result) @trusted
|
||||
/// ditto
|
||||
T* emplace(T, Args...)(void[] memory, auto ref Args args)
|
||||
if (!isPolymorphicType!T && isAggregateType!T)
|
||||
in (memory.length >= T.sizeof)
|
||||
out (result; memory.ptr is result)
|
||||
in
|
||||
{
|
||||
assert(memory.length >= T.sizeof);
|
||||
}
|
||||
out (result)
|
||||
{
|
||||
assert(memory.ptr is result);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto result = (() @trusted => cast(T*) memory.ptr)();
|
||||
|
||||
@ -291,7 +319,11 @@ private void deinitialize(bool zero, T)(ref T value)
|
||||
* Precondition: `&source !is &target`.
|
||||
*/
|
||||
void moveEmplace(T)(ref T source, ref T target) @system
|
||||
in (&source !is &target, "Source and target must be different")
|
||||
in
|
||||
{
|
||||
assert(&source !is &target, "Source and target must be different");
|
||||
}
|
||||
do
|
||||
{
|
||||
static if (is(T == struct) || isStaticArray!T)
|
||||
{
|
||||
|
@ -6,7 +6,7 @@
|
||||
* Allocator based on $(D_PSYMBOL malloc), $(D_PSYMBOL realloc) and
|
||||
* $(D_PSYMBOL free).
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -15,11 +15,6 @@
|
||||
*/
|
||||
module tanya.memory.mallocator;
|
||||
|
||||
version (TanyaNative)
|
||||
{
|
||||
}
|
||||
else:
|
||||
|
||||
import core.stdc.stdlib;
|
||||
import tanya.memory.allocator;
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
/*
|
||||
* Native allocator.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,31 +14,34 @@
|
||||
*/
|
||||
module tanya.memory.mmappool;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
import mir.linux._asm.unistd;
|
||||
import core.sys.linux.sys.mman;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.memory.op;
|
||||
import tanya.os.error;
|
||||
import tanya.sys.linux.syscall;
|
||||
import tanya.sys.posix.mman;
|
||||
|
||||
private void* mapMemory(const size_t length) @nogc nothrow pure @system
|
||||
version (Windows)
|
||||
{
|
||||
auto p = syscall_(0,
|
||||
length,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS,
|
||||
-1,
|
||||
0,
|
||||
NR_mmap);
|
||||
return p == -ErrorCode.noMemory ? null : cast(void*) p;
|
||||
import core.sys.windows.basetsd : SIZE_T;
|
||||
import core.sys.windows.windef : BOOL, DWORD;
|
||||
import core.sys.windows.winnt : MEM_COMMIT, MEM_RELEASE, PAGE_READWRITE, PVOID;
|
||||
|
||||
extern (Windows)
|
||||
private PVOID VirtualAlloc(PVOID, SIZE_T, DWORD, DWORD)
|
||||
@nogc nothrow pure @system;
|
||||
|
||||
extern (Windows)
|
||||
private BOOL VirtualFree(shared PVOID, SIZE_T, DWORD)
|
||||
@nogc nothrow pure @system;
|
||||
}
|
||||
|
||||
private bool unmapMemory(shared void* addr, const size_t length)
|
||||
@nogc nothrow pure @system
|
||||
else
|
||||
{
|
||||
return syscall_(cast(ptrdiff_t) addr, length, NR_munmap) == 0;
|
||||
extern(C) pragma(mangle, "mmap")
|
||||
private void* mapMemory(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
@nogc nothrow pure @system;
|
||||
|
||||
extern(C) pragma(mangle, "munmap")
|
||||
private bool unmapMemory(shared void* addr, size_t length)
|
||||
@nogc nothrow pure @system;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -206,7 +209,10 @@ final class MmapPool : Allocator
|
||||
{
|
||||
block.region.next.prev = block.region.prev;
|
||||
}
|
||||
return unmapMemory(block.region, block.region.size);
|
||||
version (Windows)
|
||||
return VirtualFree(block.region, 0, MEM_RELEASE) != 0;
|
||||
else
|
||||
return unmapMemory(block.region, block.region.size) == 0;
|
||||
}
|
||||
// Merge blocks if neigbours are free.
|
||||
if (block.next !is null && block.next.free)
|
||||
@ -394,11 +400,29 @@ final class MmapPool : Allocator
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
void* p = mapMemory(regionSize);
|
||||
if (p is null)
|
||||
version (Windows)
|
||||
{
|
||||
return null;
|
||||
void* p = VirtualAlloc(null,
|
||||
regionSize,
|
||||
MEM_COMMIT,
|
||||
PAGE_READWRITE);
|
||||
if (p is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
void* p = mapMemory(null,
|
||||
regionSize,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_PRIVATE | MAP_ANONYMOUS,
|
||||
-1,
|
||||
0);
|
||||
if (cast(ptrdiff_t) p == -1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Region region = cast(Region) p;
|
||||
@ -506,10 +530,10 @@ final class MmapPool : Allocator
|
||||
{
|
||||
// allocate() check.
|
||||
size_t tooMuchMemory = size_t.max
|
||||
- MmapPool.alignment_
|
||||
- BlockEntry.sizeof * 2
|
||||
- RegionEntry.sizeof
|
||||
- pageSize;
|
||||
- MmapPool.alignment_
|
||||
- MmapPool.BlockEntry.sizeof * 2
|
||||
- MmapPool.RegionEntry.sizeof
|
||||
- MmapPool.pageSize;
|
||||
assert(MmapPool.instance.allocate(tooMuchMemory) is null);
|
||||
|
||||
assert(MmapPool.instance.allocate(size_t.max) is null);
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Set of operations on memory blocks.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,23 +14,7 @@
|
||||
*/
|
||||
module tanya.memory.op;
|
||||
|
||||
version (TanyaNative)
|
||||
{
|
||||
extern private void fillMemory(void[], size_t) pure nothrow @system @nogc;
|
||||
|
||||
extern private void copyMemory(const void[], void[])
|
||||
pure nothrow @system @nogc;
|
||||
|
||||
extern private void moveMemory(const void[], void[])
|
||||
pure nothrow @system @nogc;
|
||||
|
||||
extern private bool equalMemory(const void[], const void[])
|
||||
pure nothrow @system @nogc;
|
||||
}
|
||||
else
|
||||
{
|
||||
import core.stdc.string;
|
||||
}
|
||||
import core.stdc.string;
|
||||
|
||||
private enum alignMask = size_t.sizeof - 1;
|
||||
|
||||
@ -52,18 +36,15 @@ private enum alignMask = size_t.sizeof - 1;
|
||||
* Precondition: $(D_INLINECODE source.length <= target.length).
|
||||
*/
|
||||
void copy(const void[] source, void[] target) @nogc nothrow pure @trusted
|
||||
in (source.length <= target.length)
|
||||
in (source.length == 0 || source.ptr !is null)
|
||||
in (target.length == 0 || target.ptr !is null)
|
||||
in
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
copyMemory(source, target);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(target.ptr, source.ptr, source.length);
|
||||
}
|
||||
assert(source.length <= target.length);
|
||||
assert(source.length == 0 || source.ptr !is null);
|
||||
assert(target.length == 0 || target.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
memcpy(target.ptr, source.ptr, source.length);
|
||||
}
|
||||
|
||||
///
|
||||
@ -98,16 +79,13 @@ private template filledBytes(ubyte Byte, ubyte I = 0)
|
||||
* memory = Memory block.
|
||||
*/
|
||||
void fill(ubyte c = 0)(void[] memory) @trusted
|
||||
in (memory.length == 0 || memory.ptr !is null)
|
||||
in
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
fillMemory(memory, filledBytes!c);
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(memory.ptr, c, memory.length);
|
||||
}
|
||||
assert(memory.length == 0 || memory.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
memset(memory.ptr, c, memory.length);
|
||||
}
|
||||
|
||||
///
|
||||
@ -144,18 +122,15 @@ in (memory.length == 0 || memory.ptr !is null)
|
||||
* Precondition: $(D_INLINECODE source.length <= target.length).
|
||||
*/
|
||||
void copyBackward(const void[] source, void[] target) @nogc nothrow pure @trusted
|
||||
in (source.length <= target.length)
|
||||
in (source.length == 0 || source.ptr !is null)
|
||||
in (target.length == 0 || target.ptr !is null)
|
||||
in
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
moveMemory(source, target);
|
||||
}
|
||||
else
|
||||
{
|
||||
memmove(target.ptr, source.ptr, source.length);
|
||||
}
|
||||
assert(source.length <= target.length);
|
||||
assert(source.length == 0 || source.ptr !is null);
|
||||
assert(target.length == 0 || target.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
memmove(target.ptr, source.ptr, source.length);
|
||||
}
|
||||
|
||||
///
|
||||
@ -182,7 +157,11 @@ in (target.length == 0 || target.ptr !is null)
|
||||
*/
|
||||
inout(void[]) find(return inout void[] haystack, ubyte needle)
|
||||
@nogc nothrow pure @trusted
|
||||
in (haystack.length == 0 || haystack.ptr !is null)
|
||||
in
|
||||
{
|
||||
assert(haystack.length == 0 || haystack.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto length = haystack.length;
|
||||
const size_t needleWord = size_t.max * needle;
|
||||
@ -260,7 +239,11 @@ in (haystack.length == 0 || haystack.ptr !is null)
|
||||
*/
|
||||
inout(char[]) findNullTerminated(return inout char[] haystack)
|
||||
@nogc nothrow pure @trusted
|
||||
in (haystack.length == 0 || haystack.ptr !is null)
|
||||
in
|
||||
{
|
||||
assert(haystack.length == 0 || haystack.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto length = haystack.length;
|
||||
enum size_t highBits = filledBytes!(0x01, 0);
|
||||
@ -327,18 +310,14 @@ in (haystack.length == 0 || haystack.ptr !is null)
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool equal(const void[] r1, const void[] r2) @nogc nothrow pure @trusted
|
||||
in (r1.length == 0 || r1.ptr !is null)
|
||||
in (r2.length == 0 || r2.ptr !is null)
|
||||
in
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
return equalMemory(r1, r2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return r1.length == r2.length
|
||||
&& memcmp(r1.ptr, r2.ptr, r1.length) == 0;
|
||||
}
|
||||
assert(r1.length == 0 || r1.ptr !is null);
|
||||
assert(r2.length == 0 || r2.ptr !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
return r1.length == r2.length && memcmp(r1.ptr, r2.ptr, r1.length) == 0;
|
||||
}
|
||||
|
||||
///
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Dynamic memory management.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -16,5 +16,3 @@ module tanya.memory;
|
||||
|
||||
public import tanya.memory.allocator;
|
||||
public import tanya.memory.lifetime;
|
||||
deprecated("Use tanya.meta.trait.stateSize instead")
|
||||
public import tanya.meta.trait : stateSize;
|
||||
|
@ -14,7 +14,7 @@
|
||||
* $(LI Unique ownership)
|
||||
* )
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -46,7 +46,11 @@ private final class RefCountedStore(T)
|
||||
|
||||
size_t opUnary(string op)()
|
||||
if (op == "--" || op == "++")
|
||||
in (this.counter > 0)
|
||||
in
|
||||
{
|
||||
assert(this.counter > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
mixin("return " ~ op ~ "counter;");
|
||||
}
|
||||
@ -127,7 +131,11 @@ struct RefCounted(T)
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.allocator_ = allocator;
|
||||
}
|
||||
@ -231,7 +239,11 @@ struct RefCounted(T)
|
||||
* Precondition: $(D_INLINECODE cound > 0).
|
||||
*/
|
||||
inout(Payload!T) get() inout
|
||||
in (count > 0, "Attempted to access an uninitialized reference")
|
||||
in
|
||||
{
|
||||
assert(count > 0, "Attempted to access an uninitialized reference");
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.storage.payload;
|
||||
}
|
||||
@ -321,7 +333,11 @@ struct RefCounted(T)
|
||||
RefCounted!T refCounted(T, A...)(shared Allocator allocator, auto ref A args)
|
||||
if (!is(T == interface) && !isAbstractClass!T
|
||||
&& !isAssociativeArray!T && !isArray!T)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto rc = typeof(return)(allocator);
|
||||
|
||||
@ -361,8 +377,12 @@ in (allocator !is null)
|
||||
*/
|
||||
RefCounted!T refCounted(T : E[], E)(shared Allocator allocator, size_t size)
|
||||
@trusted
|
||||
in (allocator !is null)
|
||||
in (size <= size_t.max / E.sizeof)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
assert(size <= size_t.max / E.sizeof);
|
||||
}
|
||||
do
|
||||
{
|
||||
return RefCounted!T(allocator.make!T(size), allocator);
|
||||
}
|
||||
@ -423,7 +443,11 @@ struct Unique(T)
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.allocator_ = allocator;
|
||||
}
|
||||
@ -604,7 +628,11 @@ struct Unique(T)
|
||||
Unique!T unique(T, A...)(shared Allocator allocator, auto ref A args)
|
||||
if (!is(T == interface) && !isAbstractClass!T
|
||||
&& !isAssociativeArray!T && !isArray!T)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto payload = allocator.make!(T, A)(args);
|
||||
return Unique!T(payload, allocator);
|
||||
@ -627,8 +655,12 @@ in (allocator !is null)
|
||||
*/
|
||||
Unique!T unique(T : E[], E)(shared Allocator allocator, size_t size)
|
||||
@trusted
|
||||
in (allocator !is null)
|
||||
in (size <= size_t.max / E.sizeof)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
assert(size <= size_t.max / E.sizeof);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto payload = allocator.resize!E(null, size);
|
||||
return Unique!T(payload, allocator);
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* This module provides a portable way of using operating system error codes.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -6,7 +6,7 @@
|
||||
* This package provides platform-independent interfaces to operating system
|
||||
* functionality.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -1,375 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Algorithms for comparing values.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/algorithm/comparison.d,
|
||||
* tanya/algorithm/comparison.d)
|
||||
*/
|
||||
module tanya.algorithm.comparison;
|
||||
|
||||
import std.traits : CommonType;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.math;
|
||||
static import tanya.memory.op;
|
||||
import tanya.meta.metafunction;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range.array;
|
||||
import tanya.range.primitive;
|
||||
|
||||
private ref inout(Args[0]) minMax(alias cmp, Args...)(ref inout Args args)
|
||||
{
|
||||
auto actual = ((ref arg) @trusted => &arg)(args[0]);
|
||||
|
||||
foreach (i, arg; args[1 .. $])
|
||||
{
|
||||
static if (isFloatingPoint!(Args[0]))
|
||||
{
|
||||
if (isNaN(arg))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (isNaN(*actual))
|
||||
{
|
||||
actual = ((ref arg) @trusted => &arg)(args[i + 1]);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (cmp(arg, *actual))
|
||||
{
|
||||
actual = ((ref arg) @trusted => &arg)(args[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
return *actual;
|
||||
}
|
||||
|
||||
private T moveIf(T)(ref T arg)
|
||||
{
|
||||
static if (hasElaborateCopyConstructor!T && isMutable!T)
|
||||
{
|
||||
return move(arg);
|
||||
}
|
||||
else
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the smallest element in the argument list or a range.
|
||||
*
|
||||
* If a range is passed, $(D_PSYMBOL min) returns a range of the same type,
|
||||
* whose front element is the smallest in the range. If more than one element
|
||||
* fulfills this condition, the front of the returned range points to
|
||||
* the first one found.
|
||||
* If $(D_PARAM range) is empty, the original range is returned.
|
||||
*
|
||||
* If $(D_PARAM Args) are floating point numbers, $(B NaN) is not considered
|
||||
* for comparison. $(B NaN) is returned only if all arguments are $(B NaN)s.
|
||||
*
|
||||
* Params:
|
||||
* Args = Types of the arguments. All arguments should have the same type.
|
||||
* Range = Forward range type.
|
||||
* args = Argument list.
|
||||
* range = Forward range.
|
||||
*
|
||||
* Returns: The smallest element.
|
||||
*/
|
||||
CommonType!Args min(Args...)(Args args)
|
||||
if (Args.length >= 2
|
||||
&& isOrderingComparable!(Args[0])
|
||||
&& allSameType!(Map!(Unqual, Args)))
|
||||
{
|
||||
return moveIf(minMax!((ref a, ref b) => a < b)(args));
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref inout(Unqual!(Args[0])) min(Args...)(ref inout Args args)
|
||||
if (Args.length >= 2
|
||||
&& isOrderingComparable!(Args[0])
|
||||
&& allSameType!(Map!(Unqual, Args)))
|
||||
{
|
||||
return minMax!((ref a, ref b) => a < b)(args);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
Range min(Range)(Range range)
|
||||
if (isForwardRange!Range && isOrderingComparable!(ElementType!Range))
|
||||
{
|
||||
if (range.empty)
|
||||
{
|
||||
return range;
|
||||
}
|
||||
auto actual = range.save;
|
||||
|
||||
range.popFront();
|
||||
for (; !range.empty; range.popFront())
|
||||
{
|
||||
if (range.front < actual.front)
|
||||
{
|
||||
actual = range.save;
|
||||
}
|
||||
}
|
||||
|
||||
return actual;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(min(1, 2) == 1);
|
||||
assert(min(3, 2) == 2);
|
||||
assert(min(3, 1, 2) == 1);
|
||||
|
||||
int[4] range = [3, 1, 1, 2];
|
||||
auto minElement = min(range[]);
|
||||
assert(minElement.front == 1);
|
||||
assert(minElement.length == 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the largest element in the argument list or a range.
|
||||
*
|
||||
* If a range is passed, $(D_PSYMBOL max) returns a range of the same type,
|
||||
* whose front element is the largest in the range. If more than one element
|
||||
* fulfills this condition, the front of the returned range points to
|
||||
* the first one found.
|
||||
* If $(D_PARAM range) is empty, the original range is returned.
|
||||
*
|
||||
* If $(D_PARAM Args) are floating point numbers, $(B NaN) is not considered
|
||||
* for comparison. $(B NaN) is returned only if all arguments are $(B NaN)s.
|
||||
*
|
||||
* Params:
|
||||
* Args = Types of the arguments. All arguments should have the same type.
|
||||
* Range = Forward range type.
|
||||
* args = Argument list.
|
||||
* range = Forward range.
|
||||
*
|
||||
* Returns: The largest element.
|
||||
*/
|
||||
CommonType!Args max(Args...)(Args args)
|
||||
if (Args.length >= 2
|
||||
&& isOrderingComparable!(Args[0])
|
||||
&& allSameType!(Map!(Unqual, Args)))
|
||||
{
|
||||
return moveIf(minMax!((ref a, ref b) => a > b)(args));
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref inout(Unqual!(Args[0])) max(Args...)(ref inout Args args)
|
||||
if (Args.length >= 2
|
||||
&& isOrderingComparable!(Args[0])
|
||||
&& allSameType!(Map!(Unqual, Args)))
|
||||
{
|
||||
return minMax!((ref a, ref b) => a > b)(args);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
Range max(Range)(Range range)
|
||||
if (isForwardRange!Range && isOrderingComparable!(ElementType!Range))
|
||||
{
|
||||
if (range.empty)
|
||||
{
|
||||
return range;
|
||||
}
|
||||
auto actual = range.save;
|
||||
|
||||
range.popFront();
|
||||
for (; !range.empty; range.popFront())
|
||||
{
|
||||
if (range.front > actual.front)
|
||||
{
|
||||
actual = range.save;
|
||||
}
|
||||
}
|
||||
|
||||
return actual;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(max(1, 2) == 2);
|
||||
assert(max(3, 2) == 3);
|
||||
assert(max(1, 3, 2) == 3);
|
||||
|
||||
int[4] range = [1, 5, 5, 2];
|
||||
auto maxElement = max(range[]);
|
||||
assert(maxElement.front == 5);
|
||||
assert(maxElement.length == 3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares element-wise two ranges for equality.
|
||||
*
|
||||
* If the ranges have different lengths, they aren't equal.
|
||||
*
|
||||
* Params:
|
||||
* pred = Predicate used to compare individual element pairs.
|
||||
* R1 = First range type.
|
||||
* R2 = Second range type.
|
||||
* r1 = First range.
|
||||
* r2 = Second range.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if both ranges are equal, $(D_KEYWORD false)
|
||||
* otherwise.
|
||||
*/
|
||||
bool equal(alias pred = (auto ref a, auto ref b) => a == b, R1, R2)
|
||||
(R1 r1, R2 r2)
|
||||
if (allSatisfy!(isInputRange, R1, R2)
|
||||
&& is(typeof(pred(r1.front, r2.front)) == bool))
|
||||
{
|
||||
static if (isDynamicArray!R1
|
||||
&& is(R1 == R2)
|
||||
&& __traits(isPOD, ElementType!R1))
|
||||
{
|
||||
return tanya.memory.op.equal(r1, r2);
|
||||
}
|
||||
else
|
||||
{
|
||||
static if (hasLength!R1 && hasLength!R2)
|
||||
{
|
||||
if (r1.length != r2.length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (; !r1.empty && !r2.empty; r1.popFront(), r2.popFront())
|
||||
{
|
||||
if (!pred(r1.front, r2.front))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
static if (hasLength!R1 && hasLength!R2)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return r1.empty && r2.empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int[2] range1 = [1, 2];
|
||||
assert(equal(range1[], range1[]));
|
||||
|
||||
int[3] range2 = [1, 2, 3];
|
||||
assert(!equal(range1[], range2[]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares element-wise two ranges for ordering.
|
||||
*
|
||||
* $(D_PSYMBOL compare) returns a negative value if $(D_PARAM r1) is less than
|
||||
* $(D_PARAM r2), a positive value if $(D_PARAM r2) is less than $(D_PARAM r1),
|
||||
* or `0` if $(D_PARAM r1) and $(D_PARAM r2) equal.
|
||||
*
|
||||
* $(D_PSYMBOL compare) iterates both ranges in lockstep. Whichever of them
|
||||
* contains an element that is greater than the respective element at the same
|
||||
* position in the other range is the greater one of the two.
|
||||
*
|
||||
* If one of the ranges becomes empty when iterating, but all elements equal so
|
||||
* far, the range with more elements is the greater one.
|
||||
*
|
||||
* If $(D_PARAM pred) is given, it is used for comparison. $(D_PARAM pred) is
|
||||
* called as $(D_INLINECODE pred(r1.front, r2.front)) and
|
||||
* $(D_INLINECODE pred(r2.front, r1.front)) to perform three-way comparison.
|
||||
* $(D_PARAM pred) should return a $(D_KEYWORD bool).
|
||||
*
|
||||
* If $(D_PARAM pred) is not given, but the element type of $(D_PARAM R1)
|
||||
* defines `opCmp()` for the element type of $(D_PARAM R2), `opCmp()` is used.
|
||||
*
|
||||
* Otherwise the comparison is perfomed using the basic comparison operators.
|
||||
*
|
||||
* Params:
|
||||
* pred = Predicate used for comparison.
|
||||
* R1 = First range type.
|
||||
* R2 = Second range type.
|
||||
* r1 = First range.
|
||||
* r2 = Second range.
|
||||
*
|
||||
* Returns: A negative value if $(D_PARAM r1) is less than $(D_PARAM r2), a
|
||||
* positive value if $D(_PARAM r2) is less than $(D_PARAM r1), `0`
|
||||
* otherwise.
|
||||
*/
|
||||
int compare(alias pred, R1, R2)(R1 r1, R2 r2)
|
||||
if (allSatisfy!(isInputRange, R1, R2)
|
||||
&& is(typeof(pred(r1.front, r2.front)) == bool)
|
||||
&& is(typeof(pred(r2.front, r1.front)) == bool))
|
||||
{
|
||||
alias predImpl = (ref r1, ref r2) {
|
||||
return pred(r2.front, r1.front) - pred(r1.front, r2.front);
|
||||
};
|
||||
return compareImpl!(predImpl, R1, R2)(r1, r2);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
int compare(R1, R2)(R1 r1, R2 r2)
|
||||
if (allSatisfy!(isInputRange, R1, R2)
|
||||
&& is(typeof(r1.front < r2.front || r2.front < r1.front)))
|
||||
{
|
||||
static if (is(typeof(r1.front.opCmp(r2.front)) == int))
|
||||
{
|
||||
alias pred = (ref r1, ref r2) => r1.front.opCmp(r2.front);
|
||||
}
|
||||
else
|
||||
{
|
||||
alias pred = (ref r1, ref r2) {
|
||||
return (r2.front < r1.front) - (r1.front < r2.front);
|
||||
};
|
||||
}
|
||||
return compareImpl!(pred, R1, R2)(r1, r2);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(compare("abc", "abc") == 0);
|
||||
assert(compare("abcd", "abc") > 0);
|
||||
assert(compare("ab", "abc") < 0);
|
||||
assert(compare("abc", "abcd") < 0);
|
||||
assert(compare("abc", "ab") > 0);
|
||||
assert(compare("aec", "abc") > 0);
|
||||
assert(compare("aac", "abc") < 0);
|
||||
assert(compare("abc", "aec") < 0);
|
||||
assert(compare("abc", "aab") > 0);
|
||||
assert(compare("aacd", "abc") < 0);
|
||||
assert(compare("abc", "aacd") > 0);
|
||||
|
||||
assert(compare!((a, b) => a > b)("aec", "abc") < 0);
|
||||
assert(compare!((a, b) => a > b)("aac", "abc") > 0);
|
||||
}
|
||||
|
||||
private int compareImpl(alias pred, R1, R2)(ref R1 r1, ref R2 r2)
|
||||
{
|
||||
for (; !r1.empty || !r2.empty; r1.popFront(), r2.popFront())
|
||||
{
|
||||
if (r1.empty)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (r2.empty)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
const comparison = pred(r1, r2);
|
||||
if (comparison != 0)
|
||||
{
|
||||
return comparison;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -11,7 +11,7 @@
|
||||
* All algorithms in this module are lazy, they request the next element of the
|
||||
* original range on demand.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2021.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -20,559 +20,15 @@
|
||||
*/
|
||||
module tanya.algorithm.iteration;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.typecons;
|
||||
import tanya.memory.lifetime;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range;
|
||||
import tanya.typecons;
|
||||
|
||||
// These predicates are used to help preserve `const` and `inout` for
|
||||
// ranges built on other ranges.
|
||||
|
||||
private enum hasInoutFront(T) = is(typeof((inout ref T a) => a.front));
|
||||
private enum hasInoutBack(T) = is(typeof((inout ref T a) => a.back));
|
||||
private enum hasInoutIndex(T) = is(typeof((inout ref T a, size_t i) => a[i]));
|
||||
|
||||
private enum hasConstEmpty(T) = is(typeof(((const T* a) => (*a).empty)(null)) : bool);
|
||||
private enum hasConstLength(T) = is(typeof(((const T* a) => (*a).length)(null)) : size_t);
|
||||
private enum hasConstSave(T) = is(typeof(((const T* a) => (*a).save())(null)) : T);
|
||||
private enum hasConstSlice(T) = is(typeof(((const T* a) => (*a)[0 .. $])(null)) : T);
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
// Test the definitions.
|
||||
static assert(hasInoutFront!string);
|
||||
static assert(hasInoutBack!string);
|
||||
static assert(hasInoutIndex!string);
|
||||
static assert(hasConstEmpty!string);
|
||||
static assert(hasConstLength!string);
|
||||
static assert(hasConstSave!string);
|
||||
static assert(hasConstSlice!string);
|
||||
|
||||
// Test that Take propagates const/inout correctly.
|
||||
alias TakeString = Take!(string, false);
|
||||
static assert(hasInoutFront!TakeString);
|
||||
static assert(hasInoutBack!TakeString);
|
||||
static assert(hasInoutIndex!TakeString);
|
||||
static assert(hasConstEmpty!TakeString);
|
||||
static assert(hasConstLength!TakeString);
|
||||
static assert(hasConstSave!TakeString);
|
||||
static assert(hasConstSlice!TakeString);
|
||||
|
||||
// Test that Retro propagates const/inout correctly.
|
||||
alias RetroString = Retro!string;
|
||||
static assert(hasInoutFront!RetroString);
|
||||
static assert(hasInoutBack!RetroString);
|
||||
static assert(hasInoutIndex!RetroString);
|
||||
static assert(hasConstEmpty!RetroString);
|
||||
static assert(hasConstLength!RetroString);
|
||||
static assert(hasConstSave!RetroString);
|
||||
static assert(hasConstSlice!RetroString);
|
||||
}
|
||||
|
||||
private struct Take(R, bool exactly)
|
||||
{
|
||||
private R source;
|
||||
size_t length_;
|
||||
|
||||
@disable this();
|
||||
|
||||
private this(R source, size_t length)
|
||||
{
|
||||
this.source = source;
|
||||
static if (!exactly && hasLength!R)
|
||||
{
|
||||
this.length_ = min(source.length, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.length_ = length;
|
||||
}
|
||||
}
|
||||
|
||||
mixin(`@property auto ref front() ` ~ (hasInoutFront!R ? `inout ` : ``) ~
|
||||
`in (!empty)
|
||||
{
|
||||
return this.source.front;
|
||||
}`);
|
||||
|
||||
void popFront()
|
||||
in (!empty)
|
||||
{
|
||||
this.source.popFront();
|
||||
--this.length_;
|
||||
}
|
||||
|
||||
mixin(`@property bool empty() ` ~ (exactly || isInfinite!R || hasConstEmpty!R ? `const ` : ``) ~
|
||||
`{
|
||||
static if (exactly || isInfinite!R)
|
||||
{
|
||||
return length == 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return this.length_ == 0 || this.source.empty;
|
||||
}
|
||||
}`);
|
||||
|
||||
static if (exactly || hasLength!R)
|
||||
{
|
||||
@property size_t length() const
|
||||
{
|
||||
return this.length_;
|
||||
}
|
||||
}
|
||||
|
||||
static if (hasAssignableElements!R)
|
||||
{
|
||||
@property void front(ref ElementType!R value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source.front = value;
|
||||
}
|
||||
|
||||
@property void front(ElementType!R value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source.front = move(value);
|
||||
}
|
||||
}
|
||||
|
||||
static if (isForwardRange!R)
|
||||
{
|
||||
mixin(`typeof(this) save() ` ~ (hasConstSave!R ? `const ` : ``) ~
|
||||
`{
|
||||
return typeof(this)(this.source.save(), length);
|
||||
}`);
|
||||
}
|
||||
static if (isRandomAccessRange!R)
|
||||
{
|
||||
mixin(`@property auto ref back() ` ~ (hasInoutBack!R ? `inout ` : ``) ~
|
||||
`in (!empty)
|
||||
{
|
||||
return this.source[this.length - 1];
|
||||
}`);
|
||||
|
||||
void popBack()
|
||||
in (!empty)
|
||||
{
|
||||
--this.length_;
|
||||
}
|
||||
|
||||
mixin(`auto ref opIndex(size_t i) ` ~ (hasInoutIndex!R ? `inout ` : ``) ~
|
||||
`in (i < length)
|
||||
{
|
||||
return this.source[i];
|
||||
}`);
|
||||
|
||||
static if (hasAssignableElements!R)
|
||||
{
|
||||
@property void back(ref ElementType!R value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source[length - 1] = value;
|
||||
}
|
||||
|
||||
@property void back(ElementType!R value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source[length - 1] = move(value);
|
||||
}
|
||||
|
||||
void opIndexAssign(ref ElementType!R value, size_t i)
|
||||
in (i < length)
|
||||
{
|
||||
this.source[i] = value;
|
||||
}
|
||||
|
||||
void opIndexAssign(ElementType!R value, size_t i)
|
||||
in (i < length)
|
||||
{
|
||||
this.source[i] = move(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static if (!exactly && hasSlicing!R)
|
||||
{
|
||||
static if (is(typeof(length))) alias opDollar = length;
|
||||
|
||||
mixin(`auto opSlice(size_t i, size_t j) ` ~ (hasConstSlice!R ? `const ` : ``) ~
|
||||
`in (i <= j)
|
||||
in (j <= length)
|
||||
{
|
||||
return typeof(this)(this.source[i .. j], length);
|
||||
}`);
|
||||
}
|
||||
|
||||
version (unittest) static assert(isInputRange!Take);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes $(D_PARAM n) elements from $(D_PARAM range).
|
||||
*
|
||||
* If $(D_PARAM range) doesn't have $(D_PARAM n) elements, the resulting range
|
||||
* spans all elements of $(D_PARAM range).
|
||||
*
|
||||
* $(D_PSYMBOL take) is particulary useful with infinite ranges. You can take
|
||||
` $(B n) elements from such range and pass the result to an algorithm which
|
||||
* expects a finit range.
|
||||
*
|
||||
* Params:
|
||||
* R = Type of the adapted range.
|
||||
* range = The range to take the elements from.
|
||||
* n = The number of elements to take.
|
||||
*
|
||||
* Returns: A range containing maximum $(D_PARAM n) first elements of
|
||||
* $(D_PARAM range).
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL takeExactly).
|
||||
*/
|
||||
auto take(R)(R range, size_t n)
|
||||
if (isInputRange!R)
|
||||
{
|
||||
static if (hasSlicing!R && hasLength!R)
|
||||
{
|
||||
if (range.length <= n)
|
||||
return range;
|
||||
else
|
||||
return range[0 .. n];
|
||||
}
|
||||
// Special case: take(take(...), n)
|
||||
else static if (is(Range == Take!(RRange, exact), RRange, bool exact))
|
||||
{
|
||||
if (n > range.length_)
|
||||
n = range.length_;
|
||||
static if (exact)
|
||||
// `take(takeExactly(r, n0), n)` is rewritten `takeExactly(r, min(n0, n))`.
|
||||
return Take!(RRange, true)(range.source, n);
|
||||
else
|
||||
// `take(take(r, n0), n)` is rewritten `take(r, min(n0, n))`.
|
||||
return Take!(RRange, false)(range.source, n);
|
||||
}
|
||||
else static if (isInfinite!R)
|
||||
{
|
||||
// If the range is infinite then `take` is the same as `takeExactly`.
|
||||
return Take!(R, true)(range, n);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Take!(R, false)(range, n);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct InfiniteRange
|
||||
{
|
||||
private size_t front_ = 1;
|
||||
|
||||
enum bool empty = false;
|
||||
|
||||
@property size_t front() @nogc nothrow pure @safe
|
||||
{
|
||||
return this.front_;
|
||||
}
|
||||
|
||||
@property void front(size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
this.front_ = i;
|
||||
}
|
||||
|
||||
void popFront() @nogc nothrow pure @safe
|
||||
{
|
||||
++this.front_;
|
||||
}
|
||||
|
||||
size_t opIndex(size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
return this.front_ + i;
|
||||
}
|
||||
|
||||
void opIndexAssign(size_t value, size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
this.front = i + value;
|
||||
}
|
||||
|
||||
InfiniteRange save() @nogc nothrow pure @safe
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
auto t = InfiniteRange().take(3);
|
||||
assert(t.length == 3);
|
||||
assert(t.front == 1);
|
||||
assert(t.back == 3);
|
||||
|
||||
t.popFront();
|
||||
assert(t.front == 2);
|
||||
assert(t.back == 3);
|
||||
|
||||
t.popBack();
|
||||
assert(t.front == 2);
|
||||
assert(t.back == 2);
|
||||
|
||||
t.popFront();
|
||||
assert(t.empty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes exactly $(D_PARAM n) elements from $(D_PARAM range).
|
||||
*
|
||||
* $(D_PARAM range) must have at least $(D_PARAM n) elements.
|
||||
*
|
||||
* $(D_PSYMBOL takeExactly) is particulary useful with infinite ranges. You can
|
||||
` take $(B n) elements from such range and pass the result to an algorithm
|
||||
* which expects a finit range.
|
||||
*
|
||||
* Params:
|
||||
* R = Type of the adapted range.
|
||||
* range = The range to take the elements from.
|
||||
* n = The number of elements to take.
|
||||
*
|
||||
* Returns: A range containing $(D_PARAM n) first elements of $(D_PARAM range).
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL take).
|
||||
*/
|
||||
auto takeExactly(R)(R range, size_t n)
|
||||
if (isInputRange!R)
|
||||
{
|
||||
static if (hasSlicing!R)
|
||||
{
|
||||
return range[0 .. n];
|
||||
}
|
||||
// Special case: takeExactly(take(range, ...), n) is takeExactly(range, n)
|
||||
else static if (is(Range == Take!(RRange, exact), RRange, bool exact))
|
||||
{
|
||||
assert(n <= range.length_);
|
||||
return Take!(RRange, true)(range.source, n);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Take!(R, true)(range, n);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct InfiniteRange
|
||||
{
|
||||
private size_t front_ = 1;
|
||||
|
||||
enum bool empty = false;
|
||||
|
||||
@property size_t front() @nogc nothrow pure @safe
|
||||
{
|
||||
return this.front_;
|
||||
}
|
||||
|
||||
@property void front(size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
this.front_ = i;
|
||||
}
|
||||
|
||||
void popFront() @nogc nothrow pure @safe
|
||||
{
|
||||
++this.front_;
|
||||
}
|
||||
|
||||
size_t opIndex(size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
return this.front_ + i;
|
||||
}
|
||||
|
||||
void opIndexAssign(size_t value, size_t i) @nogc nothrow pure @safe
|
||||
{
|
||||
this.front = i + value;
|
||||
}
|
||||
|
||||
InfiniteRange save() @nogc nothrow pure @safe
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
auto t = InfiniteRange().takeExactly(3);
|
||||
assert(t.length == 3);
|
||||
assert(t.front == 1);
|
||||
assert(t.back == 3);
|
||||
|
||||
t.popFront();
|
||||
assert(t.front == 2);
|
||||
assert(t.back == 3);
|
||||
|
||||
t.popBack();
|
||||
assert(t.front == 2);
|
||||
assert(t.back == 2);
|
||||
|
||||
t.popFront();
|
||||
assert(t.empty);
|
||||
}
|
||||
|
||||
// Reverse-access-order range returned by `retro`.
|
||||
private struct Retro(Range)
|
||||
{
|
||||
Range source;
|
||||
|
||||
@disable this();
|
||||
|
||||
private this(Range source) @safe
|
||||
{
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
mixin(`Retro save() ` ~ (hasConstSave!Range ? `const ` : ``) ~
|
||||
`{
|
||||
return Retro(source.save());
|
||||
}`);
|
||||
|
||||
mixin(`@property auto ref front() ` ~ (hasInoutBack!Range ? `inout ` : ``) ~
|
||||
`in (!empty)
|
||||
{
|
||||
return this.source.back;
|
||||
}`);
|
||||
|
||||
void popFront()
|
||||
in (!empty)
|
||||
{
|
||||
this.source.popBack();
|
||||
}
|
||||
|
||||
mixin(`@property auto ref back() ` ~ (hasInoutFront!Range ? `inout ` : ``) ~
|
||||
`in (!empty)
|
||||
{
|
||||
return this.source.front;
|
||||
}`);
|
||||
|
||||
void popBack()
|
||||
in (!empty)
|
||||
{
|
||||
this.source.popFront();
|
||||
}
|
||||
|
||||
mixin(`@property bool empty() ` ~ (hasConstEmpty!Range ? `const ` : ``) ~
|
||||
`{
|
||||
return this.source.empty;
|
||||
}`);
|
||||
|
||||
static if (hasLength!Range)
|
||||
{
|
||||
mixin(`@property size_t length() ` ~ (hasConstLength!Range ? `const ` : ``) ~
|
||||
`{
|
||||
return this.source.length;
|
||||
}`);
|
||||
}
|
||||
|
||||
static if (isRandomAccessRange!Range && hasLength!Range)
|
||||
{
|
||||
mixin(`auto ref opIndex(size_t i) ` ~ (hasInoutIndex!Range ? `inout ` : ``) ~
|
||||
`in (i < length)
|
||||
{
|
||||
return this.source[$ - ++i];
|
||||
}`);
|
||||
}
|
||||
|
||||
static if (hasLength!Range && hasSlicing!Range)
|
||||
{
|
||||
alias opDollar = length;
|
||||
|
||||
mixin(`auto opSlice(size_t i, size_t j) ` ~ (hasConstSlice!Range ? `const ` : ``) ~
|
||||
`in (i <= j)
|
||||
in (j <= length)
|
||||
{
|
||||
return typeof(this)(this.source[$-j .. $-i]);
|
||||
}`);
|
||||
}
|
||||
|
||||
static if (hasAssignableElements!Range)
|
||||
{
|
||||
@property void front(ref ElementType!Range value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source.back = value;
|
||||
}
|
||||
|
||||
@property void front(ElementType!Range value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source.back = move(value);
|
||||
}
|
||||
|
||||
@property void back(ref ElementType!Range value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source.front = value;
|
||||
}
|
||||
|
||||
@property void back(ElementType!Range value)
|
||||
in (!empty)
|
||||
{
|
||||
this.source.front = move(value);
|
||||
}
|
||||
|
||||
static if (isRandomAccessRange!Range && hasLength!Range)
|
||||
{
|
||||
void opIndexAssign(ref ElementType!Range value, size_t i)
|
||||
in (i < length)
|
||||
{
|
||||
this.source[$ - ++i] = value;
|
||||
}
|
||||
|
||||
void opIndexAssign(ElementType!Range value, size_t i)
|
||||
in (i < length)
|
||||
{
|
||||
this.source[$ - ++i] = move(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
version (unittest) static assert(isBidirectionalRange!Retro);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates a bidirectional range backwards.
|
||||
*
|
||||
* If $(D_PARAM Range) is a random-access range as well, the resulting range
|
||||
* is a random-access range too.
|
||||
*
|
||||
* Params:
|
||||
* Range = Bidirectional range type.
|
||||
* range = Bidirectional range.
|
||||
*
|
||||
* Returns: Bidirectional range with the elements order reversed.
|
||||
*/
|
||||
auto retro(Range)(Range range)
|
||||
if (isBidirectionalRange!Range)
|
||||
{
|
||||
// Special case: retro(retro(range)) is range
|
||||
static if (is(Range == Retro!RRange, RRange))
|
||||
return range.source;
|
||||
else
|
||||
return Retro!Range(range);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
|
||||
const int[3] given = [1, 2, 3];
|
||||
const int[3] expected = [3, 2, 1];
|
||||
|
||||
auto actual = retro(given[]);
|
||||
|
||||
assert(actual.length == expected.length);
|
||||
assert(!actual.empty);
|
||||
assert(equal(actual, expected[]));
|
||||
}
|
||||
|
||||
private struct SingletonByValue(E)
|
||||
{
|
||||
private Option!E element;
|
||||
private Nullable!E element;
|
||||
|
||||
@disable this();
|
||||
|
||||
@ -583,16 +39,20 @@ private struct SingletonByValue(E)
|
||||
}
|
||||
|
||||
private this(U)(ref U element)
|
||||
if (is(Unqual!U == Option!(Unqual!E)) || is(Unqual!U == Option!(const E)))
|
||||
if (is(Unqual!U == Nullable!(Unqual!E)) || is(Unqual!U == Nullable!(const E)))
|
||||
{
|
||||
if (!element.isNothing)
|
||||
if (!element.isNull)
|
||||
{
|
||||
this.element = element.get;
|
||||
}
|
||||
}
|
||||
|
||||
@property ref inout(E) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.element.get;
|
||||
}
|
||||
@ -600,21 +60,25 @@ private struct SingletonByValue(E)
|
||||
alias back = front;
|
||||
|
||||
void popFront()
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
this.element.reset();
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.element.nullify();
|
||||
}
|
||||
|
||||
alias popBack = popFront;
|
||||
|
||||
@property bool empty() const
|
||||
{
|
||||
return this.element.isNothing;
|
||||
return this.element.isNull;
|
||||
}
|
||||
|
||||
@property size_t length() const
|
||||
{
|
||||
return !this.element.isNothing;
|
||||
return !this.element.isNull;
|
||||
}
|
||||
|
||||
auto save()
|
||||
@ -622,14 +86,13 @@ private struct SingletonByValue(E)
|
||||
return SingletonByValue!E(this.element);
|
||||
}
|
||||
|
||||
auto save() const
|
||||
{
|
||||
return SingletonByValue!(const E)(this.element);
|
||||
}
|
||||
|
||||
ref inout(E) opIndex(size_t i) inout
|
||||
in (!empty)
|
||||
in (i == 0)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(i == 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.element.get;
|
||||
}
|
||||
@ -647,7 +110,11 @@ private struct SingletonByRef(E)
|
||||
}
|
||||
|
||||
@property ref inout(E) front() inout return
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.element;
|
||||
}
|
||||
@ -655,7 +122,11 @@ private struct SingletonByRef(E)
|
||||
alias back = front;
|
||||
|
||||
void popFront()
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.element = null;
|
||||
}
|
||||
@ -677,14 +148,13 @@ private struct SingletonByRef(E)
|
||||
return typeof(this)(*this.element);
|
||||
}
|
||||
|
||||
auto save() const return
|
||||
{
|
||||
return SingletonByRef!(const E)(*this.element);
|
||||
}
|
||||
|
||||
ref inout(E) opIndex(size_t i) inout return
|
||||
in (!empty)
|
||||
in (i == 0)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(i == 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.element;
|
||||
}
|
||||
@ -731,61 +201,9 @@ auto singleton(E)(return ref E element)
|
||||
/**
|
||||
* Accumulates all elements of a range using a function.
|
||||
*
|
||||
* $(D_PSYMBOL foldl) takes a function, an input range and the initial value.
|
||||
* The function takes this initial value and the first element of the range (in
|
||||
* this order), puts them together and returns the result. The return
|
||||
* type of the function should be the same as the type of the initial value.
|
||||
* This is than repeated for all the remaining elements of the range, whereby
|
||||
* the value returned by the passed function is used at the place of the
|
||||
* initial value.
|
||||
*
|
||||
* $(D_PSYMBOL foldl) accumulates from left to right.
|
||||
*
|
||||
* Params:
|
||||
* F = Callable accepting the accumulator and a range element.
|
||||
*/
|
||||
template foldl(F...)
|
||||
if (F.length == 1)
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* R = Input range type.
|
||||
* T = Type of the accumulated value.
|
||||
* range = Input range.
|
||||
* init = Initial value.
|
||||
*
|
||||
* Returns: Accumulated value.
|
||||
*/
|
||||
auto foldl(R, T)(R range, auto ref T init)
|
||||
if (isInputRange!R && !isInfinite!R)
|
||||
{
|
||||
if (range.empty)
|
||||
{
|
||||
return init;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto acc = F[0](init, getAndPopFront(range));
|
||||
return foldl(range, acc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int[3] range = [1, 2, 3];
|
||||
const actual = foldl!((acc, x) => acc + x)(range[], 0);
|
||||
|
||||
assert(actual == 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accumulates all elements of a range using a function.
|
||||
*
|
||||
* $(D_PSYMBOL foldr) takes a function, an input range and the initial value.
|
||||
* The function takes this initial value and the first element of the range (in
|
||||
* this order), puts them together and returns the result. The return
|
||||
* $(D_PSYMBOL foldr) takes a function, a bidirectional range and the initial
|
||||
* value. The function takes this initial value and the first element of the
|
||||
* range (in this order), puts them together and returns the result. The return
|
||||
* type of the function should be the same as the type of the initial value.
|
||||
* This is than repeated for all the remaining elements of the range, whereby
|
||||
* the value returned by the passed function is used at the place of the
|
||||
@ -808,7 +226,7 @@ if (F.length == 1)
|
||||
*
|
||||
* Returns: Accumulated value.
|
||||
*/
|
||||
auto foldr(R, T)(R range, auto ref T init)
|
||||
auto foldr(R, T)(scope R range, auto ref T init)
|
||||
if (isBidirectionalRange!R)
|
||||
{
|
||||
if (range.empty)
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Algorithms that modify its arguments.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -20,15 +20,6 @@ import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range;
|
||||
|
||||
deprecated("Use tanya.memory.lifetime.swap instead")
|
||||
alias swap = tanya.memory.lifetime.swap;
|
||||
|
||||
deprecated("Use tanya.memory.lifetime.moveEmplace instead")
|
||||
alias moveEmplace = tanya.memory.lifetime.moveEmplace;
|
||||
|
||||
deprecated("Use tanya.memory.lifetime.move instead")
|
||||
alias move = tanya.memory.lifetime.move;
|
||||
|
||||
/**
|
||||
* Copies the $(D_PARAM source) range into the $(D_PARAM target) range.
|
||||
*
|
||||
@ -86,7 +77,7 @@ do
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
import std.algorithm.comparison : equal;
|
||||
|
||||
const int[2] source = [1, 2];
|
||||
int[2] target = [3, 4];
|
||||
@ -123,7 +114,7 @@ if (isInputRange!Range && isAssignable!(ElementType!Range, Value))
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
import std.algorithm.comparison : equal;
|
||||
|
||||
int[6] actual;
|
||||
const int[6] expected = [1, 1, 1, 1, 1, 1];
|
||||
@ -163,7 +154,7 @@ if (isInputRange!Range && hasLvalueElements!Range
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
import std.algorithm.comparison : equal;
|
||||
|
||||
int[6] actual = void;
|
||||
const int[6] expected = [1, 1, 1, 1, 1, 1];
|
||||
@ -186,9 +177,7 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
||||
import tanya.memory.op : copy, fill;
|
||||
alias T = ElementType!Range;
|
||||
|
||||
static if (__VERSION__ >= 2083
|
||||
&& isDynamicArray!Range
|
||||
&& __traits(isZeroInit, T))
|
||||
static if (isDynamicArray!Range && __traits(isZeroInit, T))
|
||||
{
|
||||
fill!0(range);
|
||||
}
|
||||
@ -205,7 +194,7 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
import std.algorithm.comparison : equal;
|
||||
|
||||
int[2] actual = void;
|
||||
const int[2] expected = [0, 0];
|
||||
@ -253,54 +242,3 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
||||
|
||||
assert(counter == 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotates the elements of a union of two ranges.
|
||||
*
|
||||
* Performs a left rotation on the given ranges, as if it would be a signle
|
||||
* range, so that [`front.front`, `back.front`$(RPAREN) is a valid range, that
|
||||
* is $(D_PARAM back) would continue $(D_PARAM front).
|
||||
*
|
||||
* The elements are moved so, that the first element of $(D_PARAM back) becomes
|
||||
* the first element of $(D_PARAM front) without changing the relative order of
|
||||
* their elements.
|
||||
*
|
||||
* Params:
|
||||
* Range = Range type.
|
||||
* front = Left half.
|
||||
* back = Right half.
|
||||
*/
|
||||
void rotate(Range)(Range front, Range back)
|
||||
if (isForwardRange!Range && hasSwappableElements!Range)
|
||||
{
|
||||
auto next = back.save();
|
||||
|
||||
while (!front.empty && !next.empty && !sameHead(front, next))
|
||||
{
|
||||
tanya.memory.lifetime.swap(front.front, next.front);
|
||||
front.popFront();
|
||||
next.popFront();
|
||||
|
||||
if (next.empty)
|
||||
{
|
||||
next = back.save();
|
||||
}
|
||||
else if (front.empty)
|
||||
{
|
||||
front = back.save();
|
||||
back = next.save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
|
||||
const int[7] expected = [1, 2, 3, 4, 5, 6, 7];
|
||||
int[7] actual = [5, 6, 3, 4, 1, 2, 7];
|
||||
|
||||
rotate(actual[0 .. 2], actual[4 .. 6]);
|
||||
assert(equal(actual[], expected[]));
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Collection of generic algorithms.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2021.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,7 +14,5 @@
|
||||
*/
|
||||
module tanya.algorithm;
|
||||
|
||||
public import tanya.algorithm.comparison;
|
||||
public import tanya.algorithm.iteration;
|
||||
public import tanya.algorithm.mutation;
|
||||
public import tanya.algorithm.searching;
|
||||
|
@ -1,53 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Searching algorithms.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/algorithm/searching.d,
|
||||
* tanya/algorithm/searching.d)
|
||||
*/
|
||||
module tanya.algorithm.searching;
|
||||
|
||||
import tanya.range;
|
||||
|
||||
/**
|
||||
* Counts the elements in an input range.
|
||||
*
|
||||
* If $(D_PARAM R) has length, $(D_PSYMBOL count) returns it, otherwise it
|
||||
* iterates over the range and counts the elements.
|
||||
*
|
||||
* Params:
|
||||
* R = Input range type.
|
||||
* range = Input range.
|
||||
*
|
||||
* Returns: $(D_PARAM range) length.
|
||||
*/
|
||||
size_t count(R)(R range)
|
||||
if (isInputRange!R)
|
||||
{
|
||||
static if (hasLength!R)
|
||||
{
|
||||
return range.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t counter;
|
||||
for (; !range.empty; range.popFront(), ++counter)
|
||||
{
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int[3] array;
|
||||
assert(count(array) == 3);
|
||||
}
|
@ -1,187 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Event loop implementation for Linux.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/epoll.d,
|
||||
* tanya/async/event/epoll.d)
|
||||
*/
|
||||
module tanya.async.event.epoll;
|
||||
|
||||
version (D_Ddoc)
|
||||
{
|
||||
}
|
||||
else version (linux):
|
||||
|
||||
import core.stdc.errno;
|
||||
public import core.sys.linux.epoll;
|
||||
import core.sys.posix.unistd;
|
||||
import core.time;
|
||||
import tanya.algorithm.comparison;
|
||||
import tanya.async.event.selector;
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.container.array;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.network.socket;
|
||||
|
||||
extern (C) nothrow @nogc
|
||||
{
|
||||
int epoll_create1(int flags);
|
||||
int epoll_ctl (int epfd, int op, int fd, epoll_event *event);
|
||||
int epoll_wait (int epfd, epoll_event *events, int maxevents, int timeout);
|
||||
}
|
||||
|
||||
final class EpollLoop : SelectorLoop
|
||||
{
|
||||
protected int fd;
|
||||
private Array!epoll_event events;
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this() @nogc
|
||||
{
|
||||
if ((fd = epoll_create1(EPOLL_CLOEXEC)) < 0)
|
||||
{
|
||||
throw defaultAllocator.make!BadLoopException("epoll initialization failed");
|
||||
}
|
||||
super();
|
||||
events = Array!epoll_event(maxEvents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees loop internals.
|
||||
*/
|
||||
~this() @nogc
|
||||
{
|
||||
close(fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
protected override bool reify(SocketWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events) @nogc
|
||||
{
|
||||
int op = EPOLL_CTL_DEL;
|
||||
epoll_event ev;
|
||||
|
||||
if (events == oldEvents)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (events && oldEvents)
|
||||
{
|
||||
op = EPOLL_CTL_MOD;
|
||||
}
|
||||
else if (events && !oldEvents)
|
||||
{
|
||||
op = EPOLL_CTL_ADD;
|
||||
}
|
||||
|
||||
ev.data.fd = watcher.socket.handle;
|
||||
ev.events = (events & (Event.read | Event.accept) ? EPOLLIN | EPOLLPRI : 0)
|
||||
| (events & Event.write ? EPOLLOUT : 0)
|
||||
| EPOLLET;
|
||||
|
||||
return epoll_ctl(fd, op, watcher.socket.handle, &ev) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
protected override void poll() @nogc
|
||||
{
|
||||
// Don't block
|
||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||
auto eventCount = epoll_wait(fd, events.get().ptr, maxEvents, timeout);
|
||||
|
||||
if (eventCount < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
throw defaultAllocator.make!BadLoopException();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = 0; i < eventCount; ++i)
|
||||
{
|
||||
auto transport = cast(StreamTransport) connections[events[i].data.fd];
|
||||
|
||||
if (transport is null)
|
||||
{
|
||||
auto connection = cast(ConnectionWatcher) connections[events[i].data.fd];
|
||||
assert(connection !is null);
|
||||
|
||||
acceptConnections(connection);
|
||||
}
|
||||
else if (events[i].events & EPOLLERR)
|
||||
{
|
||||
kill(transport);
|
||||
continue;
|
||||
}
|
||||
else if (events[i].events & (EPOLLIN | EPOLLPRI | EPOLLHUP))
|
||||
{
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
ptrdiff_t received;
|
||||
do
|
||||
{
|
||||
received = transport.socket.receive(transport.output[]);
|
||||
transport.output += received;
|
||||
}
|
||||
while (received);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
if (transport.socket.disconnected)
|
||||
{
|
||||
kill(transport, exception);
|
||||
continue;
|
||||
}
|
||||
else if (transport.output.length)
|
||||
{
|
||||
pendings.insertBack(transport);
|
||||
}
|
||||
}
|
||||
if (events[i].events & EPOLLOUT)
|
||||
{
|
||||
transport.writeReady = true;
|
||||
if (transport.input.length)
|
||||
{
|
||||
feed(transport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
override protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow
|
||||
{
|
||||
return min(super.blockTime, 1.dur!"seconds");
|
||||
}
|
||||
}
|
@ -1,378 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Event loop implementation for Windows.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/iocp.d,
|
||||
* tanya/async/event/iocp.d)
|
||||
*/
|
||||
module tanya.async.event.iocp;
|
||||
|
||||
version (D_Ddoc)
|
||||
{
|
||||
}
|
||||
else version (Windows):
|
||||
|
||||
import core.sys.windows.mswsock;
|
||||
import core.sys.windows.winsock2;
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.container.buffer;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.network.socket;
|
||||
import tanya.sys.windows.winbase;
|
||||
|
||||
/**
|
||||
* Transport for stream sockets.
|
||||
*/
|
||||
final class StreamTransport : SocketWatcher, DuplexTransport, SocketTransport
|
||||
{
|
||||
private SocketException exception;
|
||||
|
||||
private ReadBuffer!ubyte output;
|
||||
|
||||
private WriteBuffer!ubyte input;
|
||||
|
||||
private Protocol protocol_;
|
||||
|
||||
private bool closing;
|
||||
|
||||
/**
|
||||
* Creates new completion port transport.
|
||||
*
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
*
|
||||
* Precondition: $(D_INLINECODE socket !is null)
|
||||
*/
|
||||
this(OverlappedConnectedSocket socket) @nogc
|
||||
{
|
||||
super(socket);
|
||||
output = ReadBuffer!ubyte(8192, 1024);
|
||||
input = WriteBuffer!ubyte(8192);
|
||||
active = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*
|
||||
* Postcondition: $(D_INLINECODE socket !is null)
|
||||
*/
|
||||
override @property OverlappedConnectedSocket socket() pure nothrow @safe @nogc
|
||||
out (socket; socket !is null)
|
||||
{
|
||||
return cast(OverlappedConnectedSocket) socket_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns $(D_PARAM true) if the transport is closing or closed.
|
||||
*/
|
||||
bool isClosing() const pure nothrow @safe @nogc
|
||||
{
|
||||
return closing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the transport.
|
||||
*
|
||||
* Buffered data will be flushed. No more data will be received.
|
||||
*/
|
||||
void close() pure nothrow @safe @nogc
|
||||
{
|
||||
closing = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data) @nogc
|
||||
{
|
||||
input ~= data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Application protocol.
|
||||
*/
|
||||
@property Protocol protocol() pure nothrow @safe @nogc
|
||||
{
|
||||
return protocol_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches the protocol.
|
||||
*
|
||||
* The protocol is deallocated by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* protocol = Application protocol.
|
||||
*
|
||||
* Precondition: $(D_INLINECODE protocol !is null)
|
||||
*/
|
||||
@property void protocol(Protocol protocol) pure nothrow @safe @nogc
|
||||
in (protocol !is null)
|
||||
{
|
||||
protocol_ = protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the watcher callback.
|
||||
*/
|
||||
override void invoke() @nogc
|
||||
{
|
||||
if (output.length)
|
||||
{
|
||||
immutable empty = input.length == 0;
|
||||
protocol.received(output[0 .. $]);
|
||||
output.clear();
|
||||
if (empty)
|
||||
{
|
||||
SocketState overlapped;
|
||||
try
|
||||
{
|
||||
overlapped = defaultAllocator.make!SocketState;
|
||||
socket.beginSend(input[], overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
defaultAllocator.dispose(overlapped);
|
||||
defaultAllocator.dispose(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol.disconnected(exception);
|
||||
defaultAllocator.dispose(protocol_);
|
||||
defaultAllocator.dispose(exception);
|
||||
active = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final class IOCPLoop : Loop
|
||||
{
|
||||
protected HANDLE completionPort;
|
||||
|
||||
protected OVERLAPPED overlap;
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this() @nogc
|
||||
{
|
||||
super();
|
||||
|
||||
completionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, null, 0, 0);
|
||||
if (!completionPort)
|
||||
{
|
||||
throw make!BadLoopException(defaultAllocator,
|
||||
"Creating completion port failed");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
override protected bool reify(SocketWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events) @nogc
|
||||
{
|
||||
SocketState overlapped;
|
||||
if (!(oldEvents & Event.accept) && (events & Event.accept))
|
||||
{
|
||||
auto socket = cast(OverlappedStreamSocket) watcher.socket;
|
||||
assert(socket !is null);
|
||||
|
||||
if (CreateIoCompletionPort(cast(HANDLE) socket.handle,
|
||||
completionPort,
|
||||
cast(size_t) (cast(void*) watcher),
|
||||
0) !is completionPort)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
overlapped = defaultAllocator.make!SocketState;
|
||||
socket.beginAccept(overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
defaultAllocator.dispose(overlapped);
|
||||
defaultAllocator.dispose(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ((!(oldEvents & Event.read) && (events & Event.read))
|
||||
|| (!(oldEvents & Event.write) && (events & Event.write)))
|
||||
{
|
||||
auto transport = cast(StreamTransport) watcher;
|
||||
assert(transport !is null);
|
||||
|
||||
if (CreateIoCompletionPort(cast(HANDLE) transport.socket.handle,
|
||||
completionPort,
|
||||
cast(size_t) (cast(void*) watcher),
|
||||
0) !is completionPort)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Begin to read
|
||||
if (!(oldEvents & Event.read) && (events & Event.read))
|
||||
{
|
||||
try
|
||||
{
|
||||
overlapped = defaultAllocator.make!SocketState;
|
||||
transport.socket.beginReceive(transport.output[], overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
defaultAllocator.dispose(overlapped);
|
||||
defaultAllocator.dispose(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void kill(StreamTransport transport,
|
||||
SocketException exception = null) @nogc
|
||||
in (transport !is null)
|
||||
{
|
||||
transport.socket.shutdown();
|
||||
defaultAllocator.dispose(transport.socket);
|
||||
transport.exception = exception;
|
||||
pendings.insertBack(transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
override protected void poll() @nogc
|
||||
{
|
||||
DWORD lpNumberOfBytes;
|
||||
size_t key;
|
||||
OVERLAPPED* overlap;
|
||||
immutable timeout = cast(immutable int) blockTime.total!"msecs";
|
||||
|
||||
auto result = GetQueuedCompletionStatus(completionPort,
|
||||
&lpNumberOfBytes,
|
||||
&key,
|
||||
&overlap,
|
||||
timeout);
|
||||
if (result == FALSE && overlap is null)
|
||||
{
|
||||
return; // Timeout
|
||||
}
|
||||
|
||||
enum size_t offset = size_t.sizeof * 2;
|
||||
auto overlapped = cast(SocketState) ((cast(void*) overlap) - offset);
|
||||
assert(overlapped !is null);
|
||||
scope (failure)
|
||||
{
|
||||
defaultAllocator.dispose(overlapped);
|
||||
}
|
||||
|
||||
switch (overlapped.event)
|
||||
{
|
||||
case OverlappedSocketEvent.accept:
|
||||
auto connection = cast(ConnectionWatcher) (cast(void*) key);
|
||||
assert(connection !is null);
|
||||
|
||||
auto listener = cast(OverlappedStreamSocket) connection.socket;
|
||||
assert(listener !is null);
|
||||
|
||||
auto socket = listener.endAccept(overlapped);
|
||||
auto transport = defaultAllocator.make!StreamTransport(socket);
|
||||
|
||||
connection.incoming.insertBack(transport);
|
||||
|
||||
reify(transport,
|
||||
EventMask(Event.none),
|
||||
EventMask(Event.read | Event.write));
|
||||
|
||||
pendings.insertBack(connection);
|
||||
listener.beginAccept(overlapped);
|
||||
break;
|
||||
case OverlappedSocketEvent.read:
|
||||
auto transport = cast(StreamTransport) (cast(void*) key);
|
||||
assert(transport !is null);
|
||||
|
||||
if (!transport.active)
|
||||
{
|
||||
defaultAllocator.dispose(transport);
|
||||
defaultAllocator.dispose(overlapped);
|
||||
return;
|
||||
}
|
||||
|
||||
int received;
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
received = transport.socket.endReceive(overlapped);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
if (transport.socket.disconnected)
|
||||
{
|
||||
// We want to get one last notification to destroy the watcher.
|
||||
transport.socket.beginReceive(transport.output[], overlapped);
|
||||
kill(transport, exception);
|
||||
}
|
||||
else if (received > 0)
|
||||
{
|
||||
immutable full = transport.output.free == received;
|
||||
|
||||
transport.output += received;
|
||||
// Receive was interrupted because the buffer is full. We have to continue.
|
||||
if (full)
|
||||
{
|
||||
transport.socket.beginReceive(transport.output[], overlapped);
|
||||
}
|
||||
pendings.insertBack(transport);
|
||||
}
|
||||
break;
|
||||
case OverlappedSocketEvent.write:
|
||||
auto transport = cast(StreamTransport) (cast(void*) key);
|
||||
assert(transport !is null);
|
||||
|
||||
transport.input += transport.socket.endSend(overlapped);
|
||||
if (transport.input.length > 0)
|
||||
{
|
||||
transport.socket.beginSend(transport.input[], overlapped);
|
||||
}
|
||||
else
|
||||
{
|
||||
transport.socket.beginReceive(transport.output[], overlapped);
|
||||
if (transport.isClosing())
|
||||
{
|
||||
kill(transport);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
assert(false, "Unknown event");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,331 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* Event loop implementation for *BSD.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/kqueue.d,
|
||||
* tanya/async/event/kqueue.d)
|
||||
*/
|
||||
module tanya.async.event.kqueue;
|
||||
|
||||
version (D_Ddoc)
|
||||
{
|
||||
}
|
||||
else version (OSX)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (iOS)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (TVOS)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (WatchOS)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (FreeBSD)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (OpenBSD)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
else version (DragonFlyBSD)
|
||||
{
|
||||
version = MacBSD;
|
||||
}
|
||||
|
||||
version (MacBSD):
|
||||
|
||||
import core.stdc.errno;
|
||||
import core.sys.posix.time; // timespec
|
||||
import core.sys.posix.unistd;
|
||||
import core.time;
|
||||
import tanya.algorithm.comparison;
|
||||
import tanya.async.event.selector;
|
||||
import tanya.async.loop;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.container.array;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.network.socket;
|
||||
|
||||
void EV_SET(kevent_t* kevp, typeof(kevent_t.tupleof) args) pure nothrow @nogc
|
||||
{
|
||||
*kevp = kevent_t(args);
|
||||
}
|
||||
|
||||
enum : short
|
||||
{
|
||||
EVFILT_READ = -1,
|
||||
EVFILT_WRITE = -2,
|
||||
EVFILT_AIO = -3, /* attached to aio requests */
|
||||
EVFILT_VNODE = -4, /* attached to vnodes */
|
||||
EVFILT_PROC = -5, /* attached to struct proc */
|
||||
EVFILT_SIGNAL = -6, /* attached to struct proc */
|
||||
EVFILT_TIMER = -7, /* timers */
|
||||
EVFILT_MACHPORT = -8, /* Mach portsets */
|
||||
EVFILT_FS = -9, /* filesystem events */
|
||||
EVFILT_USER = -10, /* User events */
|
||||
EVFILT_VM = -12, /* virtual memory events */
|
||||
EVFILT_SYSCOUNT = 11
|
||||
}
|
||||
|
||||
struct kevent_t
|
||||
{
|
||||
uintptr_t ident; // Identifier for this event
|
||||
short filter; // Filter for event
|
||||
ushort flags;
|
||||
uint fflags;
|
||||
intptr_t data;
|
||||
void* udata; // Opaque user data identifier
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
/* actions */
|
||||
EV_ADD = 0x0001, /* add event to kq (implies enable) */
|
||||
EV_DELETE = 0x0002, /* delete event from kq */
|
||||
EV_ENABLE = 0x0004, /* enable event */
|
||||
EV_DISABLE = 0x0008, /* disable event (not reported) */
|
||||
|
||||
/* flags */
|
||||
EV_ONESHOT = 0x0010, /* only report one occurrence */
|
||||
EV_CLEAR = 0x0020, /* clear event state after reporting */
|
||||
EV_RECEIPT = 0x0040, /* force EV_ERROR on success, data=0 */
|
||||
EV_DISPATCH = 0x0080, /* disable event after reporting */
|
||||
|
||||
EV_SYSFLAGS = 0xF000, /* reserved by system */
|
||||
EV_FLAG1 = 0x2000, /* filter-specific flag */
|
||||
|
||||
/* returned values */
|
||||
EV_EOF = 0x8000, /* EOF detected */
|
||||
EV_ERROR = 0x4000, /* error, data contains errno */
|
||||
}
|
||||
|
||||
extern(C) int kqueue() nothrow @nogc;
|
||||
extern(C) int kevent(int kq, const kevent_t *changelist, int nchanges,
|
||||
kevent_t *eventlist, int nevents, const timespec *timeout)
|
||||
nothrow @nogc;
|
||||
|
||||
final class KqueueLoop : SelectorLoop
|
||||
{
|
||||
protected int fd;
|
||||
private Array!kevent_t events;
|
||||
private Array!kevent_t changes;
|
||||
private size_t changeCount;
|
||||
|
||||
/**
|
||||
* Returns: Maximal event count can be got at a time
|
||||
* (should be supported by the backend).
|
||||
*/
|
||||
override protected @property uint maxEvents()
|
||||
const pure nothrow @safe @nogc
|
||||
{
|
||||
return cast(uint) events.length;
|
||||
}
|
||||
|
||||
this() @nogc
|
||||
{
|
||||
super();
|
||||
|
||||
if ((fd = kqueue()) == -1)
|
||||
{
|
||||
throw make!BadLoopException(defaultAllocator,
|
||||
"kqueue initialization failed");
|
||||
}
|
||||
events = Array!kevent_t(64);
|
||||
changes = Array!kevent_t(64);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees loop internals.
|
||||
*/
|
||||
~this() @nogc
|
||||
{
|
||||
close(fd);
|
||||
}
|
||||
|
||||
private void set(SocketType socket, short filter, ushort flags) @nogc
|
||||
{
|
||||
if (changes.length <= changeCount)
|
||||
{
|
||||
changes.length = changeCount + maxEvents;
|
||||
}
|
||||
EV_SET(&changes[changeCount],
|
||||
cast(ulong) socket,
|
||||
filter,
|
||||
flags,
|
||||
0U,
|
||||
0,
|
||||
null);
|
||||
++changeCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
override protected bool reify(SocketWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events) @nogc
|
||||
{
|
||||
if (events != oldEvents)
|
||||
{
|
||||
if (oldEvents & Event.read || oldEvents & Event.accept)
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_READ, EV_DELETE);
|
||||
}
|
||||
if (oldEvents & Event.write)
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_WRITE, EV_DELETE);
|
||||
}
|
||||
}
|
||||
if (events & (Event.read | events & Event.accept))
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_READ, EV_ADD | EV_ENABLE);
|
||||
}
|
||||
if (events & Event.write)
|
||||
{
|
||||
set(watcher.socket.handle, EVFILT_WRITE, EV_ADD | EV_DISPATCH);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
protected override void poll() @nogc
|
||||
{
|
||||
timespec ts;
|
||||
blockTime.split!("seconds", "nsecs")(ts.tv_sec, ts.tv_nsec);
|
||||
|
||||
if (changeCount > maxEvents)
|
||||
{
|
||||
events.length = changes.length;
|
||||
}
|
||||
|
||||
auto eventCount = kevent(fd,
|
||||
changes.get().ptr,
|
||||
cast(int) changeCount,
|
||||
events.get().ptr,
|
||||
maxEvents,
|
||||
&ts);
|
||||
changeCount = 0;
|
||||
|
||||
if (eventCount < 0)
|
||||
{
|
||||
if (errno != EINTR)
|
||||
{
|
||||
throw defaultAllocator.make!BadLoopException();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i; i < eventCount; ++i)
|
||||
{
|
||||
assert(connections.length > events[i].ident);
|
||||
|
||||
auto transport = cast(StreamTransport) connections[events[i].ident];
|
||||
// If it is a ConnectionWatcher. Accept connections.
|
||||
if (transport is null)
|
||||
{
|
||||
auto connection = cast(ConnectionWatcher) connections[events[i].ident];
|
||||
assert(connection !is null);
|
||||
|
||||
acceptConnections(connection);
|
||||
}
|
||||
else if (events[i].flags & EV_ERROR)
|
||||
{
|
||||
kill(transport);
|
||||
}
|
||||
else if (events[i].filter == EVFILT_READ)
|
||||
{
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
ptrdiff_t received;
|
||||
do
|
||||
{
|
||||
received = transport.socket.receive(transport.output[]);
|
||||
transport.output += received;
|
||||
}
|
||||
while (received);
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
}
|
||||
if (transport.socket.disconnected)
|
||||
{
|
||||
kill(transport, exception);
|
||||
}
|
||||
else if (transport.output.length)
|
||||
{
|
||||
pendings.insertBack(transport);
|
||||
}
|
||||
}
|
||||
else if (events[i].filter == EVFILT_WRITE)
|
||||
{
|
||||
transport.writeReady = true;
|
||||
if (transport.input.length)
|
||||
{
|
||||
feed(transport);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
override protected @property inout(Duration) blockTime()
|
||||
inout @nogc @safe pure nothrow
|
||||
{
|
||||
return min(super.blockTime, 1.dur!"seconds");
|
||||
}
|
||||
|
||||
/**
|
||||
* If the transport couldn't send the data, the further sending should
|
||||
* be handled by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* exception = Exception thrown on sending.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
||||
* transport will be destroyed then).
|
||||
*/
|
||||
protected override bool feed(StreamTransport transport,
|
||||
SocketException exception = null) @nogc
|
||||
{
|
||||
if (!super.feed(transport, exception))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!transport.writeReady)
|
||||
{
|
||||
set(transport.socket.handle, EVFILT_WRITE, EV_DISPATCH);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,383 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* This module contains base implementations for reactor event loops.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/event/selector.d,
|
||||
* tanya/async/event/selector.d)
|
||||
*/
|
||||
module tanya.async.event.selector;
|
||||
|
||||
version (D_Ddoc)
|
||||
{
|
||||
}
|
||||
else version (Posix):
|
||||
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.container.array;
|
||||
import tanya.container.buffer;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.network.socket;
|
||||
|
||||
/**
|
||||
* Transport for stream sockets.
|
||||
*/
|
||||
package class StreamTransport : SocketWatcher, DuplexTransport, SocketTransport
|
||||
{
|
||||
private SelectorLoop loop;
|
||||
|
||||
private SocketException exception;
|
||||
|
||||
package ReadBuffer!ubyte output;
|
||||
|
||||
package WriteBuffer!ubyte input;
|
||||
|
||||
private Protocol protocol_;
|
||||
|
||||
private bool closing;
|
||||
|
||||
/// Received notification that the underlying socket is write-ready.
|
||||
package bool writeReady;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* loop = Event loop.
|
||||
* socket = Socket.
|
||||
*
|
||||
* Precondition: $(D_INLINECODE loop !is null && socket !is null)
|
||||
*/
|
||||
this(SelectorLoop loop, ConnectedSocket socket) @nogc
|
||||
in
|
||||
{
|
||||
assert(loop !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
super(socket);
|
||||
this.loop = loop;
|
||||
output = ReadBuffer!ubyte(8192, 1024);
|
||||
input = WriteBuffer!ubyte(8192);
|
||||
active = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*
|
||||
* Postcondition: $(D_INLINECODE socket !is null)
|
||||
*/
|
||||
override @property ConnectedSocket socket() pure nothrow @safe @nogc
|
||||
out (socket; socket !is null)
|
||||
{
|
||||
return cast(ConnectedSocket) socket_;
|
||||
}
|
||||
|
||||
private @property void socket(ConnectedSocket socket)
|
||||
pure nothrow @safe @nogc
|
||||
in (socket !is null)
|
||||
{
|
||||
socket_ = socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Application protocol.
|
||||
*/
|
||||
@property Protocol protocol() pure nothrow @safe @nogc
|
||||
{
|
||||
return protocol_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches the protocol.
|
||||
*
|
||||
* The protocol is deallocated by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* protocol = Application protocol.
|
||||
*
|
||||
* Precondition: $(D_INLINECODE protocol !is null)
|
||||
*/
|
||||
@property void protocol(Protocol protocol) pure nothrow @safe @nogc
|
||||
in (protocol !is null)
|
||||
{
|
||||
protocol_ = protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns $(D_PARAM true) if the transport is closing or closed.
|
||||
*/
|
||||
bool isClosing() const pure nothrow @safe @nogc
|
||||
{
|
||||
return closing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the transport.
|
||||
*
|
||||
* Buffered data will be flushed. No more data will be received.
|
||||
*/
|
||||
void close() @nogc
|
||||
{
|
||||
closing = true;
|
||||
loop.reify(this,
|
||||
EventMask(Event.read | Event.write),
|
||||
EventMask(Event.write));
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the watcher callback.
|
||||
*/
|
||||
override void invoke() @nogc
|
||||
{
|
||||
if (output.length)
|
||||
{
|
||||
protocol.received(output[0 .. $]);
|
||||
output.clear();
|
||||
if (isClosing() && input.length == 0)
|
||||
{
|
||||
loop.kill(this);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
protocol.disconnected(exception);
|
||||
defaultAllocator.dispose(protocol_);
|
||||
defaultAllocator.dispose(exception);
|
||||
active = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data) @nogc
|
||||
{
|
||||
if (!data.length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Try to write if the socket is write ready.
|
||||
if (writeReady)
|
||||
{
|
||||
ptrdiff_t sent;
|
||||
SocketException exception;
|
||||
try
|
||||
{
|
||||
sent = socket.send(data);
|
||||
if (sent == 0)
|
||||
{
|
||||
writeReady = false;
|
||||
}
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
writeReady = false;
|
||||
exception = e;
|
||||
}
|
||||
if (sent < data.length)
|
||||
{
|
||||
input ~= data[sent..$];
|
||||
loop.feed(this, exception);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
input ~= data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class SelectorLoop : Loop
|
||||
{
|
||||
/// Pending connections.
|
||||
protected Array!SocketWatcher connections;
|
||||
|
||||
this() @nogc
|
||||
{
|
||||
super();
|
||||
this.connections = Array!SocketWatcher(maxEvents);
|
||||
}
|
||||
|
||||
~this() @nogc
|
||||
{
|
||||
foreach (ref connection; this.connections[])
|
||||
{
|
||||
// We want to free only the transports. ConnectionWatcher are
|
||||
// created by the user and should be freed by himself.
|
||||
if (cast(StreamTransport) connection !is null)
|
||||
{
|
||||
defaultAllocator.dispose(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
override abstract protected bool reify(SocketWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events) @nogc;
|
||||
|
||||
/**
|
||||
* Kills the watcher and closes the connection.
|
||||
*
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* exception = Occurred exception.
|
||||
*/
|
||||
protected void kill(StreamTransport transport,
|
||||
SocketException exception = null) @nogc
|
||||
in (transport !is null)
|
||||
{
|
||||
transport.socket.shutdown();
|
||||
defaultAllocator.dispose(transport.socket);
|
||||
transport.exception = exception;
|
||||
pendings.insertBack(transport);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the transport couldn't send the data, the further sending should
|
||||
* be handled by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* transport = Transport.
|
||||
* exception = Exception thrown on sending.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation could be successfully
|
||||
* completed or scheduled, $(D_KEYWORD false) otherwise (the
|
||||
* transport will be destroyed then).
|
||||
*/
|
||||
protected bool feed(StreamTransport transport,
|
||||
SocketException exception = null) @nogc
|
||||
in (transport !is null)
|
||||
{
|
||||
while (transport.input.length && transport.writeReady)
|
||||
{
|
||||
try
|
||||
{
|
||||
ptrdiff_t sent = transport.socket.send(transport.input[]);
|
||||
if (sent == 0)
|
||||
{
|
||||
transport.writeReady = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
transport.input += sent;
|
||||
}
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
exception = e;
|
||||
transport.writeReady = false;
|
||||
}
|
||||
}
|
||||
if (exception !is null)
|
||||
{
|
||||
kill(transport, exception);
|
||||
return false;
|
||||
}
|
||||
if (transport.input.length == 0 && transport.isClosing())
|
||||
{
|
||||
kill(transport);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
override void start(ConnectionWatcher watcher) @nogc
|
||||
{
|
||||
if (watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (connections.length <= watcher.socket)
|
||||
{
|
||||
connections.length = watcher.socket.handle + maxEvents / 2;
|
||||
}
|
||||
connections[watcher.socket.handle] = watcher;
|
||||
|
||||
super.start(watcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept incoming connections.
|
||||
*
|
||||
* Params:
|
||||
* connection = Connection watcher ready to accept.
|
||||
*/
|
||||
package void acceptConnections(ConnectionWatcher connection) @nogc
|
||||
in (connection !is null)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
ConnectedSocket client;
|
||||
try
|
||||
{
|
||||
client = (cast(StreamSocket) connection.socket).accept();
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
defaultAllocator.dispose(e);
|
||||
break;
|
||||
}
|
||||
if (client is null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
StreamTransport transport;
|
||||
|
||||
if (connections.length > client.handle)
|
||||
{
|
||||
transport = cast(StreamTransport) connections[client.handle];
|
||||
}
|
||||
else
|
||||
{
|
||||
connections.length = client.handle + maxEvents / 2;
|
||||
}
|
||||
if (transport is null)
|
||||
{
|
||||
transport = defaultAllocator.make!StreamTransport(this, client);
|
||||
connections[client.handle] = transport;
|
||||
}
|
||||
else
|
||||
{
|
||||
transport.socket = client;
|
||||
}
|
||||
|
||||
reify(transport,
|
||||
EventMask(Event.none),
|
||||
EventMask(Event.read | Event.write));
|
||||
connection.incoming.insertBack(transport);
|
||||
}
|
||||
|
||||
if (!connection.incoming.empty)
|
||||
{
|
||||
pendings.insertBack(connection);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* This module provides API for Windows I/O Completion Ports.
|
||||
*
|
||||
* Note: Available only on Windows.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/iocp.d,
|
||||
* tanya/async/iocp.d)
|
||||
*/
|
||||
module tanya.async.iocp;
|
||||
|
||||
version (Windows)
|
||||
{
|
||||
version = WindowsDoc;
|
||||
}
|
||||
else version (D_Ddoc)
|
||||
{
|
||||
version = WindowsDoc;
|
||||
version (Windows)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
private struct OVERLAPPED
|
||||
{
|
||||
}
|
||||
private alias HANDLE = void*;
|
||||
}
|
||||
}
|
||||
|
||||
version (WindowsDoc):
|
||||
|
||||
import tanya.sys.windows.winbase;
|
||||
|
||||
/**
|
||||
* Provides an extendable representation of a Win32 $(D_PSYMBOL OVERLAPPED)
|
||||
* structure.
|
||||
*/
|
||||
class State
|
||||
{
|
||||
/// For internal use by Windows API.
|
||||
align(1) OVERLAPPED overlapped;
|
||||
|
||||
/// File/socket handle.
|
||||
HANDLE handle;
|
||||
|
||||
/// For keeping events or event masks.
|
||||
int event;
|
||||
}
|
@ -1,344 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Interface for the event loop implementations and the default event loop
|
||||
* chooser.
|
||||
*
|
||||
* ---
|
||||
* import tanya.async;
|
||||
* import tanya.memory;
|
||||
* import tanya.network.socket;
|
||||
*
|
||||
* class EchoProtocol : TransmissionControlProtocol
|
||||
* {
|
||||
* private DuplexTransport transport;
|
||||
*
|
||||
* void received(in ubyte[] data) @nogc
|
||||
* {
|
||||
* ubyte[512] buffer;
|
||||
* buffer[0 .. data.length] = data;
|
||||
* transport.write(buffer[]);
|
||||
* }
|
||||
*
|
||||
* void connected(DuplexTransport transport) @nogc
|
||||
* {
|
||||
* this.transport = transport;
|
||||
* }
|
||||
*
|
||||
* void disconnected(SocketException e) @nogc
|
||||
* {
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* void main()
|
||||
* {
|
||||
* auto address = defaultAllocator.make!InternetAddress("127.0.0.1", cast(ushort) 8192);
|
||||
*
|
||||
* version (Windows)
|
||||
* {
|
||||
* auto sock = defaultAllocator.make!OverlappedStreamSocket(AddressFamily.inet);
|
||||
* }
|
||||
* else
|
||||
* {
|
||||
* auto sock = defaultAllocator.make!StreamSocket(AddressFamily.inet);
|
||||
* sock.blocking = false;
|
||||
* }
|
||||
*
|
||||
* sock.bind(address);
|
||||
* sock.listen(5);
|
||||
*
|
||||
* auto io = defaultAllocator.make!ConnectionWatcher(sock);
|
||||
* io.setProtocol!EchoProtocol;
|
||||
*
|
||||
* defaultLoop.start(io);
|
||||
* defaultLoop.run();
|
||||
*
|
||||
* sock.shutdown();
|
||||
* defaultAllocator.dispose(io);
|
||||
* defaultAllocator.dispose(sock);
|
||||
* defaultAllocator.dispose(address);
|
||||
* }
|
||||
* ---
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/loop.d,
|
||||
* tanya/async/loop.d)
|
||||
*/
|
||||
module tanya.async.loop;
|
||||
|
||||
import core.time;
|
||||
import tanya.async.transport;
|
||||
import tanya.async.watcher;
|
||||
import tanya.bitmanip;
|
||||
import tanya.container.buffer;
|
||||
import tanya.container.list;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.network.socket;
|
||||
|
||||
version (DisableBackends)
|
||||
{
|
||||
}
|
||||
else version (D_Ddoc)
|
||||
{
|
||||
}
|
||||
else version (linux)
|
||||
{
|
||||
import tanya.async.event.epoll;
|
||||
version = Epoll;
|
||||
}
|
||||
else version (Windows)
|
||||
{
|
||||
import tanya.async.event.iocp;
|
||||
version = IOCP;
|
||||
}
|
||||
else version (OSX)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (iOS)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (FreeBSD)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (OpenBSD)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
else version (DragonFlyBSD)
|
||||
{
|
||||
version = Kqueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Events.
|
||||
*/
|
||||
enum Event : uint
|
||||
{
|
||||
none = 0x00, /// No events.
|
||||
read = 0x01, /// Non-blocking read call.
|
||||
write = 0x02, /// Non-blocking write call.
|
||||
accept = 0x04, /// Connection made.
|
||||
error = 0x80000000, /// Sent when an error occurs.
|
||||
}
|
||||
|
||||
alias EventMask = BitFlags!Event;
|
||||
|
||||
/**
|
||||
* Event loop.
|
||||
*/
|
||||
abstract class Loop
|
||||
{
|
||||
protected bool done = true;
|
||||
|
||||
/// Pending watchers.
|
||||
protected DList!Watcher pendings;
|
||||
|
||||
/**
|
||||
* Returns: Maximal event count can be got at a time
|
||||
* (should be supported by the backend).
|
||||
*/
|
||||
protected @property uint maxEvents()
|
||||
const pure nothrow @safe @nogc
|
||||
{
|
||||
return 128U;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the loop.
|
||||
*/
|
||||
this() @nogc
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees loop internals.
|
||||
*/
|
||||
~this() @nogc
|
||||
{
|
||||
for (; !this.pendings.empty; this.pendings.removeFront())
|
||||
{
|
||||
defaultAllocator.dispose(this.pendings.front);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the loop.
|
||||
*/
|
||||
void run() @nogc
|
||||
{
|
||||
this.done = false;
|
||||
do
|
||||
{
|
||||
poll();
|
||||
|
||||
// Invoke pendings
|
||||
for (; !this.pendings.empty; this.pendings.removeFront())
|
||||
{
|
||||
this.pendings.front.invoke();
|
||||
}
|
||||
}
|
||||
while (!this.done);
|
||||
}
|
||||
|
||||
/**
|
||||
* Break out of the loop.
|
||||
*/
|
||||
void unloop() @safe pure nothrow @nogc
|
||||
{
|
||||
this.done = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
void start(ConnectionWatcher watcher) @nogc
|
||||
{
|
||||
if (watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
watcher.active = true;
|
||||
|
||||
reify(watcher, EventMask(Event.none), EventMask(Event.accept));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop watching.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
*/
|
||||
void stop(ConnectionWatcher watcher) @nogc
|
||||
{
|
||||
if (!watcher.active)
|
||||
{
|
||||
return;
|
||||
}
|
||||
watcher.active = false;
|
||||
|
||||
reify(watcher, EventMask(Event.accept), EventMask(Event.none));
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be called if the backend configuration changes.
|
||||
*
|
||||
* Params:
|
||||
* watcher = Watcher.
|
||||
* oldEvents = The events were already set.
|
||||
* events = The events should be set.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if the operation was successful.
|
||||
*/
|
||||
abstract protected bool reify(SocketWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events) @nogc;
|
||||
|
||||
/**
|
||||
* Returns: The blocking time.
|
||||
*/
|
||||
protected @property inout(Duration) blockTime()
|
||||
inout @safe pure nothrow @nogc
|
||||
{
|
||||
// Don't block if we have to do.
|
||||
return pendings.empty ? blockTime_ : Duration.zero;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the blocking time for IO watchers.
|
||||
*
|
||||
* Params:
|
||||
* blockTime = The blocking time. Cannot be larger than
|
||||
* $(D_PSYMBOL maxBlockTime).
|
||||
*/
|
||||
protected @property void blockTime(in Duration blockTime) @safe pure nothrow @nogc
|
||||
in (blockTime <= 1.dur!"hours", "Too long to wait.")
|
||||
in (!blockTime.isNegative)
|
||||
{
|
||||
blockTime_ = blockTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does the actual polling.
|
||||
*/
|
||||
abstract protected void poll() @nogc;
|
||||
|
||||
/// Maximal block time.
|
||||
protected Duration blockTime_ = 1.dur!"minutes";
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception thrown on errors in the event loop.
|
||||
*/
|
||||
class BadLoopException : Exception
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* file = The file where the exception occurred.
|
||||
* line = The line number where the exception occurred.
|
||||
* next = The previous exception in the chain of exceptions, if any.
|
||||
*/
|
||||
this(string file = __FILE__, size_t line = __LINE__, Throwable next = null)
|
||||
pure nothrow const @safe @nogc
|
||||
{
|
||||
super("Event loop cannot be initialized.", file, line, next);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the event loop used by default. If an event loop wasn't set with
|
||||
* $(D_PSYMBOL defaultLoop) before, $(D_PSYMBOL defaultLoop) will try to
|
||||
* choose an event loop supported on the system.
|
||||
*
|
||||
* Returns: The default event loop.
|
||||
*/
|
||||
@property Loop defaultLoop() @nogc
|
||||
{
|
||||
if (defaultLoop_ !is null)
|
||||
{
|
||||
return defaultLoop_;
|
||||
}
|
||||
version (Epoll)
|
||||
{
|
||||
defaultLoop_ = defaultAllocator.make!EpollLoop;
|
||||
}
|
||||
else version (IOCP)
|
||||
{
|
||||
defaultLoop_ = defaultAllocator.make!IOCPLoop;
|
||||
}
|
||||
else version (Kqueue)
|
||||
{
|
||||
import tanya.async.event.kqueue;
|
||||
defaultLoop_ = defaultAllocator.make!KqueueLoop;
|
||||
}
|
||||
return defaultLoop_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default event loop.
|
||||
*
|
||||
* This property makes it possible to implement your own backends or event
|
||||
* loops, for example, if the system is not supported or if you want to
|
||||
* extend the supported implementation. Just extend $(D_PSYMBOL Loop) and pass
|
||||
* your implementation to this property.
|
||||
*
|
||||
* Params:
|
||||
* loop = The event loop.
|
||||
*/
|
||||
@property void defaultLoop(Loop loop) @nogc
|
||||
in (loop !is null)
|
||||
{
|
||||
defaultLoop_ = loop;
|
||||
}
|
||||
|
||||
private Loop defaultLoop_;
|
@ -1,20 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* This package provides asynchronous capabilities.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/package.d,
|
||||
* tanya/async/package.d)
|
||||
*/
|
||||
module tanya.async;
|
||||
|
||||
public import tanya.async.loop;
|
||||
public import tanya.async.protocol;
|
||||
public import tanya.async.transport;
|
||||
public import tanya.async.watcher;
|
@ -1,58 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* This module contains protocol which handle data in asynchronous
|
||||
* applications.
|
||||
*
|
||||
* When an event from the network arrives, a protocol method gets
|
||||
* called and can respond to the event.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/protocol.d,
|
||||
* tanya/async/protocol.d)
|
||||
*/
|
||||
module tanya.async.protocol;
|
||||
|
||||
import tanya.async.transport;
|
||||
import tanya.network.socket;
|
||||
|
||||
/**
|
||||
* Common protocol interface.
|
||||
*/
|
||||
interface Protocol
|
||||
{
|
||||
/**
|
||||
* Params:
|
||||
* data = Read data.
|
||||
*/
|
||||
void received(in ubyte[] data) @nogc;
|
||||
|
||||
/**
|
||||
* Called when a connection is made.
|
||||
*
|
||||
* Params:
|
||||
* transport = Protocol transport.
|
||||
*/
|
||||
void connected(DuplexTransport transport) @nogc;
|
||||
|
||||
/**
|
||||
* Called when a connection is lost.
|
||||
*
|
||||
* Params:
|
||||
* exception = $(D_PSYMBOL Exception) if an error caused
|
||||
* the disconnect, $(D_KEYWORD null) otherwise.
|
||||
*/
|
||||
void disconnected(SocketException exception) @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for TCP.
|
||||
*/
|
||||
interface TransmissionControlProtocol : Protocol
|
||||
{
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* This module contains transports which are responsible for data dilvery
|
||||
* between two parties of an asynchronous communication.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/transport.d,
|
||||
* tanya/async/transport.d)
|
||||
*/
|
||||
module tanya.async.transport;
|
||||
|
||||
import tanya.async.protocol;
|
||||
import tanya.network.socket;
|
||||
|
||||
/**
|
||||
* Base transport interface.
|
||||
*/
|
||||
interface Transport
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for read-only transports.
|
||||
*/
|
||||
interface ReadTransport : Transport
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for write-only transports.
|
||||
*/
|
||||
interface WriteTransport : Transport
|
||||
{
|
||||
/**
|
||||
* Write some data to the transport.
|
||||
*
|
||||
* Params:
|
||||
* data = Data to send.
|
||||
*/
|
||||
void write(ubyte[] data) @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a bidirectional transport.
|
||||
*/
|
||||
interface DuplexTransport : ReadTransport, WriteTransport
|
||||
{
|
||||
/**
|
||||
* Returns: Application protocol.
|
||||
*
|
||||
* Postcondition: $(D_INLINECODE protocol !is null)
|
||||
*/
|
||||
@property Protocol protocol() pure nothrow @safe @nogc
|
||||
out (protocol; protocol !is null);
|
||||
|
||||
/**
|
||||
* Switches the protocol.
|
||||
*
|
||||
* The protocol is deallocated by the event loop.
|
||||
*
|
||||
* Params:
|
||||
* protocol = Application protocol.
|
||||
*
|
||||
* Precondition: $(D_INLINECODE protocol !is null)
|
||||
*/
|
||||
@property void protocol(Protocol protocol) pure nothrow @safe @nogc
|
||||
in (protocol !is null);
|
||||
|
||||
|
||||
/**
|
||||
* Returns $(D_PARAM true) if the transport is closing or closed.
|
||||
*/
|
||||
bool isClosing() const pure nothrow @safe @nogc;
|
||||
|
||||
/**
|
||||
* Close the transport.
|
||||
*
|
||||
* Buffered data will be flushed. No more data will be received.
|
||||
*/
|
||||
void close() @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a socket transport.
|
||||
*/
|
||||
interface SocketTransport : Transport
|
||||
{
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*/
|
||||
@property Socket socket() pure nothrow @safe @nogc;
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Watchers register user's interest in some event.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/async/watcher.d,
|
||||
* tanya/async/watcher.d)
|
||||
*/
|
||||
module tanya.async.watcher;
|
||||
|
||||
import tanya.async.loop;
|
||||
import tanya.async.protocol;
|
||||
import tanya.async.transport;
|
||||
import tanya.container.buffer;
|
||||
import tanya.container.list;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.network.socket;
|
||||
|
||||
/**
|
||||
* A watcher is an opaque structure that you allocate and register to record
|
||||
* your interest in some event.
|
||||
*/
|
||||
abstract class Watcher
|
||||
{
|
||||
/// Whether the watcher is active.
|
||||
bool active;
|
||||
|
||||
/**
|
||||
* Invoke some action on event.
|
||||
*/
|
||||
void invoke() @nogc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Socket watcher.
|
||||
*/
|
||||
abstract class SocketWatcher : Watcher
|
||||
{
|
||||
/// Watched socket.
|
||||
protected Socket socket_;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
*
|
||||
* Precondition: $(D_INLINECODE socket !is null)
|
||||
*/
|
||||
this(Socket socket) pure nothrow @safe @nogc
|
||||
in (socket !is null)
|
||||
{
|
||||
socket_ = socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Socket.
|
||||
*/
|
||||
@property Socket socket() pure nothrow @safe @nogc
|
||||
{
|
||||
return socket_;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connection watcher.
|
||||
*/
|
||||
class ConnectionWatcher : SocketWatcher
|
||||
{
|
||||
/// Incoming connection queue.
|
||||
DList!DuplexTransport incoming;
|
||||
|
||||
private Protocol delegate() @nogc protocolFactory;
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* socket = Socket.
|
||||
*/
|
||||
this(Socket socket) @nogc
|
||||
{
|
||||
super(socket);
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* P = Protocol should be used.
|
||||
*/
|
||||
void setProtocol(P : Protocol)() @nogc
|
||||
{
|
||||
this.protocolFactory = () @nogc => cast(Protocol) defaultAllocator.make!P;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes new connection callback.
|
||||
*/
|
||||
override void invoke() @nogc
|
||||
in (protocolFactory !is null, "Protocol isn't set.")
|
||||
{
|
||||
for (; !this.incoming.empty; this.incoming.removeFront())
|
||||
{
|
||||
this.incoming.front.protocol = protocolFactory();
|
||||
this.incoming.front.protocol.connected(this.incoming.front);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,318 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Bit manipulation.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/bitmanip.d,
|
||||
* tanya/bitmanip.d)
|
||||
*/
|
||||
module tanya.bitmanip;
|
||||
|
||||
import tanya.meta.metafunction;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
/**
|
||||
* Determines whether $(D_PARAM E) is a $(D_KEYWORD enum), whose members can be
|
||||
* used as bit flags.
|
||||
*
|
||||
* This is the case if all members of $(D_PARAM E) are integral numbers that
|
||||
* are either 0 or positive integral powers of 2.
|
||||
*
|
||||
* Params:
|
||||
* E = Some $(D_KEYWORD enum).
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM E) contains only bit flags,
|
||||
* $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
template isBitFlagEnum(E)
|
||||
{
|
||||
enum bool isValid(OriginalType!E x) = x == 0
|
||||
|| (x > 0 && ((x & (x - 1)) == 0));
|
||||
static if (isIntegral!E)
|
||||
{
|
||||
enum bool isBitFlagEnum = allSatisfy!(isValid, EnumMembers!E);
|
||||
}
|
||||
else
|
||||
{
|
||||
enum bool isBitFlagEnum = false;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
enum Valid
|
||||
{
|
||||
none = 0,
|
||||
one = 1 << 0,
|
||||
two = 1 << 1,
|
||||
}
|
||||
static assert(isBitFlagEnum!Valid);
|
||||
|
||||
enum Invalid
|
||||
{
|
||||
one,
|
||||
two,
|
||||
three,
|
||||
four,
|
||||
}
|
||||
static assert(!isBitFlagEnum!Invalid);
|
||||
|
||||
enum Negative
|
||||
{
|
||||
one = -1,
|
||||
two = -2,
|
||||
}
|
||||
static assert(!isBitFlagEnum!Negative);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that $(D_PARAM field) contains only bits from $(D_PARAM E).
|
||||
*
|
||||
* Params:
|
||||
* E = Some $(D_KEYWORD enum).
|
||||
* field = Bit field.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_PARAM field) is valid, $(D_KEYWORD false)
|
||||
* otherwise.
|
||||
*/
|
||||
bool containsBitFlags(E)(E field)
|
||||
if (isBitFlagEnum!E)
|
||||
{
|
||||
OriginalType!E fillField()
|
||||
{
|
||||
typeof(return) full;
|
||||
static foreach (member; EnumMembers!E)
|
||||
{
|
||||
full |= member;
|
||||
}
|
||||
return full;
|
||||
}
|
||||
enum OriginalType!E full = fillField();
|
||||
return (field & ~full) == OriginalType!E.init;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
enum E
|
||||
{
|
||||
one,
|
||||
two,
|
||||
three,
|
||||
}
|
||||
assert(containsBitFlags(E.one | E.two));
|
||||
assert(!containsBitFlags(cast(E) 0x8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows to use $(D_KEYWORD enum) values as a set of bit flags.
|
||||
*
|
||||
* $(D_PSYMBOL BitFlags) behaves the same as a bit field of type $(D_PARAM E),
|
||||
* but does additional cheks to ensure that the bit field contains only valid
|
||||
* values, this is only values from $(D_PARAM E).
|
||||
*
|
||||
* Params:
|
||||
* E = Some $(D_KEYWORD enum).
|
||||
*/
|
||||
struct BitFlags(E)
|
||||
if (isBitFlagEnum!E)
|
||||
{
|
||||
private OriginalType!E field;
|
||||
|
||||
/**
|
||||
* Constructs $(D_PSYMBOL BitFlags) from $(D_PARAM field).
|
||||
*
|
||||
* Params:
|
||||
* field = Bits to be set.
|
||||
*/
|
||||
this(E field)
|
||||
{
|
||||
this.field = field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts $(D_PSYMBOL BitFlags) to a boolean.
|
||||
*
|
||||
* It is $(D_KEYWORD true) if any bit is set, $(D_KEYWORD false) otherwise.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL BitFlags) contains any
|
||||
* set bits, $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool opCast(T : bool)()
|
||||
{
|
||||
return this.field != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts to the original type of $(D_PARAM E) ($(D_KEYWORD int) by
|
||||
* default).
|
||||
*
|
||||
* Returns: $(D_KEYWORD this) as $(D_INLINECODE OriginalType!T).
|
||||
*/
|
||||
OriginalType!E opCast(T : OriginalType!E)() const
|
||||
{
|
||||
return this.field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests (&), sets (|) or toggles (^) bits.
|
||||
*
|
||||
* Params:
|
||||
* op = Operation.
|
||||
* that = 0 or more bit flags.
|
||||
*
|
||||
* Returns: New $(D_PSYMBOL BitFlags) object.
|
||||
*/
|
||||
BitFlags opBinary(string op)(E that) const
|
||||
if (op == "&" || op == "|" || op == "^")
|
||||
{
|
||||
BitFlags result = this;
|
||||
mixin("return result " ~ op ~ "= that;");
|
||||
}
|
||||
|
||||
/// ditto
|
||||
BitFlags opBinary(string op)(BitFlags that) const
|
||||
if (op == "&" || op == "|" || op == "^")
|
||||
{
|
||||
BitFlags result = this;
|
||||
mixin("return result " ~ op ~ "= that;");
|
||||
}
|
||||
|
||||
/// ditto
|
||||
BitFlags opBinaryRight(string op)(E that) const
|
||||
if (op == "&" || op == "|" || op == "^")
|
||||
{
|
||||
BitFlags result = this;
|
||||
mixin("return result " ~ op ~ "= that;");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests (&), sets (|) or toggles (^) bits.
|
||||
*
|
||||
* Params:
|
||||
* op = Operation.
|
||||
* that = 0 or more bit flags.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
ref BitFlags opOpAssign(string op)(E that)
|
||||
if (op == "&" || op == "|" || op == "^")
|
||||
{
|
||||
mixin("this.field " ~ op ~ "= that;");
|
||||
return this;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref BitFlags opOpAssign(string op)(BitFlags that)
|
||||
if (op == "&" || op == "|" || op == "^")
|
||||
{
|
||||
mixin("this.field " ~ op ~ "= that.field;");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts all bit flags.
|
||||
*
|
||||
* Returns: New $(D_PSYMBOL BitFlags) object with all bits inverted.
|
||||
*/
|
||||
BitFlags opUnary(string op : "~")() const
|
||||
{
|
||||
BitFlags result;
|
||||
result.field = ~this.field;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a bit field.
|
||||
*
|
||||
* Params:
|
||||
* that = Bit field of type $(D_PARAM E).
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
ref BitFlags opAssign(E that)
|
||||
{
|
||||
this.field = that;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this $(D_PSYMBOL BitFlags) object to another bit field.
|
||||
*
|
||||
* Params:
|
||||
* that = $(D_PSYMBOL BitFlags) object or a bit field of type
|
||||
* $(D_PARAM E).
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if $(D_KEYWORD this) and $(D_PARAM that)
|
||||
* contain the same bits ,$(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool opEquals(E that) const
|
||||
{
|
||||
return this.field == that;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
bool opEquals(BitFlags that) const
|
||||
{
|
||||
return this.field == that.field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a hash value of this object.
|
||||
*
|
||||
* Returns: Hash value.
|
||||
*/
|
||||
size_t toHash() const
|
||||
{
|
||||
return cast(size_t) this.field;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a $(D_PSYMBOL BitFlags) object initialized with $(D_PARAM field).
|
||||
*
|
||||
* Params:
|
||||
* E = Some $(D_KEYWORD enum).
|
||||
* field = Bits to be set.
|
||||
*/
|
||||
BitFlags!E bitFlags(E)(E field)
|
||||
if (isBitFlagEnum!E)
|
||||
{
|
||||
return BitFlags!E(field);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
enum E
|
||||
{
|
||||
one = 1 << 0,
|
||||
two = 1 << 1,
|
||||
three = 1 << 2,
|
||||
}
|
||||
// Construct with E.one and E.two set
|
||||
auto flags = bitFlags(E.one | E.two);
|
||||
|
||||
// Test wheter E.one is set
|
||||
assert(flags & E.one);
|
||||
|
||||
// Toggle E.one
|
||||
flags ^= E.one;
|
||||
assert(!(flags & E.one));
|
||||
|
||||
// Set E.three
|
||||
flags |= E.three;
|
||||
assert(flags & E.three);
|
||||
|
||||
// Clear E.three
|
||||
flags &= ~E.three;
|
||||
assert(!(flags & E.three));
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Single-dimensioned array.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2021.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -15,8 +15,9 @@
|
||||
module tanya.container.array;
|
||||
|
||||
import core.checkedint;
|
||||
import tanya.algorithm.comparison;
|
||||
import tanya.algorithm.iteration;
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.iteration;
|
||||
import std.algorithm.mutation : bringToFront;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.memory.lifetime;
|
||||
@ -36,16 +37,23 @@ struct Range(A)
|
||||
private E* begin, end;
|
||||
private A* container;
|
||||
|
||||
invariant (this.begin <= this.end);
|
||||
invariant (this.container !is null);
|
||||
invariant (this.begin >= this.container.data);
|
||||
invariant (this.end <= this.container.data + this.container.length);
|
||||
invariant
|
||||
{
|
||||
assert(this.begin <= this.end);
|
||||
assert(this.container !is null);
|
||||
assert(this.begin >= this.container.data);
|
||||
assert(this.end <= this.container.data + this.container.length);
|
||||
}
|
||||
|
||||
private this(return ref A container, return E* begin, return E* end)
|
||||
@trusted
|
||||
in (begin <= end)
|
||||
in (begin >= container.data)
|
||||
in (end <= container.data + container.length)
|
||||
in
|
||||
{
|
||||
assert(begin <= end);
|
||||
assert(begin >= container.data);
|
||||
assert(end <= container.data + container.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.container = &container;
|
||||
this.begin = begin;
|
||||
@ -72,31 +80,51 @@ struct Range(A)
|
||||
alias opDollar = length;
|
||||
|
||||
@property ref inout(E) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.begin;
|
||||
}
|
||||
|
||||
@property ref inout(E) back() inout @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.end - 1);
|
||||
}
|
||||
|
||||
void popFront() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
++this.begin;
|
||||
}
|
||||
|
||||
void popBack() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
--this.end;
|
||||
}
|
||||
|
||||
ref inout(E) opIndex(size_t i) inout @trusted
|
||||
in (i < length)
|
||||
in
|
||||
{
|
||||
assert(i < length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.begin + i);
|
||||
}
|
||||
@ -112,15 +140,23 @@ struct Range(A)
|
||||
}
|
||||
|
||||
Range opSlice(size_t i, size_t j) @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(*this.container, this.begin + i, this.begin + j);
|
||||
}
|
||||
|
||||
A.ConstRange opSlice(size_t i, size_t j) const @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(*this.container, this.begin + i, this.begin + j);
|
||||
}
|
||||
@ -149,8 +185,11 @@ struct Array(T)
|
||||
private T* data;
|
||||
private size_t capacity_;
|
||||
|
||||
invariant (this.length_ <= this.capacity_);
|
||||
invariant (this.capacity_ == 0 || this.data !is null);
|
||||
invariant
|
||||
{
|
||||
assert(this.length_ <= this.capacity_);
|
||||
assert(this.capacity_ == 0 || this.data !is null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new $(D_PSYMBOL Array) with the elements from a static array.
|
||||
@ -276,7 +315,11 @@ struct Array(T)
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
allocator_ = allocator;
|
||||
}
|
||||
@ -532,7 +575,11 @@ struct Array(T)
|
||||
* Precondition: $(D_INLINECODE !empty).
|
||||
*/
|
||||
void removeBack()
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
length = length - 1;
|
||||
}
|
||||
@ -550,7 +597,11 @@ struct Array(T)
|
||||
* Returns: The number of elements removed
|
||||
*/
|
||||
size_t removeBack(size_t howMany)
|
||||
out (removed; removed <= howMany)
|
||||
out (removed)
|
||||
{
|
||||
assert(removed <= howMany);
|
||||
}
|
||||
do
|
||||
{
|
||||
const toRemove = min(howMany, length);
|
||||
|
||||
@ -571,7 +622,11 @@ struct Array(T)
|
||||
}
|
||||
|
||||
private inout(T)[] slice(size_t length) inout @trusted
|
||||
in (length <= capacity)
|
||||
in
|
||||
{
|
||||
assert(length <= capacity);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.data[0 .. length];
|
||||
}
|
||||
@ -593,9 +648,13 @@ struct Array(T)
|
||||
* Precondition: $(D_PARAM r) refers to a region of $(D_KEYWORD this).
|
||||
*/
|
||||
Range remove(scope Range r)
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= end)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= end);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto target = r.begin;
|
||||
auto source = r.end;
|
||||
@ -676,7 +735,7 @@ struct Array(T)
|
||||
{
|
||||
reserve(length + el.length);
|
||||
}
|
||||
return foldl!((acc, e) => acc + insertBack(e))(el, 0U);
|
||||
return fold!((acc, e) => acc + insertBack(e))(el, size_t.init);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
@ -745,23 +804,31 @@ struct Array(T)
|
||||
if (!isInfinite!R
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
const oldLength = length;
|
||||
const after = r.end - this.data;
|
||||
const inserted = insertBack(el);
|
||||
|
||||
rotate(this.data[after .. oldLength], this.data[oldLength .. length]);
|
||||
bringToFront(this.data[after .. oldLength], this.data[oldLength .. length]);
|
||||
return inserted;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
size_t insertAfter(size_t R)(Range r, T[R] el)
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return insertAfter!(T[])(r, el[]);
|
||||
}
|
||||
@ -769,9 +836,13 @@ struct Array(T)
|
||||
/// ditto
|
||||
size_t insertAfter(R)(Range r, auto ref R el)
|
||||
if (isImplicitlyConvertible!(R, T))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
const oldLen = length;
|
||||
const offset = r.end - this.data;
|
||||
@ -784,7 +855,7 @@ struct Array(T)
|
||||
{
|
||||
moveBack(el);
|
||||
}
|
||||
rotate(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
||||
bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -792,20 +863,28 @@ struct Array(T)
|
||||
/// ditto
|
||||
size_t insertBefore(R)(Range r, scope R el)
|
||||
if (!isInfinite!R
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return insertAfter(Range(this, this.data, r.begin), el);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
size_t insertBefore(size_t R)(Range r, T[R] el)
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return insertBefore!(T[])(r, el[]);
|
||||
}
|
||||
@ -813,9 +892,13 @@ struct Array(T)
|
||||
/// ditto
|
||||
size_t insertBefore(R)(Range r, auto ref R el)
|
||||
if (isImplicitlyConvertible!(R, T))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
const oldLen = length;
|
||||
const offset = r.begin - this.data;
|
||||
@ -828,7 +911,7 @@ struct Array(T)
|
||||
{
|
||||
moveBack(el);
|
||||
}
|
||||
rotate(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
||||
bringToFront(this.data[offset .. oldLen], this.data[oldLen .. length]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -983,7 +1066,11 @@ struct Array(T)
|
||||
* Precondition: $(D_INLINECODE length > pos).
|
||||
*/
|
||||
ref inout(T) opIndex(size_t pos) inout @trusted
|
||||
in (length > pos)
|
||||
in
|
||||
{
|
||||
assert(length > pos);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.data + pos);
|
||||
}
|
||||
@ -1037,7 +1124,8 @@ struct Array(T)
|
||||
}
|
||||
|
||||
/// ditto
|
||||
bool opEquals(Range that)
|
||||
bool opEquals(R)(R that)
|
||||
if (is(R == Range))
|
||||
{
|
||||
return equal(opIndex(), that);
|
||||
}
|
||||
@ -1084,7 +1172,11 @@ struct Array(T)
|
||||
* Precondition: $(D_INLINECODE !empty).
|
||||
*/
|
||||
@property ref inout(T) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.data;
|
||||
}
|
||||
@ -1107,7 +1199,11 @@ struct Array(T)
|
||||
* Precondition: $(D_INLINECODE !empty).
|
||||
*/
|
||||
@property ref inout(T) back() inout @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.data + length - 1);
|
||||
}
|
||||
@ -1135,16 +1231,24 @@ struct Array(T)
|
||||
* Precondition: $(D_INLINECODE i <= j && j <= length).
|
||||
*/
|
||||
Range opSlice(size_t i, size_t j) @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(this, this.data + i, this.data + j);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ConstRange opSlice(size_t i, size_t j) const @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(this, this.data + i, this.data + j);
|
||||
}
|
||||
@ -1201,8 +1305,12 @@ struct Array(T)
|
||||
*/
|
||||
Range opSliceAssign(size_t R)(T[R] value, size_t i, size_t j)
|
||||
@trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
copy(value[], this.data[i .. j]);
|
||||
return opSlice(i, j);
|
||||
@ -1211,8 +1319,12 @@ struct Array(T)
|
||||
/// ditto
|
||||
Range opSliceAssign(R : T)(auto ref R value, size_t i, size_t j)
|
||||
@trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
fill(this.data[i .. j], value);
|
||||
return opSlice(i, j);
|
||||
@ -1220,9 +1332,13 @@ struct Array(T)
|
||||
|
||||
/// ditto
|
||||
Range opSliceAssign()(Range value, size_t i, size_t j) @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in (j - i == value.length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
assert(j - i == value.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
copy(value, this.data[i .. j]);
|
||||
return opSlice(i, j);
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* This module contains buffers designed for C-style input/output APIs.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,6 +14,7 @@
|
||||
*/
|
||||
module tanya.container.buffer;
|
||||
|
||||
import std.traits : isScalarType;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.meta.trait;
|
||||
|
||||
@ -22,7 +23,11 @@ version (unittest)
|
||||
private int fillBuffer(ubyte[] buffer,
|
||||
int start = 0,
|
||||
int end = 10) @nogc pure nothrow
|
||||
in (start < end)
|
||||
in
|
||||
{
|
||||
assert(start < end);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto numberRead = end - start;
|
||||
for (ubyte i; i < numberRead; ++i)
|
||||
@ -67,9 +72,12 @@ if (isScalarType!T)
|
||||
/// Size by which the buffer will grow.
|
||||
private size_t blockSize = 8192;
|
||||
|
||||
invariant (this.length_ <= this.buffer_.length);
|
||||
invariant (this.blockSize > 0);
|
||||
invariant (this.minAvailable > 0);
|
||||
invariant
|
||||
{
|
||||
assert(this.length_ <= this.buffer_.length);
|
||||
assert(this.blockSize > 0);
|
||||
assert(this.minAvailable > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new read buffer.
|
||||
@ -94,7 +102,11 @@ if (isScalarType!T)
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator_ is null)
|
||||
in
|
||||
{
|
||||
assert(allocator_ is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
allocator_ = allocator;
|
||||
}
|
||||
@ -312,9 +324,13 @@ if (isScalarType!T)
|
||||
/// The position of the free area in the buffer.
|
||||
private size_t position;
|
||||
|
||||
invariant (this.blockSize > 0);
|
||||
// Position can refer to an element outside the buffer if the buffer is full.
|
||||
invariant (this.position <= this.buffer_.length);
|
||||
invariant
|
||||
{
|
||||
assert(this.blockSize > 0);
|
||||
// Position can refer to an element outside the buffer if the buffer is
|
||||
// full.
|
||||
assert(this.position <= this.buffer_.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
@ -325,8 +341,12 @@ if (isScalarType!T)
|
||||
* Precondition: $(D_INLINECODE size > 0 && allocator !is null)
|
||||
*/
|
||||
this(size_t size, shared Allocator allocator = defaultAllocator) @trusted
|
||||
in (size > 0)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(size > 0);
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.blockSize = size;
|
||||
ring = size - 1;
|
||||
@ -487,7 +507,11 @@ if (isScalarType!T)
|
||||
*/
|
||||
ref WriteBuffer opOpAssign(string op)(size_t length)
|
||||
if (op == "+")
|
||||
in (length <= this.length)
|
||||
in
|
||||
{
|
||||
assert(length <= this.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto afterRing = ring + 1;
|
||||
auto oldStart = start;
|
||||
|
@ -5,7 +5,7 @@
|
||||
/*
|
||||
* Internal package used by containers that rely on entries/nodes.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2022.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -19,7 +19,6 @@ import tanya.memory.allocator;
|
||||
import tanya.memory.lifetime;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.typecons;
|
||||
|
||||
package struct SEntry(T)
|
||||
{
|
||||
@ -54,7 +53,11 @@ package struct Bucket(K, V = void)
|
||||
}
|
||||
else
|
||||
{
|
||||
alias KV = Tuple!(K, "key", V, "value");
|
||||
package struct KV
|
||||
{
|
||||
package K key;
|
||||
package V value;
|
||||
}
|
||||
KV kv;
|
||||
}
|
||||
BucketStatus status = BucketStatus.empty;
|
||||
@ -128,14 +131,22 @@ package(tanya.container) struct HashArray(alias hasher, K, V = void)
|
||||
size_t length;
|
||||
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.array = Buckets(allocator);
|
||||
}
|
||||
|
||||
this(T)(ref T data, shared Allocator allocator)
|
||||
if (is(Unqual!T == HashArray))
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.array = Buckets(data.array, allocator);
|
||||
this.lengthIndex = data.lengthIndex;
|
||||
@ -144,7 +155,11 @@ package(tanya.container) struct HashArray(alias hasher, K, V = void)
|
||||
|
||||
// Move constructor
|
||||
void move(ref HashArray data, shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.array = Buckets(.move(data.array), allocator);
|
||||
this.lengthIndex = data.lengthIndex;
|
||||
@ -225,7 +240,11 @@ package(tanya.container) struct HashArray(alias hasher, K, V = void)
|
||||
|
||||
// Takes an index in the primes array.
|
||||
void rehashToSize(const size_t n)
|
||||
in (n < primes.length)
|
||||
in
|
||||
{
|
||||
assert(n < primes.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto storage = typeof(this.array)(primes[n], this.array.allocator);
|
||||
DataLoop: foreach (ref e1; this.array[])
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Hash table.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2021.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,7 +14,7 @@
|
||||
*/
|
||||
module tanya.container.hashtable;
|
||||
|
||||
import tanya.algorithm.iteration;
|
||||
import std.algorithm.iteration;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container.array;
|
||||
import tanya.container.entry;
|
||||
@ -71,9 +71,16 @@ struct Range(T)
|
||||
}
|
||||
|
||||
void popFront()
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -83,9 +90,16 @@ struct Range(T)
|
||||
}
|
||||
|
||||
void popBack()
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -95,15 +109,23 @@ struct Range(T)
|
||||
}
|
||||
|
||||
@property ref inout(KV) front() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.front.kv;
|
||||
}
|
||||
|
||||
@property ref inout(KV) back() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.back.kv;
|
||||
}
|
||||
@ -164,9 +186,16 @@ struct ByKey(T)
|
||||
}
|
||||
|
||||
@property void popFront()
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -176,9 +205,16 @@ struct ByKey(T)
|
||||
}
|
||||
|
||||
@property void popBack()
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -188,15 +224,23 @@ struct ByKey(T)
|
||||
}
|
||||
|
||||
@property ref inout(Key) front() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.front.key;
|
||||
}
|
||||
|
||||
@property ref inout(Key) back() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.back.key;
|
||||
}
|
||||
@ -257,9 +301,16 @@ struct ByValue(T)
|
||||
}
|
||||
|
||||
@property void popFront()
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -269,9 +320,16 @@ struct ByValue(T)
|
||||
}
|
||||
|
||||
@property void popBack()
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -281,15 +339,23 @@ struct ByValue(T)
|
||||
}
|
||||
|
||||
@property ref inout(Value) front() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.front.kv.value;
|
||||
}
|
||||
|
||||
@property ref inout(Value) back() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.back.kv.value;
|
||||
}
|
||||
@ -347,7 +413,10 @@ if (isHashFunction!(hasher, Key))
|
||||
/// ditto
|
||||
alias ConstByValue = .ByValue!(const HashArray);
|
||||
|
||||
invariant (this.data.lengthIndex < primes.length);
|
||||
invariant
|
||||
{
|
||||
assert(this.data.lengthIndex < primes.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -359,7 +428,11 @@ if (isHashFunction!(hasher, Key))
|
||||
* Precondition: $(D_INLINECODE allocator !is null).
|
||||
*/
|
||||
this(size_t n, shared Allocator allocator = defaultAllocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this(allocator);
|
||||
this.data.rehash(n);
|
||||
@ -374,7 +447,11 @@ if (isHashFunction!(hasher, Key))
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.data = HashArray(allocator);
|
||||
}
|
||||
@ -394,7 +471,11 @@ if (isHashFunction!(hasher, Key))
|
||||
*/
|
||||
this(S)(ref S init, shared Allocator allocator = defaultAllocator)
|
||||
if (is(Unqual!S == HashTable))
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.data = HashArray(init.data, allocator);
|
||||
}
|
||||
@ -402,7 +483,11 @@ if (isHashFunction!(hasher, Key))
|
||||
/// ditto
|
||||
this(S)(S init, shared Allocator allocator = defaultAllocator)
|
||||
if (is(S == HashTable))
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.data.move(init.data, allocator);
|
||||
}
|
||||
@ -419,7 +504,11 @@ if (isHashFunction!(hasher, Key))
|
||||
*/
|
||||
this(R)(scope R range, shared Allocator allocator = defaultAllocator)
|
||||
if (isForwardRange!R && is(ElementType!R == KeyValue) && !isInfinite!R)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this(allocator);
|
||||
insert(range);
|
||||
@ -449,7 +538,11 @@ if (isHashFunction!(hasher, Key))
|
||||
*/
|
||||
this(size_t n)(KeyValue[n] array,
|
||||
shared Allocator allocator = defaultAllocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this(allocator);
|
||||
insert(array[]);
|
||||
@ -498,7 +591,11 @@ if (isHashFunction!(hasher, Key))
|
||||
* Postcondition: $(D_INLINECODE allocator !is null)
|
||||
*/
|
||||
@property shared(Allocator) allocator() const
|
||||
out (allocator; allocator !is null)
|
||||
out (allocator)
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.data.array.allocator;
|
||||
}
|
||||
@ -719,7 +816,7 @@ if (isHashFunction!(hasher, Key))
|
||||
size_t insert(R)(scope R range)
|
||||
if (isForwardRange!R && is(ElementType!R == KeyValue) && !isInfinite!R)
|
||||
{
|
||||
return foldl!((acc, x) => acc + insert(x))(range, 0U);
|
||||
return fold!((acc, x) => acc + insert(x))(range, size_t.init);
|
||||
}
|
||||
|
||||
///
|
||||
|
@ -6,7 +6,7 @@
|
||||
* This module contains singly-linked ($(D_PSYMBOL SList)) and doubly-linked
|
||||
* ($(D_PSYMBOL DList)) lists.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2021.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -15,8 +15,8 @@
|
||||
*/
|
||||
module tanya.container.list;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import tanya.algorithm.iteration;
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.iteration;
|
||||
import tanya.container.entry;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.memory.lifetime;
|
||||
@ -38,7 +38,10 @@ struct SRange(L)
|
||||
|
||||
private EntryPointer* head;
|
||||
|
||||
invariant (this.head !is null);
|
||||
invariant
|
||||
{
|
||||
assert(this.head !is null);
|
||||
}
|
||||
|
||||
private this(return ref EntryPointer head) @trusted
|
||||
{
|
||||
@ -58,13 +61,21 @@ struct SRange(L)
|
||||
}
|
||||
|
||||
@property ref inout(E) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return (*this.head).content;
|
||||
}
|
||||
|
||||
void popFront() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.head = &(*this.head).next;
|
||||
}
|
||||
@ -196,7 +207,11 @@ struct SList(T)
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.allocator_ = allocator;
|
||||
}
|
||||
@ -318,7 +333,11 @@ struct SList(T)
|
||||
* Precondition: $(D_INLINECODE !empty).
|
||||
*/
|
||||
@property ref inout(T) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.head.content;
|
||||
}
|
||||
@ -459,7 +478,11 @@ struct SList(T)
|
||||
*/
|
||||
size_t insertBefore(R)(Range r, R el)
|
||||
if (isImplicitlyConvertible!(R, T))
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
return moveEntry(*r.head, el);
|
||||
}
|
||||
@ -476,9 +499,13 @@ struct SList(T)
|
||||
/// ditto
|
||||
size_t insertBefore(R)(Range r, scope R el)
|
||||
if (!isInfinite!R
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in (checkRangeBelonging(r))
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
size_t inserted;
|
||||
foreach (e; el)
|
||||
@ -503,7 +530,11 @@ struct SList(T)
|
||||
|
||||
/// ditto
|
||||
size_t insertBefore()(Range r, ref T el) @trusted
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
*r.head = allocator.make!Entry(el, *r.head);
|
||||
return 1;
|
||||
@ -599,7 +630,11 @@ struct SList(T)
|
||||
* Precondition: $(D_INLINECODE !empty)
|
||||
*/
|
||||
void removeFront()
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto n = this.head.next;
|
||||
|
||||
@ -634,7 +669,11 @@ struct SList(T)
|
||||
* Returns: The number of elements removed.
|
||||
*/
|
||||
size_t removeFront(size_t howMany)
|
||||
out (removed; removed <= howMany)
|
||||
out (removed)
|
||||
{
|
||||
assert(removed <= howMany);
|
||||
}
|
||||
do
|
||||
{
|
||||
size_t i;
|
||||
for (; i < howMany && !empty; ++i)
|
||||
@ -666,7 +705,11 @@ struct SList(T)
|
||||
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||
*/
|
||||
Range remove(scope Range r)
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
auto outOfScopeList = typeof(this)(allocator);
|
||||
outOfScopeList.head = *r.head;
|
||||
@ -700,8 +743,12 @@ struct SList(T)
|
||||
* $(D_PARAM range) is extracted from this list.
|
||||
*/
|
||||
Range popFirstOf(Range range)
|
||||
in (!range.empty)
|
||||
in (checkRangeBelonging(range))
|
||||
in
|
||||
{
|
||||
assert(!range.empty);
|
||||
assert(checkRangeBelonging(range));
|
||||
}
|
||||
do
|
||||
{
|
||||
auto next = (*range.head).next;
|
||||
|
||||
@ -891,8 +938,11 @@ struct DRange(L)
|
||||
private EntryPointer* head;
|
||||
private EntryPointer* tail;
|
||||
|
||||
invariant (this.head !is null);
|
||||
invariant (this.tail !is null);
|
||||
invariant
|
||||
{
|
||||
assert(this.head !is null);
|
||||
assert(this.tail !is null);
|
||||
}
|
||||
|
||||
private this(return ref EntryPointer head, return ref EntryPointer tail)
|
||||
@trusted
|
||||
@ -914,25 +964,41 @@ struct DRange(L)
|
||||
}
|
||||
|
||||
@property ref inout(E) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return (*this.head).content;
|
||||
}
|
||||
|
||||
@property ref inout(E) back() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return (*this.tail).content;
|
||||
}
|
||||
|
||||
void popFront() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.head = &(*this.head).next;
|
||||
}
|
||||
|
||||
void popBack() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.tail = &(*this.tail).prev;
|
||||
}
|
||||
@ -980,9 +1046,12 @@ struct DList(T)
|
||||
|
||||
static if (__VERSION__ < 2086) // Bug #20171.
|
||||
{
|
||||
invariant ((this.tail is null) == (this.head is null));
|
||||
invariant (this.tail is null || this.tail.next is null);
|
||||
invariant (this.head is null || this.head.prev is null);
|
||||
invariant
|
||||
{
|
||||
assert((this.tail is null) == (this.head is null));
|
||||
assert(this.tail is null || this.tail.next is null);
|
||||
assert(this.head is null || this.head.prev is null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1087,7 +1156,11 @@ struct DList(T)
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.allocator_ = allocator;
|
||||
}
|
||||
@ -1213,7 +1286,11 @@ struct DList(T)
|
||||
* Precondition: $(D_INLINECODE !empty).
|
||||
*/
|
||||
@property ref inout(T) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.head.content;
|
||||
}
|
||||
@ -1224,7 +1301,11 @@ struct DList(T)
|
||||
* Precondition: $(D_INLINECODE !empty).
|
||||
*/
|
||||
@property ref inout(T) back() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.tail.content;
|
||||
}
|
||||
@ -1267,8 +1348,12 @@ struct DList(T)
|
||||
// Returns count of the elements in the list.
|
||||
private size_t makeList(R)(scope ref R el, out Entry* head, out Entry* tail)
|
||||
@trusted
|
||||
out (retLength; (retLength == 0 && head is null && tail is null)
|
||||
|| (retLength > 0 && head !is null && tail !is null))
|
||||
out (retLength)
|
||||
{
|
||||
assert((retLength == 0 && head is null && tail is null)
|
||||
|| (retLength > 0 && head !is null && tail !is null));
|
||||
}
|
||||
do
|
||||
{
|
||||
size_t retLength;
|
||||
|
||||
@ -1536,7 +1621,11 @@ struct DList(T)
|
||||
*/
|
||||
size_t insertBefore(R)(Range r, R el)
|
||||
if (isImplicitlyConvertible!(R, T))
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
return moveFront(*r.head, el);
|
||||
}
|
||||
@ -1552,7 +1641,11 @@ struct DList(T)
|
||||
|
||||
/// ditto
|
||||
size_t insertBefore()(Range r, ref T el) @trusted
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
auto temp = allocator.make!Entry(el, *r.head);
|
||||
|
||||
@ -1584,9 +1677,13 @@ struct DList(T)
|
||||
/// ditto
|
||||
size_t insertBefore(R)(Range r, scope R el)
|
||||
if (!isInfinite!R
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in (checkRangeBelonging(r))
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
size_t inserted;
|
||||
foreach (e; el)
|
||||
@ -1639,7 +1736,11 @@ struct DList(T)
|
||||
*/
|
||||
size_t insertAfter(R)(Range r, R el) @trusted
|
||||
if (isImplicitlyConvertible!(R, T))
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
return moveBack(*r.tail, el);
|
||||
}
|
||||
@ -1657,7 +1758,11 @@ struct DList(T)
|
||||
|
||||
/// ditto
|
||||
size_t insertAfter()(Range r, ref T el) @trusted
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
auto temp = allocator.make!Entry(el, null, *r.tail);
|
||||
|
||||
@ -1689,11 +1794,15 @@ struct DList(T)
|
||||
/// ditto
|
||||
size_t insertAfter(R)(Range r, scope R el)
|
||||
if (!isInfinite!R
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in (checkRangeBelonging(r))
|
||||
&& isInputRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T))
|
||||
in
|
||||
{
|
||||
return foldl!((acc, x) => acc + insertAfter(r, x))(el, 0U);
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
return fold!((acc, x) => acc + insertAfter(r, x))(el, size_t.init);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1777,7 +1886,11 @@ struct DList(T)
|
||||
* Precondition: $(D_INLINECODE !empty)
|
||||
*/
|
||||
void removeFront()
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto n = this.head.next;
|
||||
|
||||
@ -1809,7 +1922,11 @@ struct DList(T)
|
||||
|
||||
/// ditto
|
||||
void removeBack()
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto n = this.tail.prev;
|
||||
|
||||
@ -1851,7 +1968,11 @@ struct DList(T)
|
||||
* Returns: The number of elements removed.
|
||||
*/
|
||||
size_t removeFront(size_t howMany)
|
||||
out (removed; removed <= howMany)
|
||||
out (removed)
|
||||
{
|
||||
assert(removed <= howMany);
|
||||
}
|
||||
do
|
||||
{
|
||||
size_t i;
|
||||
for (; i < howMany && !empty; ++i)
|
||||
@ -1874,7 +1995,11 @@ struct DList(T)
|
||||
|
||||
/// ditto
|
||||
size_t removeBack(size_t howMany)
|
||||
out (removed; removed <= howMany)
|
||||
out (removed)
|
||||
{
|
||||
assert(removed <= howMany);
|
||||
}
|
||||
do
|
||||
{
|
||||
size_t i;
|
||||
for (; i < howMany && !empty; ++i)
|
||||
@ -1906,7 +2031,11 @@ struct DList(T)
|
||||
* Precondition: $(D_PARAM r) is extracted from this list.
|
||||
*/
|
||||
Range remove(scope Range r)
|
||||
in (checkRangeBelonging(r))
|
||||
in
|
||||
{
|
||||
assert(checkRangeBelonging(r));
|
||||
}
|
||||
do
|
||||
{
|
||||
// Save references to the elements before and after the range.
|
||||
Entry* headPrev;
|
||||
@ -1978,7 +2107,11 @@ struct DList(T)
|
||||
* $(D_PARAM range) is extracted from this list.
|
||||
*/
|
||||
Range popFirstOf(Range range)
|
||||
in (!range.empty)
|
||||
in
|
||||
{
|
||||
assert(!range.empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
remove(Range(*range.head, *range.head));
|
||||
return range;
|
||||
@ -1986,7 +2119,11 @@ struct DList(T)
|
||||
|
||||
/// ditto
|
||||
Range popLastOf(Range range)
|
||||
in (!range.empty)
|
||||
in
|
||||
{
|
||||
assert(!range.empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
remove(Range(*range.tail, *range.tail));
|
||||
return range;
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Abstract data types whose instances are collections of other objects.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -6,7 +6,7 @@
|
||||
* This module implements a $(D_PSYMBOL Set) container that stores unique
|
||||
* values without any particular order.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -69,9 +69,16 @@ struct Range(T)
|
||||
}
|
||||
|
||||
void popFront()
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -81,9 +88,16 @@ struct Range(T)
|
||||
}
|
||||
|
||||
void popBack()
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
out (; empty || this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
out
|
||||
{
|
||||
assert(empty || this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
@ -93,15 +107,23 @@ struct Range(T)
|
||||
}
|
||||
|
||||
@property ref inout(E) front() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.front.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.front.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.front.key;
|
||||
}
|
||||
|
||||
@property ref inout(E) back() inout
|
||||
in (!empty)
|
||||
in (this.dataRange.back.status == BucketStatus.used)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
assert(this.dataRange.back.status == BucketStatus.used);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.dataRange.back.key;
|
||||
}
|
||||
@ -146,9 +168,12 @@ if (isHashFunction!(hasher, T))
|
||||
/// ditto
|
||||
alias ConstRange = .Range!(const HashArray);
|
||||
|
||||
invariant (this.data.lengthIndex < primes.length);
|
||||
invariant (this.data.array.length == 0
|
||||
|| this.data.array.length == primes[this.data.lengthIndex]);
|
||||
invariant
|
||||
{
|
||||
assert(this.data.lengthIndex < primes.length);
|
||||
assert(this.data.array.length == 0
|
||||
|| this.data.array.length == primes[this.data.lengthIndex]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -160,7 +185,11 @@ if (isHashFunction!(hasher, T))
|
||||
* Precondition: $(D_INLINECODE allocator !is null).
|
||||
*/
|
||||
this(size_t n, shared Allocator allocator = defaultAllocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this(allocator);
|
||||
this.data.rehash(n);
|
||||
@ -175,7 +204,11 @@ if (isHashFunction!(hasher, T))
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.data = HashArray(allocator);
|
||||
}
|
||||
@ -195,7 +228,11 @@ if (isHashFunction!(hasher, T))
|
||||
*/
|
||||
this(S)(ref S init, shared Allocator allocator = defaultAllocator)
|
||||
if (is(Unqual!S == Set))
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.data = HashArray(init.data, allocator);
|
||||
}
|
||||
@ -203,7 +240,11 @@ if (isHashFunction!(hasher, T))
|
||||
/// ditto
|
||||
this(S)(S init, shared Allocator allocator = defaultAllocator)
|
||||
if (is(S == Set))
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.data.move(init.data, allocator);
|
||||
}
|
||||
@ -222,7 +263,11 @@ if (isHashFunction!(hasher, T))
|
||||
if (isForwardRange!R
|
||||
&& isImplicitlyConvertible!(ElementType!R, T)
|
||||
&& !isInfinite!R)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this(allocator);
|
||||
insert(range);
|
||||
@ -249,7 +294,11 @@ if (isHashFunction!(hasher, T))
|
||||
* Precondition: $(D_INLINECODE allocator !is null).
|
||||
*/
|
||||
this(size_t n)(T[n] array, shared Allocator allocator = defaultAllocator)
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this(allocator);
|
||||
insert(array[]);
|
||||
@ -297,7 +346,11 @@ if (isHashFunction!(hasher, T))
|
||||
* Postcondition: $(D_INLINECODE allocator !is null)
|
||||
*/
|
||||
@property shared(Allocator) allocator() const
|
||||
out (allocator; allocator !is null)
|
||||
out (allocator)
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.data.array.allocator;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@
|
||||
* Internally $(D_PSYMBOL String) is represented by a sequence of
|
||||
* $(D_KEYWORD char)s.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -26,7 +26,8 @@
|
||||
*/
|
||||
module tanya.container.string;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.algorithm.comparison;
|
||||
import std.algorithm.mutation : bringToFront;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.hash.lookup;
|
||||
import tanya.memory.allocator;
|
||||
@ -70,15 +71,22 @@ if (is(Unqual!E == char))
|
||||
private alias ContainerType = CopyConstness!(E, String);
|
||||
private ContainerType* container;
|
||||
|
||||
invariant (this.begin <= this.end);
|
||||
invariant (this.container !is null);
|
||||
invariant (this.begin >= this.container.data);
|
||||
invariant (this.end <= this.container.data + this.container.length);
|
||||
invariant
|
||||
{
|
||||
assert(this.begin <= this.end);
|
||||
assert(this.container !is null);
|
||||
assert(this.begin >= this.container.data);
|
||||
assert(this.end <= this.container.data + this.container.length);
|
||||
}
|
||||
|
||||
private this(ref ContainerType container, E* begin, E* end) @trusted
|
||||
in (begin <= end)
|
||||
in (begin >= container.data)
|
||||
in (end <= container.data + container.length)
|
||||
in
|
||||
{
|
||||
assert(begin <= end);
|
||||
assert(begin >= container.data);
|
||||
assert(end <= container.data + container.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.container = &container;
|
||||
this.begin = begin;
|
||||
@ -105,31 +113,51 @@ if (is(Unqual!E == char))
|
||||
alias opDollar = length;
|
||||
|
||||
@property ref inout(E) front() inout
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.begin;
|
||||
}
|
||||
|
||||
@property ref inout(E) back() inout @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.end - 1);
|
||||
}
|
||||
|
||||
void popFront() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
++this.begin;
|
||||
}
|
||||
|
||||
void popBack() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
--this.end;
|
||||
}
|
||||
|
||||
ref inout(E) opIndex(const size_t i) inout @trusted
|
||||
in (i < length)
|
||||
in
|
||||
{
|
||||
assert(i < length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.begin + i);
|
||||
}
|
||||
@ -145,15 +173,23 @@ if (is(Unqual!E == char))
|
||||
}
|
||||
|
||||
ByCodeUnit opSlice(const size_t i, const size_t j) @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(*this.container, this.begin + i, this.begin + j);
|
||||
}
|
||||
|
||||
ByCodeUnit!(const E) opSlice(const size_t i, const size_t j) const @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(*this.container, this.begin + i, this.begin + j);
|
||||
}
|
||||
@ -177,15 +213,22 @@ if (is(Unqual!E == char))
|
||||
private alias ContainerType = CopyConstness!(E, String);
|
||||
private ContainerType* container;
|
||||
|
||||
invariant (this.begin <= this.end);
|
||||
invariant (this.container !is null);
|
||||
invariant (this.begin >= this.container.data);
|
||||
invariant (this.end <= this.container.data + this.container.length);
|
||||
invariant
|
||||
{
|
||||
assert(this.begin <= this.end);
|
||||
assert(this.container !is null);
|
||||
assert(this.begin >= this.container.data);
|
||||
assert(this.end <= this.container.data + this.container.length);
|
||||
}
|
||||
|
||||
private this(ref ContainerType container, E* begin, E* end) @trusted
|
||||
in (begin <= end)
|
||||
in (begin >= container.data)
|
||||
in (end <= container.data + container.length)
|
||||
in
|
||||
{
|
||||
assert(begin <= end);
|
||||
assert(begin >= container.data);
|
||||
assert(end <= container.data + container.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.container = &container;
|
||||
this.begin = begin;
|
||||
@ -205,8 +248,15 @@ if (is(Unqual!E == char))
|
||||
}
|
||||
|
||||
@property dchar front() const @trusted
|
||||
in (!empty)
|
||||
out (chr; chr < 0xd800 || chr > 0xdfff)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
out (chr)
|
||||
{
|
||||
assert(chr < 0xd800 || chr > 0xdfff);
|
||||
}
|
||||
do
|
||||
{
|
||||
dchar chr;
|
||||
ubyte units;
|
||||
@ -236,7 +286,11 @@ if (is(Unqual!E == char))
|
||||
}
|
||||
|
||||
void popFront() @trusted
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
ubyte units;
|
||||
if ((*begin & 0xf0) == 0xf0)
|
||||
@ -282,7 +336,10 @@ struct String
|
||||
private char* data;
|
||||
private size_t capacity_;
|
||||
|
||||
@nogc nothrow pure @safe invariant (this.length_ <= this.capacity_);
|
||||
@nogc nothrow pure @safe invariant
|
||||
{
|
||||
assert(this.length_ <= this.capacity_);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the string from a stringish range.
|
||||
@ -372,7 +429,11 @@ struct String
|
||||
|
||||
/// ditto
|
||||
this(shared Allocator allocator) @nogc nothrow pure @safe
|
||||
in (allocator !is null)
|
||||
in
|
||||
{
|
||||
assert(allocator !is null);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.allocator_ = allocator;
|
||||
}
|
||||
@ -451,8 +512,12 @@ struct String
|
||||
|
||||
private void write4Bytes(ref const dchar src)
|
||||
@nogc nothrow pure @trusted
|
||||
in (capacity - length >= 4)
|
||||
in (src - 0x10000 < 0x100000)
|
||||
in
|
||||
{
|
||||
assert(capacity - length >= 4);
|
||||
assert(src - 0x10000 < 0x100000);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto dst = this.data + length;
|
||||
|
||||
@ -466,7 +531,11 @@ struct String
|
||||
|
||||
private size_t insertWideChar(C)(auto ref const C chr) @trusted
|
||||
if (is(C == wchar) || is(C == dchar))
|
||||
in (capacity - length >= 3)
|
||||
in
|
||||
{
|
||||
assert(capacity - length >= 3);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto dst = this.data + length;
|
||||
if (chr < 0x80)
|
||||
@ -789,9 +858,13 @@ struct String
|
||||
const size_t i,
|
||||
const size_t j)
|
||||
if (is(Unqual!R == char))
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in (j - i == value.length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
assert(j - i == value.length);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto target = opSlice(i, j);
|
||||
copy(value, target);
|
||||
@ -803,8 +876,12 @@ struct String
|
||||
const size_t i,
|
||||
const size_t j)
|
||||
@nogc nothrow pure @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
copy(value[], this.data[i .. j]);
|
||||
return opSlice(i, j);
|
||||
@ -815,8 +892,12 @@ struct String
|
||||
const size_t i,
|
||||
const size_t j)
|
||||
@nogc nothrow pure @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
for (auto p = this.data + i; p < this.data + j; ++p)
|
||||
{
|
||||
@ -893,7 +974,11 @@ struct String
|
||||
* Precondition: $(D_INLINECODE length > pos).
|
||||
*/
|
||||
ref inout(char) opIndex(const size_t pos) inout @nogc nothrow pure @trusted
|
||||
in (length > pos)
|
||||
in
|
||||
{
|
||||
assert(length > pos);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *(this.data + pos);
|
||||
}
|
||||
@ -1038,8 +1123,12 @@ struct String
|
||||
*/
|
||||
ByCodeUnit!char opSlice(const size_t i, const size_t j)
|
||||
@nogc nothrow pure @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(this, this.data + i, this.data + j);
|
||||
}
|
||||
@ -1047,8 +1136,12 @@ struct String
|
||||
/// ditto
|
||||
ByCodeUnit!(const char) opSlice(const size_t i, const size_t j)
|
||||
const @nogc nothrow pure @trusted
|
||||
in (i <= j)
|
||||
in (j <= length)
|
||||
in
|
||||
{
|
||||
assert(i <= j);
|
||||
assert(j <= length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return typeof(return)(this, this.data + i, this.data + j);
|
||||
}
|
||||
@ -1166,29 +1259,29 @@ struct String
|
||||
int opCmp(S)(auto ref S that) const @trusted
|
||||
if (is(Unqual!S == String))
|
||||
{
|
||||
return compare(this.data[0 .. length], that.data[0 .. that.length]);
|
||||
return cmp(this.data[0 .. length], that.data[0 .. that.length]);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
int opCmp(S)(ByCodeUnit!S that) const @trusted
|
||||
if (is(Unqual!S == char))
|
||||
{
|
||||
return compare(this.data[0 .. length],
|
||||
that.begin[0 .. that.end - that.begin]);
|
||||
return cmp(this.data[0 .. length],
|
||||
that.begin[0 .. that.end - that.begin]);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
int opCmp(S)(ByCodePoint!S that) const @trusted
|
||||
if (is(Unqual!S == char))
|
||||
{
|
||||
return compare(this.data[0 .. length],
|
||||
that.begin[0 .. that.end - that.begin]);
|
||||
return cmp(this.data[0 .. length],
|
||||
that.begin[0 .. that.end - that.begin]);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
int opCmp()(const char[] that) const @trusted
|
||||
{
|
||||
return compare(this.data[0 .. length], that);
|
||||
return cmp(this.data[0 .. length], that);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1329,9 +1422,13 @@ struct String
|
||||
*/
|
||||
R remove(R)(R r) @trusted
|
||||
if (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto end = this.data + this.length;
|
||||
copy(ByCodeUnit!char(this, r.end, end), ByCodeUnit!char(this, r.begin, end));
|
||||
@ -1342,7 +1439,7 @@ struct String
|
||||
///
|
||||
@nogc pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.searching : count;
|
||||
import std.algorithm.searching : count;
|
||||
|
||||
auto s = String("Из пословицы слова не выкинешь.");
|
||||
|
||||
@ -1377,18 +1474,22 @@ struct String
|
||||
*/
|
||||
size_t insertAfter(T, R)(R r, T el) @trusted
|
||||
if ((isSomeChar!T || (!isInfinite!T
|
||||
&& isInputRange!T
|
||||
&& isSomeChar!(ElementType!T)))
|
||||
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
&& isInputRange!T
|
||||
&& isSomeChar!(ElementType!T)))
|
||||
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
const oldLength = length;
|
||||
const after = r.end - this.data;
|
||||
const inserted = insertBack(el);
|
||||
|
||||
rotate(this.data[after .. oldLength], this.data[oldLength .. length]);
|
||||
bringToFront(this.data[after .. oldLength], this.data[oldLength .. length]);
|
||||
return inserted;
|
||||
}
|
||||
|
||||
@ -1407,12 +1508,16 @@ struct String
|
||||
///
|
||||
size_t insertBefore(T, R)(R r, T el) @trusted
|
||||
if ((isSomeChar!T || (!isInfinite!T
|
||||
&& isInputRange!T
|
||||
&& isSomeChar!(ElementType!T)))
|
||||
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
||||
in (r.container is &this)
|
||||
in (r.begin >= this.data)
|
||||
in (r.end <= this.data + length)
|
||||
&& isInputRange!T
|
||||
&& isSomeChar!(ElementType!T)))
|
||||
&& (is(R == ByCodeUnit!char) || is(R == ByCodePoint!char)))
|
||||
in
|
||||
{
|
||||
assert(r.container is &this);
|
||||
assert(r.begin >= this.data);
|
||||
assert(r.end <= this.data + length);
|
||||
}
|
||||
do
|
||||
{
|
||||
return insertAfter(R(this, this.data, r.begin), el);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* This module provides functions for converting between different types.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,11 +14,9 @@
|
||||
*/
|
||||
module tanya.conv;
|
||||
|
||||
import std.traits : Unsigned;
|
||||
import std.traits : Unsigned, isNumeric;
|
||||
import tanya.container.string;
|
||||
import tanya.memory.allocator;
|
||||
deprecated("Use tanya.memory.lifetime.emplace instead")
|
||||
public import tanya.memory.lifetime : emplace;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range;
|
||||
@ -62,11 +60,15 @@ final class ConvException : Exception
|
||||
*/
|
||||
package T readIntegral(T, R)(ref R range, const ubyte base = 10)
|
||||
if (isInputRange!R
|
||||
&& isSomeChar!(ElementType!R)
|
||||
&& isIntegral!T
|
||||
&& isUnsigned!T)
|
||||
in (base >= 2)
|
||||
in (base <= 36)
|
||||
&& isSomeChar!(ElementType!R)
|
||||
&& isIntegral!T
|
||||
&& isUnsigned!T)
|
||||
in
|
||||
{
|
||||
assert(base >= 2);
|
||||
assert(base <= 36);
|
||||
}
|
||||
do
|
||||
{
|
||||
T boundary = cast(T) (T.max / base);
|
||||
if (range.empty)
|
||||
|
@ -38,7 +38,7 @@
|
||||
*
|
||||
* More advanced formatting is currently not implemented.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2022.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -47,16 +47,15 @@
|
||||
*/
|
||||
module tanya.format;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.algorithm.comparison;
|
||||
import std.ascii;
|
||||
import tanya.container.string;
|
||||
import tanya.encoding.ascii;
|
||||
import tanya.math;
|
||||
static import tanya.memory.op;
|
||||
import tanya.meta.metafunction;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range;
|
||||
import tanya.typecons : Tuple;
|
||||
|
||||
// Returns the last part of buffer with converted number.
|
||||
package(tanya) char[] integral2String(T)(T number, return ref char[21] buffer)
|
||||
@ -940,6 +939,12 @@ private struct uint128
|
||||
{
|
||||
ulong[2] data;
|
||||
|
||||
private struct DivMod
|
||||
{
|
||||
uint128 quotient;
|
||||
uint128 remainder;
|
||||
}
|
||||
|
||||
this(ulong upper, ulong lower) @nogc nothrow pure @safe
|
||||
{
|
||||
this.data[0] = upper;
|
||||
@ -1174,8 +1179,12 @@ private struct uint128
|
||||
return this.data[1];
|
||||
}
|
||||
|
||||
Tuple!(uint128, uint128) divMod(ulong rhs) const @nogc nothrow pure @safe
|
||||
in (rhs != uint128(), "Division by 0")
|
||||
DivMod divMod(ulong rhs) const @nogc nothrow pure @safe
|
||||
in
|
||||
{
|
||||
assert(rhs != uint128(), "Division by 0");
|
||||
}
|
||||
do
|
||||
{
|
||||
if (rhs == 1)
|
||||
{
|
||||
@ -1193,22 +1202,22 @@ private struct uint128
|
||||
typeof(return) result;
|
||||
for (ubyte x = this.bits; x > 0; --x)
|
||||
{
|
||||
result[0] = result[0] << 1;
|
||||
result[1] = result[1] << 1;
|
||||
result.quotient = result.quotient << 1;
|
||||
result.remainder = result.remainder << 1;
|
||||
|
||||
if ((this >> (x - 1U)) & 1)
|
||||
{
|
||||
++result[1];
|
||||
++result.remainder;
|
||||
}
|
||||
|
||||
if (result[1] >= rhs)
|
||||
if (result.remainder >= rhs)
|
||||
{
|
||||
if (result[1].data[1] < rhs)
|
||||
if (result.remainder.data[1] < rhs)
|
||||
{
|
||||
--result[1].data[0];
|
||||
--result.remainder.data[0];
|
||||
}
|
||||
result[1].data[1] -= rhs;
|
||||
++result[0];
|
||||
result.remainder.data[1] -= rhs;
|
||||
++result.quotient;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@ -1216,12 +1225,12 @@ private struct uint128
|
||||
|
||||
uint128 opBinary(string op : "/")(ulong rhs)
|
||||
{
|
||||
return divMod(rhs)[0];
|
||||
return divMod(rhs).quotient;
|
||||
}
|
||||
|
||||
uint128 opBinary(string op : "%")(ulong rhs) const
|
||||
{
|
||||
return divMod(rhs)[1];
|
||||
return divMod(rhs).remainder;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1273,9 +1282,13 @@ private int indexMismatch(ulong low, ulong high) @nogc nothrow pure @safe
|
||||
}
|
||||
|
||||
private char[] errol2(double value,
|
||||
return ref char[512] buffer,
|
||||
out int exponent) @nogc nothrow pure @safe
|
||||
in (value > 9.007199254740992e15 && value < 3.40282366920938e38)
|
||||
return ref char[512] buffer,
|
||||
out int exponent) @nogc nothrow pure @safe
|
||||
in
|
||||
{
|
||||
assert(value > 9.007199254740992e15 && value < 3.40282366920938e38);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto v = uint128(value);
|
||||
auto leftBoundary = v + raise2ToExp((value - previous(value)) / 2.0);
|
||||
@ -1294,12 +1307,12 @@ in (value > 9.007199254740992e15 && value < 3.40282366920938e38)
|
||||
enum ulong power19 = cast(ulong) 1e19;
|
||||
|
||||
auto qr = leftBoundary.divMod(power19);
|
||||
auto low = cast(ulong) qr[1];
|
||||
const lowFactor = cast(ulong) (qr[0] % power19);
|
||||
auto low = cast(ulong) qr.remainder;
|
||||
const lowFactor = cast(ulong) (qr.quotient % power19);
|
||||
|
||||
qr = rightBoundary.divMod(power19);
|
||||
auto high = cast(ulong) qr[1];
|
||||
const highFactor = cast(ulong) (qr[0] % power19);
|
||||
auto high = cast(ulong) qr.remainder;
|
||||
const highFactor = cast(ulong) (qr.quotient % power19);
|
||||
size_t digitIndex;
|
||||
|
||||
if (lowFactor != highFactor)
|
||||
@ -1358,9 +1371,13 @@ in (value > 9.007199254740992e15 && value < 3.40282366920938e38)
|
||||
}
|
||||
|
||||
private char[] errolFixed(double value,
|
||||
return ref char[512] buffer,
|
||||
out int exponent) @nogc nothrow pure @safe
|
||||
in (value >= 16.0 && value <= 9.007199254740992e15)
|
||||
return ref char[512] buffer,
|
||||
out int exponent) @nogc nothrow pure @safe
|
||||
in
|
||||
{
|
||||
assert(value >= 16.0 && value <= 9.007199254740992e15);
|
||||
}
|
||||
do
|
||||
{
|
||||
auto decimal = cast(ulong) value;
|
||||
auto n = cast(double) decimal;
|
||||
@ -2253,26 +2270,6 @@ private void printToString(string fmt, OR, Args...)(ref OR result,
|
||||
{
|
||||
formatRange(args[0], result);
|
||||
}
|
||||
else static if (is(Unqual!(typeof(args[0].stringify())) == String))
|
||||
{
|
||||
pragma(msg, ".stringify() is deprecated. Use toString() with an output"
|
||||
~ " range instead");
|
||||
static if (is(Arg == class) || is(Arg == interface))
|
||||
{
|
||||
if (args[0] is null)
|
||||
{
|
||||
put(result, "null");
|
||||
}
|
||||
else
|
||||
{
|
||||
put(result, args[0].stringify()[]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
put(result, args[0].stringify()[]);
|
||||
}
|
||||
}
|
||||
else static if (is(typeof(args[0].toString(result)) == OR))
|
||||
{
|
||||
static if (is(Arg == class) || is(Arg == interface))
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Non-cryptographic, lookup hash functions.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,6 +14,7 @@
|
||||
*/
|
||||
module tanya.hash.lookup;
|
||||
|
||||
import std.traits : isScalarType;
|
||||
import tanya.meta.trait;
|
||||
import tanya.range.primitive;
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -1,151 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Number theory.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/math/nbtheory.d,
|
||||
* tanya/math/nbtheory.d)
|
||||
*/
|
||||
module tanya.math.nbtheory;
|
||||
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
version (TanyaNative)
|
||||
{
|
||||
private extern float fabs(float) @nogc nothrow pure @safe;
|
||||
private extern double fabs(double) @nogc nothrow pure @safe;
|
||||
private extern real fabs(real) @nogc nothrow pure @safe;
|
||||
|
||||
private extern double log(double) @nogc nothrow pure @safe;
|
||||
private extern float logf(float) @nogc nothrow pure @safe;
|
||||
private extern real logl(real) @nogc nothrow pure @safe;
|
||||
}
|
||||
else
|
||||
{
|
||||
import core.math : fabs;
|
||||
import std.math : log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the absolute value of a number.
|
||||
*
|
||||
* Params:
|
||||
* T = Argument type.
|
||||
* x = Argument.
|
||||
*
|
||||
* Returns: Absolute value of $(D_PARAM x).
|
||||
*/
|
||||
Unqual!T abs(T)(T x)
|
||||
if (isIntegral!T)
|
||||
{
|
||||
static if (isSigned!T)
|
||||
{
|
||||
return x >= 0 ? x : -x;
|
||||
}
|
||||
else
|
||||
{
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int i = -1;
|
||||
assert(i.abs == 1);
|
||||
static assert(is(typeof(i.abs) == int));
|
||||
|
||||
uint u = 1;
|
||||
assert(u.abs == 1);
|
||||
static assert(is(typeof(u.abs) == uint));
|
||||
}
|
||||
|
||||
/// ditto
|
||||
Unqual!T abs(T)(T x)
|
||||
if (isFloatingPoint!T)
|
||||
{
|
||||
return fabs(x);
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
float f = -1.64;
|
||||
assert(f.abs == 1.64F);
|
||||
static assert(is(typeof(f.abs) == float));
|
||||
|
||||
double d = -1.64;
|
||||
assert(d.abs == 1.64);
|
||||
static assert(is(typeof(d.abs) == double));
|
||||
|
||||
real r = -1.64;
|
||||
assert(r.abs == 1.64L);
|
||||
static assert(is(typeof(r.abs) == real));
|
||||
}
|
||||
|
||||
version (D_Ddoc)
|
||||
{
|
||||
/**
|
||||
* Calculates natural logarithm of $(D_PARAM x).
|
||||
*
|
||||
* Params:
|
||||
* T = Argument type.
|
||||
* x = Argument.
|
||||
*
|
||||
* Returns: Natural logarithm of $(D_PARAM x).
|
||||
*/
|
||||
Unqual!T ln(T)(T x)
|
||||
if (isFloatingPoint!T);
|
||||
}
|
||||
else version (TanyaNative)
|
||||
{
|
||||
Unqual!T ln(T)(T x) @nogc nothrow pure @safe
|
||||
if (isFloatingPoint!T)
|
||||
{
|
||||
static if (is(Unqual!T == float))
|
||||
{
|
||||
return logf(x);
|
||||
}
|
||||
else static if (is(Unqual!T == double))
|
||||
{
|
||||
return log(x);
|
||||
}
|
||||
else
|
||||
{
|
||||
return logl(x);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Unqual!T ln(T)(T x)
|
||||
if (isFloatingPoint!T)
|
||||
{
|
||||
return log(x);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.math;
|
||||
|
||||
assert(isNaN(ln(-7.389f)));
|
||||
assert(isNaN(ln(-7.389)));
|
||||
assert(isNaN(ln(-7.389L)));
|
||||
|
||||
assert(isInfinity(ln(0.0f)));
|
||||
assert(isInfinity(ln(0.0)));
|
||||
assert(isInfinity(ln(0.0L)));
|
||||
|
||||
assert(ln(1.0f) == 0.0f);
|
||||
assert(ln(1.0) == 0.0);
|
||||
assert(ln(1.0L) == 0.0L);
|
||||
}
|
@ -12,7 +12,7 @@
|
||||
* be found in its submodules. $(D_PSYMBOL tanya.math) doesn't import any
|
||||
* submodules publically, they should be imported explicitly.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2022.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -21,7 +21,7 @@
|
||||
*/
|
||||
module tanya.math;
|
||||
|
||||
import tanya.math.nbtheory;
|
||||
import std.math;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
|
||||
@ -543,69 +543,3 @@ if (isFloatingPoint!F)
|
||||
assert(signBit(-1.0L));
|
||||
assert(!signBit(1.0L));
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes $(D_PARAM x) to the power $(D_PARAM y) modulo $(D_PARAM z).
|
||||
*
|
||||
* Params:
|
||||
* I = Base type.
|
||||
* G = Exponent type.
|
||||
* H = Divisor type:
|
||||
* x = Base.
|
||||
* y = Exponent.
|
||||
* z = Divisor.
|
||||
*
|
||||
* Returns: Reminder of the division of $(D_PARAM x) to the power $(D_PARAM y)
|
||||
* by $(D_PARAM z).
|
||||
*
|
||||
* Precondition: $(D_INLINECODE z > 0)
|
||||
*/
|
||||
H pow(I, G, H)(in auto ref I x, in auto ref G y, in auto ref H z)
|
||||
if (isIntegral!I && isIntegral!G && isIntegral!H)
|
||||
in (z > 0, "Division by zero")
|
||||
{
|
||||
G mask = G.max / 2 + 1;
|
||||
H result;
|
||||
|
||||
if (y == 0)
|
||||
{
|
||||
return 1 % z;
|
||||
}
|
||||
else if (y == 1)
|
||||
{
|
||||
return x % z;
|
||||
}
|
||||
do
|
||||
{
|
||||
immutable bit = y & mask;
|
||||
if (!result && bit)
|
||||
{
|
||||
result = x;
|
||||
continue;
|
||||
}
|
||||
|
||||
result *= result;
|
||||
if (bit)
|
||||
{
|
||||
result *= x;
|
||||
}
|
||||
result %= z;
|
||||
}
|
||||
while (mask >>= 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(pow(3, 5, 7) == 5);
|
||||
assert(pow(2, 2, 1) == 0);
|
||||
assert(pow(3, 3, 3) == 0);
|
||||
assert(pow(7, 4, 2) == 1);
|
||||
assert(pow(53, 0, 2) == 1);
|
||||
assert(pow(53, 1, 3) == 2);
|
||||
assert(pow(53, 2, 5) == 4);
|
||||
assert(pow(0, 0, 5) == 1);
|
||||
assert(pow(0, 5, 5) == 0);
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Random number generator.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2022.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,8 +14,8 @@
|
||||
*/
|
||||
module tanya.math.random;
|
||||
|
||||
import std.typecons;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.typecons;
|
||||
|
||||
/// Maximum amount gathered from the entropy sources.
|
||||
enum maxGather = 128;
|
||||
@ -90,8 +90,7 @@ abstract class EntropySource
|
||||
* Postcondition: Returned length is less than or equal to
|
||||
* $(D_PARAM output) length.
|
||||
*/
|
||||
Option!ubyte poll(out ubyte[maxGather] output) @nogc
|
||||
out (length; length.isNothing || length.get <= maxGather);
|
||||
Nullable!ubyte poll(out ubyte[maxGather] output) @nogc;
|
||||
}
|
||||
|
||||
version (CRuntime_Bionic)
|
||||
@ -151,12 +150,17 @@ version (linux)
|
||||
* Returns: Number of bytes that were copied to the $(D_PARAM output)
|
||||
* or nothing on error.
|
||||
*/
|
||||
override Option!ubyte poll(out ubyte[maxGather] output) @nogc nothrow
|
||||
override Nullable!ubyte poll(out ubyte[maxGather] output) @nogc nothrow
|
||||
out (length)
|
||||
{
|
||||
assert(length.isNull || length.get <= maxGather);
|
||||
}
|
||||
do
|
||||
{
|
||||
// int getrandom(void *buf, size_t buflen, unsigned int flags);
|
||||
import mir.linux._asm.unistd : NR_getrandom;
|
||||
auto length = syscall(NR_getrandom, output.ptr, output.length, 0);
|
||||
Option!ubyte ret;
|
||||
Nullable!ubyte ret;
|
||||
|
||||
if (length >= 0)
|
||||
{
|
||||
@ -202,11 +206,16 @@ else version (SecureARC4Random)
|
||||
* Returns: Number of bytes that were copied to the $(D_PARAM output)
|
||||
* or nothing on error.
|
||||
*/
|
||||
override Option!ubyte poll(out ubyte[maxGather] output)
|
||||
override Nullable!ubyte poll(out ubyte[maxGather] output)
|
||||
@nogc nothrow @safe
|
||||
out (length)
|
||||
{
|
||||
assert(length.isNull || length.get <= maxGather);
|
||||
}
|
||||
do
|
||||
{
|
||||
(() @trusted => arc4random_buf(output.ptr, output.length))();
|
||||
return Option!ubyte(cast(ubyte) (output.length));
|
||||
return Nullable!ubyte(cast(ubyte) (output.length));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -310,10 +319,15 @@ else version (Windows)
|
||||
* Returns: Number of bytes that were copied to the $(D_PARAM output)
|
||||
* or nothing on error.
|
||||
*/
|
||||
override Option!ubyte poll(out ubyte[maxGather] output)
|
||||
override Nullable!ubyte poll(out ubyte[maxGather] output)
|
||||
@nogc nothrow @safe
|
||||
out (length)
|
||||
{
|
||||
Option!ubyte ret;
|
||||
assert(length.isNull || length.get <= maxGather);
|
||||
}
|
||||
do
|
||||
{
|
||||
Nullable!ubyte ret;
|
||||
|
||||
assert(hProvider > 0, "hProvider not properly initialized");
|
||||
if ((() @trusted => CryptGenRandom(hProvider, output.length, cast(PBYTE) output.ptr))())
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Network interfaces.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -20,18 +20,24 @@ import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
import tanya.range;
|
||||
|
||||
version (TanyaNative)
|
||||
version (Windows)
|
||||
{
|
||||
import mir.linux._asm.unistd;
|
||||
import tanya.sys.linux.syscall;
|
||||
import tanya.sys.posix.ioctl;
|
||||
import tanya.sys.posix.net.if_;
|
||||
import tanya.sys.posix.socket;
|
||||
}
|
||||
else version (Windows)
|
||||
{
|
||||
import tanya.sys.windows.ifdef;
|
||||
import tanya.sys.windows.iphlpapi;
|
||||
private union NET_LUID_LH { ulong Value, Info; }
|
||||
private alias NET_LUID = NET_LUID_LH;
|
||||
private alias NET_IFINDEX = uint;
|
||||
private enum IF_MAX_STRING_SIZE = 256;
|
||||
extern(Windows) @nogc nothrow private @system
|
||||
{
|
||||
uint ConvertInterfaceNameToLuidA(const(char)* InterfaceName,
|
||||
NET_LUID* InterfaceLuid);
|
||||
uint ConvertInterfaceLuidToIndex(const(NET_LUID)* InterfaceLuid,
|
||||
NET_IFINDEX* InterfaceIndex);
|
||||
uint ConvertInterfaceIndexToLuid(NET_IFINDEX InterfaceIndex,
|
||||
NET_LUID* InterfaceLuid);
|
||||
uint ConvertInterfaceLuidToNameA(const(NET_LUID)* InterfaceLuid,
|
||||
char* InterfaceName,
|
||||
size_t Length);
|
||||
}
|
||||
}
|
||||
else version (Posix)
|
||||
{
|
||||
@ -52,39 +58,7 @@ else version (Posix)
|
||||
uint nameToIndex(R)(R name) @trusted
|
||||
if (isInputRange!R && is(Unqual!(ElementType!R) == char) && hasLength!R)
|
||||
{
|
||||
version (TanyaNative)
|
||||
{
|
||||
if (name.length >= IF_NAMESIZE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
ifreq ifreq_ = void;
|
||||
|
||||
copy(name, ifreq_.ifr_name[]);
|
||||
ifreq_.ifr_name[name.length] = '\0';
|
||||
|
||||
auto socket = syscall(AF_INET,
|
||||
SOCK_DGRAM | SOCK_CLOEXEC,
|
||||
0,
|
||||
NR_socket);
|
||||
if (socket <= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
scope (exit)
|
||||
{
|
||||
syscall(socket, NR_close);
|
||||
}
|
||||
if (syscall(socket,
|
||||
SIOCGIFINDEX,
|
||||
cast(ptrdiff_t) &ifreq_,
|
||||
NR_ioctl) == 0)
|
||||
{
|
||||
return ifreq_.ifr_ifindex;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else version (Windows)
|
||||
version (Windows)
|
||||
{
|
||||
if (name.length > IF_MAX_STRING_SIZE)
|
||||
{
|
||||
@ -155,33 +129,7 @@ String indexToName(uint index) @nogc nothrow @trusted
|
||||
{
|
||||
import tanya.memory.op : findNullTerminated;
|
||||
|
||||
version (TanyaNative)
|
||||
{
|
||||
ifreq ifreq_ = void;
|
||||
ifreq_.ifr_ifindex = index;
|
||||
|
||||
auto socket = syscall(AF_INET,
|
||||
SOCK_DGRAM | SOCK_CLOEXEC,
|
||||
0,
|
||||
NR_socket);
|
||||
if (socket <= 0)
|
||||
{
|
||||
return String();
|
||||
}
|
||||
scope (exit)
|
||||
{
|
||||
syscall(socket, NR_close);
|
||||
}
|
||||
if (syscall(socket,
|
||||
SIOCGIFNAME,
|
||||
cast(ptrdiff_t) &ifreq_,
|
||||
NR_ioctl) == 0)
|
||||
{
|
||||
return String(findNullTerminated(ifreq_.ifr_name));
|
||||
}
|
||||
return String();
|
||||
}
|
||||
else version (Windows)
|
||||
version (Windows)
|
||||
{
|
||||
NET_LUID luid;
|
||||
if (ConvertInterfaceIndexToLuid(index, &luid) != 0)
|
||||
@ -208,3 +156,23 @@ String indexToName(uint index) @nogc nothrow @trusted
|
||||
return String(findNullTerminated(buffer));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* $(D_PSYMBOL AddressFamily) specifies a communication domain; this selects
|
||||
* the protocol family which will be used for communication.
|
||||
*/
|
||||
enum AddressFamily : int
|
||||
{
|
||||
unspec = 0, /// Unspecified.
|
||||
local = 1, /// Local to host (pipes and file-domain).
|
||||
unix = local, /// POSIX name for PF_LOCAL.
|
||||
inet = 2, /// IP protocol family.
|
||||
ax25 = 3, /// Amateur Radio AX.25.
|
||||
ipx = 4, /// Novell Internet Protocol.
|
||||
appletalk = 5, /// Appletalk DDP.
|
||||
netrom = 6, /// Amateur radio NetROM.
|
||||
bridge = 7, /// Multiprotocol bridge.
|
||||
atmpvc = 8, /// ATM PVCs.
|
||||
x25 = 9, /// Reserved for X.25 project.
|
||||
inet6 = 10, /// IP version 6.
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Internet utilities.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* Copyright: Eugene Wissner 2016-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -72,7 +72,11 @@ if (L > ubyte.sizeof && L <= ulong.sizeof)
|
||||
*/
|
||||
this(T)(T value)
|
||||
if (isUnsigned!T)
|
||||
in (value <= (2 ^^ (L * 8)) - 1)
|
||||
in
|
||||
{
|
||||
assert(value <= (2 ^^ (L * 8)) - 1);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.value = value & StorageType.max;
|
||||
}
|
||||
@ -83,7 +87,11 @@ if (L > ubyte.sizeof && L <= ulong.sizeof)
|
||||
* Precondition: $(D_INLINECODE length > 0).
|
||||
*/
|
||||
@property ubyte back() const
|
||||
in (this.length > 0)
|
||||
in
|
||||
{
|
||||
assert(this.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
return this.value & 0xff;
|
||||
}
|
||||
@ -94,7 +102,11 @@ if (L > ubyte.sizeof && L <= ulong.sizeof)
|
||||
* Precondition: $(D_INLINECODE length > 0).
|
||||
*/
|
||||
@property ubyte front() const
|
||||
in (this.length > 0)
|
||||
in
|
||||
{
|
||||
assert(this.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
return (this.value >> ((this.length - 1) * 8)) & 0xff;
|
||||
}
|
||||
@ -105,7 +117,11 @@ if (L > ubyte.sizeof && L <= ulong.sizeof)
|
||||
* Precondition: $(D_INLINECODE length > 0).
|
||||
*/
|
||||
void popBack()
|
||||
in (this.length > 0)
|
||||
in
|
||||
{
|
||||
assert(this.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.value >>= 8;
|
||||
--this.size;
|
||||
@ -117,7 +133,11 @@ if (L > ubyte.sizeof && L <= ulong.sizeof)
|
||||
* Precondition: $(D_INLINECODE length > 0).
|
||||
*/
|
||||
void popFront()
|
||||
in (this.length > 0)
|
||||
in
|
||||
{
|
||||
assert(this.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.value &= StorageType.max >> ((StorageType.sizeof - this.length) * 8);
|
||||
--this.size;
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Internet Protocol implementation.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,12 +14,14 @@
|
||||
*/
|
||||
module tanya.net.ip;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.algorithm.comparison;
|
||||
import std.ascii;
|
||||
import std.sumtype;
|
||||
import std.typecons;
|
||||
import tanya.algorithm.iteration;
|
||||
import tanya.algorithm.mutation;
|
||||
import tanya.container.string;
|
||||
import tanya.conv;
|
||||
import tanya.encoding.ascii;
|
||||
import tanya.format;
|
||||
import tanya.memory.lifetime;
|
||||
import tanya.meta.trait;
|
||||
@ -27,7 +29,6 @@ import tanya.meta.transform;
|
||||
import tanya.net.iface;
|
||||
import tanya.net.inet;
|
||||
import tanya.range;
|
||||
import tanya.typecons;
|
||||
|
||||
/**
|
||||
* IPv4 internet address.
|
||||
@ -94,8 +95,8 @@ struct Address4
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(address4("127.0.0.1") > address4("126.0.0.0"));
|
||||
assert(address4("127.0.0.1") < address4("127.0.0.2"));
|
||||
assert(address4("127.0.0.1").get > address4("126.0.0.0").get);
|
||||
assert(address4("127.0.0.1").get < address4("127.0.0.2").get);
|
||||
assert(address4("127.0.0.1") == address4("127.0.0.1"));
|
||||
}
|
||||
|
||||
@ -233,26 +234,6 @@ struct Address4
|
||||
assert(address4("192.168.0.1").get.isUnicast());
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a string containing an IPv4 address in dotted-decimal notation.
|
||||
*
|
||||
* Returns: This address in dotted-decimal notation.
|
||||
*/
|
||||
deprecated("Use Address4.toString() instead")
|
||||
String stringify() const @nogc nothrow pure @safe
|
||||
{
|
||||
const octets = (() @trusted => (cast(ubyte*) &this.address)[0 .. 4])();
|
||||
enum string fmt = "{}.{}.{}.{}";
|
||||
version (LittleEndian)
|
||||
{
|
||||
return format!fmt(octets[0], octets[1], octets[2], octets[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
return format!fmt(octets[3], octets[2], octets[1], octets[0]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes this IPv4 address in dotted-decimal notation.
|
||||
*
|
||||
@ -262,7 +243,7 @@ struct Address4
|
||||
*
|
||||
* Returns: $(D_PARAM output).
|
||||
*/
|
||||
OR toString(OR)(OR output) const @nogc nothrow pure @safe
|
||||
OR toString(OR)(OR output) const
|
||||
if (isOutputRange!(OR, const(char)[]))
|
||||
{
|
||||
const octets = (() @trusted => (cast(ubyte*) &this.address)[0 .. 4])();
|
||||
@ -344,10 +325,10 @@ struct Address4
|
||||
* R = Input range type.
|
||||
* range = Stringish range containing the address.
|
||||
*
|
||||
* Returns: $(D_PSYMBOL Option) containing the address if the parsing was
|
||||
* Returns: $(D_PSYMBOL Nullable) containing the address if the parsing was
|
||||
* successful, or nothing otherwise.
|
||||
*/
|
||||
Option!Address4 address4(R)(R range)
|
||||
Nullable!Address4 address4(R)(R range)
|
||||
if (isForwardRange!R && is(Unqual!(ElementType!R) == char) && hasLength!R)
|
||||
{
|
||||
Address4 result;
|
||||
@ -390,10 +371,10 @@ if (isForwardRange!R && is(Unqual!(ElementType!R) == char) && hasLength!R)
|
||||
* R = Input range type.
|
||||
* range = $(D_KEYWORD ubyte) range containing the address.
|
||||
*
|
||||
* Returns: $(D_PSYMBOL Option) containing the address if the $(D_PARAM range)
|
||||
* Returns: $(D_PSYMBOL Nullable) containing the address if the $(D_PARAM range)
|
||||
* contains exactly 4 bytes, or nothing otherwise.
|
||||
*/
|
||||
Option!Address4 address4(R)(R range)
|
||||
Nullable!Address4 address4(R)(R range)
|
||||
if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte))
|
||||
{
|
||||
Address4 result;
|
||||
@ -427,11 +408,11 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte))
|
||||
}
|
||||
{
|
||||
ubyte[3] actual = [127, 0, 0];
|
||||
assert(address4(actual[]).isNothing);
|
||||
assert(address4(actual[]).isNull);
|
||||
}
|
||||
{
|
||||
ubyte[5] actual = [127, 0, 0, 0, 1];
|
||||
assert(address4(actual[]).isNothing);
|
||||
assert(address4(actual[]).isNull);
|
||||
}
|
||||
}
|
||||
|
||||
@ -485,7 +466,7 @@ struct Address6
|
||||
*/
|
||||
int opCmp(ref const Address6 that) const @nogc nothrow pure @safe
|
||||
{
|
||||
const diff = compare(this.address[], that.address[]);
|
||||
const diff = cmp(this.address[], that.address[]);
|
||||
if (diff == 0)
|
||||
{
|
||||
return (that.scopeID < this.scopeID) - (this.scopeID < that.scopeID);
|
||||
@ -502,11 +483,11 @@ struct Address6
|
||||
///
|
||||
@nogc nothrow @safe unittest
|
||||
{
|
||||
assert(address6("::14") > address6("::1"));
|
||||
assert(address6("::1") < address6("::14"));
|
||||
assert(address6("::14").get > address6("::1").get);
|
||||
assert(address6("::1").get < address6("::14").get);
|
||||
assert(address6("::1") == address6("::1"));
|
||||
assert(address6("fe80::1%1") < address6("fe80::1%2"));
|
||||
assert(address6("fe80::1%2") > address6("fe80::1%1"));
|
||||
assert(address6("fe80::1%1").get < address6("fe80::1%2").get);
|
||||
assert(address6("fe80::1%2").get > address6("fe80::1%1").get);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -650,20 +631,6 @@ struct Address6
|
||||
assert(address6("fd80:124e:34f3::1").get.isUniqueLocal());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns text representation of this address.
|
||||
*
|
||||
* Returns: text representation of this address.
|
||||
*/
|
||||
deprecated("Use Address6.toString() instead")
|
||||
String stringify() const @nogc nothrow pure @safe
|
||||
{
|
||||
String output;
|
||||
|
||||
toString(backInserter(output));
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes text representation of this address to an output range.
|
||||
*
|
||||
@ -788,7 +755,11 @@ private void read2Bytes(R)(ref R range, ubyte[] address)
|
||||
}
|
||||
|
||||
private char toHexDigit(ubyte digit) @nogc nothrow pure @safe
|
||||
in (digit < 16)
|
||||
in
|
||||
{
|
||||
assert(digit < 16);
|
||||
}
|
||||
do
|
||||
{
|
||||
return cast(char) (digit >= 10 ? (digit - 10 + 'a') : (digit + '0'));
|
||||
}
|
||||
@ -796,7 +767,11 @@ in (digit < 16)
|
||||
private bool writeHexDigit(OR)(ref OR output,
|
||||
ubyte digit,
|
||||
bool groupStarted = false)
|
||||
in (digit < 16)
|
||||
in
|
||||
{
|
||||
assert(digit < 16);
|
||||
}
|
||||
do
|
||||
{
|
||||
if (digit != 0 || groupStarted)
|
||||
{
|
||||
@ -829,10 +804,10 @@ in (digit < 16)
|
||||
* R = Input range type.
|
||||
* range = Stringish range containing the address.
|
||||
*
|
||||
* Returns: $(D_PSYMBOL Option) containing the address if the parsing was
|
||||
* Returns: $(D_PSYMBOL Nullable) containing the address if the parsing was
|
||||
* successful, or nothing otherwise.
|
||||
*/
|
||||
Option!Address6 address6(R)(R range)
|
||||
Nullable!Address6 address6(R)(R range)
|
||||
if (isForwardRange!R && is(Unqual!(ElementType!R) == char) && hasLength!R)
|
||||
{
|
||||
if (range.empty)
|
||||
@ -1040,10 +1015,10 @@ CopyTail:
|
||||
* range = $(D_KEYWORD ubyte) range containing the address.
|
||||
* scopeID = Scope ID.
|
||||
*
|
||||
* Returns: $(D_PSYMBOL Option) containing the address if the $(D_PARAM range)
|
||||
* Returns: $(D_PSYMBOL Nullable) containing the address if the $(D_PARAM range)
|
||||
* contains exactly 16 bytes, or nothing otherwise.
|
||||
*/
|
||||
Option!Address6 address6(R)(R range, uint scopeID = 0)
|
||||
Nullable!Address6 address6(R)(R range, uint scopeID = 0)
|
||||
if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte))
|
||||
{
|
||||
Address6 result;
|
||||
@ -1064,20 +1039,20 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte))
|
||||
{
|
||||
ubyte[16] actual = [ 1, 2, 3, 4, 5, 6, 7, 8,
|
||||
9, 10, 11, 12, 13, 14, 15, 16 ];
|
||||
assert(!address6(actual[]).isNothing);
|
||||
assert(!address6(actual[]).isNull);
|
||||
}
|
||||
{
|
||||
ubyte[15] actual = [ 1, 2, 3, 4, 5, 6, 7, 8,
|
||||
9, 10, 11, 12, 13, 14, 15 ];
|
||||
assert(address6(actual[]).isNothing);
|
||||
assert(address6(actual[]).isNull);
|
||||
}
|
||||
{
|
||||
ubyte[17] actual = [ 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
10, 11, 12, 13, 14, 15, 16, 17 ];
|
||||
assert(address6(actual[]).isNothing);
|
||||
assert(address6(actual[]).isNull);
|
||||
}
|
||||
{
|
||||
assert(address6(cast(ubyte[]) []).isNothing);
|
||||
assert(address6(cast(ubyte[]) []).isNull);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1086,7 +1061,7 @@ if (isInputRange!R && is(Unqual!(ElementType!R) == ubyte))
|
||||
*/
|
||||
struct Address
|
||||
{
|
||||
private Variant!(Address4, Address6) address;
|
||||
private SumType!(Address4, Address6) address;
|
||||
|
||||
@disable this();
|
||||
|
||||
@ -1120,7 +1095,10 @@ struct Address
|
||||
*/
|
||||
bool isV4() const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.address.peek!Address4;
|
||||
return this.address.match!(
|
||||
(Address4 address4) => true,
|
||||
(Address6 address6) => false
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1137,7 +1115,10 @@ struct Address
|
||||
*/
|
||||
bool isV6() const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.address.peek!Address6;
|
||||
return this.address.match!(
|
||||
(Address4 address4) => false,
|
||||
(Address6 address6) => true
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1156,10 +1137,12 @@ struct Address
|
||||
*
|
||||
* Precondition: This is an IPv4 address.
|
||||
*/
|
||||
ref inout(Address4) toV4() inout @nogc nothrow pure @safe
|
||||
in (this.address.peek!Address4)
|
||||
Address4 toV4() inout @nogc nothrow pure @safe
|
||||
{
|
||||
return this.address.get!Address4;
|
||||
return this.address.match!(
|
||||
(Address4 address4) => address4,
|
||||
_ => assert(false, "Not an IPv4 address")
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1179,10 +1162,12 @@ struct Address
|
||||
*
|
||||
* Precondition: This is an IPv6 address.
|
||||
*/
|
||||
ref inout(Address6) toV6() inout @nogc nothrow pure @safe
|
||||
in (this.address.peek!Address6)
|
||||
Address6 toV6() inout @nogc nothrow pure @safe
|
||||
{
|
||||
return this.address.get!Address6;
|
||||
return this.address.match!(
|
||||
(Address6 address6) => address6,
|
||||
_ => assert(false, "Not an IPv6 address")
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1202,13 +1187,11 @@ struct Address
|
||||
* $(D_PSYMBOL Address6.loopback).
|
||||
*/
|
||||
bool isLoopback() const @nogc nothrow pure @safe
|
||||
in (this.address.hasValue)
|
||||
{
|
||||
if (this.address.peek!Address4)
|
||||
{
|
||||
return this.address.get!Address4.isLoopback();
|
||||
}
|
||||
return this.address.get!Address6.isLoopback();
|
||||
return this.address.match!(
|
||||
(Address4 address) => address.isLoopback(),
|
||||
(Address6 address) => address.isLoopback()
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1228,13 +1211,11 @@ struct Address
|
||||
* $(D_PSYMBOL Address6.isMulticast).
|
||||
*/
|
||||
bool isMulticast() const @nogc nothrow pure @safe
|
||||
in (this.address.hasValue)
|
||||
{
|
||||
if (this.address.peek!Address4)
|
||||
{
|
||||
return this.address.get!Address4.isMulticast();
|
||||
}
|
||||
return this.address.get!Address6.isMulticast();
|
||||
return this.address.match!(
|
||||
(Address4 address) => address.isMulticast(),
|
||||
(Address6 address) => address.isMulticast()
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1253,13 +1234,11 @@ struct Address
|
||||
* See_Also: $(D_PSYMBOL Address4.isAny), $(D_PSYMBOL Address6.isAny).
|
||||
*/
|
||||
bool isAny() const @nogc nothrow pure @safe
|
||||
in (this.address.hasValue)
|
||||
{
|
||||
if (this.address.peek!Address4)
|
||||
{
|
||||
return this.address.get!Address4.isAny();
|
||||
}
|
||||
return this.address.get!Address6.isAny();
|
||||
return this.address.match!(
|
||||
(Address4 address) => address.isAny(),
|
||||
(Address6 address) => address.isAny()
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1282,14 +1261,22 @@ struct Address
|
||||
* otherwise.
|
||||
*/
|
||||
bool opEquals(T)(T that) const
|
||||
if (is(Unqual!T == Address4) || is(Unqual!T == Address6))
|
||||
if (is(Unqual!T == Address4))
|
||||
{
|
||||
alias AddressType = Unqual!T;
|
||||
if (this.address.peek!AddressType)
|
||||
{
|
||||
return this.address.get!AddressType == that;
|
||||
}
|
||||
return false;
|
||||
return this.address.match!(
|
||||
(Address4 address) => address == that,
|
||||
(Address6 address) => false
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
bool opEquals(T)(T that) const
|
||||
if (is(Unqual!T == Address6))
|
||||
{
|
||||
return this.address.match!(
|
||||
(Address4 address) => false,
|
||||
(Address6 address) => address == that,
|
||||
);
|
||||
}
|
||||
|
||||
///
|
||||
@ -1329,3 +1316,98 @@ struct Address
|
||||
assert(address == Address4.loopback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Service endpoint specified by a version independent IP address and port.
|
||||
*/
|
||||
struct Endpoint
|
||||
{
|
||||
private AddressFamily family = AddressFamily.unspec;
|
||||
private ubyte[ushort.sizeof] service;
|
||||
private Address4 address4; // Unused sin6_flowinfo if IPv6
|
||||
private Address6 address6; // Unused if IPv4
|
||||
|
||||
static assert(Address4.sizeof == 4);
|
||||
|
||||
/// Allows the system to select a free port.
|
||||
enum ushort anyPort = 0;
|
||||
|
||||
/**
|
||||
* Constructs an endpoint.
|
||||
*
|
||||
* Params:
|
||||
* T = Address type (IPv4 or IPv6).
|
||||
* address = IP address that should be associated with the endpoint.
|
||||
* port = Port number in network byte order.
|
||||
*/
|
||||
this(T)(T address, const ushort port = anyPort)
|
||||
if (is(T == Address) || is(T == Address4) || is(T == Address6))
|
||||
{
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: Port number in network byte order.
|
||||
*/
|
||||
@property inout(ushort) port() inout const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.service[].toHostOrder!ushort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* port = Port number in network byte order.
|
||||
*/
|
||||
@property void port(const ushort port) @nogc nothrow pure @safe
|
||||
{
|
||||
NetworkOrder!(ushort.sizeof)(port).copy(this.service[]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns: IP address associated with the endpoint.
|
||||
*/
|
||||
@property inout(Address) address() inout @nogc nothrow pure @safe
|
||||
{
|
||||
if (this.family == AddressFamily.inet)
|
||||
{
|
||||
return Address(this.address4);
|
||||
}
|
||||
else if (this.family == AddressFamily.inet6)
|
||||
{
|
||||
return Address(this.address6);
|
||||
}
|
||||
return Address.init;
|
||||
}
|
||||
|
||||
/**
|
||||
* Params:
|
||||
* address = IP address associated with the endpoint.
|
||||
*/
|
||||
@property void address(Address address) @nogc nothrow pure @safe
|
||||
{
|
||||
if (address.isV4())
|
||||
{
|
||||
this.address = address.toV4();
|
||||
}
|
||||
else if (address.isV6())
|
||||
{
|
||||
this.address = address.toV6();
|
||||
}
|
||||
}
|
||||
|
||||
/// ditto
|
||||
@property void address(Address4 address) @nogc nothrow pure @safe
|
||||
{
|
||||
this.family = AddressFamily.inet;
|
||||
this.address4 = address;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
@property void address(Address6 address) @nogc nothrow pure @safe
|
||||
{
|
||||
this.family = AddressFamily.inet6;
|
||||
this.address4 = Address4(0);
|
||||
this.address6 = address;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Network programming.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2022.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* URL parser.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,8 +14,8 @@
|
||||
*/
|
||||
module tanya.net.uri;
|
||||
|
||||
import std.ascii;
|
||||
import tanya.conv;
|
||||
import tanya.encoding.ascii;
|
||||
import tanya.memory.allocator;
|
||||
|
||||
/**
|
||||
|
@ -1,17 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Network programming.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2016-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/network/package.d,
|
||||
* tanya/network/package.d)
|
||||
*/
|
||||
module tanya.network;
|
||||
|
||||
public import tanya.network.socket;
|
File diff suppressed because it is too large
Load Diff
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Range adapters transform some data structures into ranges.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -170,7 +170,11 @@ if (isArray!Array)
|
||||
|
||||
void opCall(T)(auto ref T data)
|
||||
if (is(T : E))
|
||||
in (!this.data.empty)
|
||||
in
|
||||
{
|
||||
assert(!this.data.empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
put(this.data, data);
|
||||
}
|
||||
|
@ -31,7 +31,7 @@
|
||||
* (D_INLINECODE dchar[])) are treated as any other normal array, they aren't
|
||||
* auto-decoded.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -55,7 +55,11 @@ module tanya.range.array;
|
||||
* Precondition: $(D_INLINECODE array.length > 0).
|
||||
*/
|
||||
@property ref inout(T) front(T)(return scope inout(T)[] array)
|
||||
in (array.length > 0)
|
||||
in
|
||||
{
|
||||
assert(array.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
return array[0];
|
||||
}
|
||||
@ -91,7 +95,11 @@ in (array.length > 0)
|
||||
* Precondition: $(D_INLINECODE array.length > 0).
|
||||
*/
|
||||
@property ref inout(T) back(T)(return scope inout(T)[] array)
|
||||
in (array.length > 0)
|
||||
in
|
||||
{
|
||||
assert(array.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
return array[$ - 1];
|
||||
}
|
||||
@ -125,15 +133,23 @@ in (array.length > 0)
|
||||
*
|
||||
* Precondition: $(D_INLINECODE array.length > 0).
|
||||
*/
|
||||
void popFront(T)(scope ref inout(T)[] array)
|
||||
in (array.length > 0)
|
||||
void popFront(T)(ref inout(T)[] array)
|
||||
in
|
||||
{
|
||||
assert(array.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
array = array[1 .. $];
|
||||
}
|
||||
|
||||
/// ditto
|
||||
void popBack(T)(scope ref inout(T)[] array)
|
||||
in (array.length > 0)
|
||||
void popBack(T)(ref inout(T)[] array)
|
||||
in
|
||||
{
|
||||
assert(array.length > 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
array = array[0 .. $ - 1];
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
* This package contains generic functions and templates to be used with D
|
||||
* ranges.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* This module defines primitives for working with ranges.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -14,7 +14,7 @@
|
||||
*/
|
||||
module tanya.range.primitive;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.algorithm.comparison;
|
||||
import tanya.memory.lifetime;
|
||||
import tanya.meta.trait;
|
||||
import tanya.meta.transform;
|
||||
@ -305,7 +305,7 @@ private template isDynamicArrayRange(R)
|
||||
|
||||
private struct Primitive(Candidate, string primitive)
|
||||
{
|
||||
auto ref returnType(Candidate candidate)
|
||||
auto ref returnType(ref Candidate candidate)
|
||||
{
|
||||
mixin("return candidate." ~ primitive ~ ";");
|
||||
}
|
||||
@ -630,13 +630,7 @@ template isRandomAccessRange(R)
|
||||
*/
|
||||
void put(R, E)(ref R range, auto ref E e)
|
||||
{
|
||||
static if (__traits(hasMember, R, "put")
|
||||
&& is(typeof((R r, E e) => r.put(e))))
|
||||
{
|
||||
pragma(msg, "OutputRange.put()-primitive is deprecated. Define opCall() instead.");
|
||||
range.put(e);
|
||||
}
|
||||
else static if (is(typeof((R r, E e) => r(e))))
|
||||
static if (is(typeof((R r, E e) => r(e))))
|
||||
{
|
||||
range(e);
|
||||
}
|
||||
@ -1562,7 +1556,11 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
||||
*/
|
||||
ElementType!R getAndPopFront(R)(ref R range)
|
||||
if (isInputRange!R)
|
||||
in (!range.empty)
|
||||
in
|
||||
{
|
||||
assert(!range.empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
static if (hasLvalueElements!R)
|
||||
{
|
||||
@ -1615,7 +1613,11 @@ in (!range.empty)
|
||||
*/
|
||||
auto ref getAndPopBack(R)(ref R range)
|
||||
if (isBidirectionalRange!R)
|
||||
in (!range.empty)
|
||||
in
|
||||
{
|
||||
assert(!range.empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
static if (hasLvalueElements!R)
|
||||
{
|
||||
|
@ -1,701 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Type constructors.
|
||||
*
|
||||
* This module contains templates that allow to build new types from the
|
||||
* available ones.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
* Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/typecons.d,
|
||||
* tanya/typecons.d)
|
||||
*/
|
||||
module tanya.typecons;
|
||||
|
||||
import tanya.format;
|
||||
import tanya.memory.lifetime;
|
||||
import tanya.meta.metafunction;
|
||||
import tanya.meta.trait;
|
||||
|
||||
/**
|
||||
* $(D_PSYMBOL Tuple) can store two or more heterogeneous objects.
|
||||
*
|
||||
* The objects can by accessed by index as `obj[0]` and `obj[1]` or by optional
|
||||
* names (e.g. `obj.first`).
|
||||
*
|
||||
* $(D_PARAM Specs) contains a list of object types and names. First
|
||||
* comes the object type, then an optional string containing the name.
|
||||
* If you want the object be accessible only by its index (`0` or `1`),
|
||||
* just skip the name.
|
||||
*
|
||||
* Params:
|
||||
* Specs = Field types and names.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL tuple).
|
||||
*/
|
||||
template Tuple(Specs...)
|
||||
{
|
||||
template parseSpecs(size_t fieldCount, Specs...)
|
||||
{
|
||||
static if (Specs.length == 0)
|
||||
{
|
||||
alias parseSpecs = AliasSeq!();
|
||||
}
|
||||
else static if (is(Specs[0]) && fieldCount < 2)
|
||||
{
|
||||
static if (is(typeof(Specs[1]) == string))
|
||||
{
|
||||
alias parseSpecs
|
||||
= AliasSeq!(Pack!(Specs[0], Specs[1]),
|
||||
parseSpecs!(fieldCount + 1, Specs[2 .. $]));
|
||||
}
|
||||
else
|
||||
{
|
||||
alias parseSpecs
|
||||
= AliasSeq!(Pack!(Specs[0]),
|
||||
parseSpecs!(fieldCount + 1, Specs[1 .. $]));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
static assert(false, "Invalid argument: " ~ Specs[0].stringof);
|
||||
}
|
||||
}
|
||||
|
||||
alias ChooseType(alias T) = T.Seq[0];
|
||||
alias ParsedSpecs = parseSpecs!(0, Specs);
|
||||
|
||||
static assert(ParsedSpecs.length > 1, "Invalid argument count");
|
||||
|
||||
private string formatAliases(size_t n, Specs...)()
|
||||
{
|
||||
static if (Specs.length == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
else
|
||||
{
|
||||
string fieldAlias;
|
||||
static if (Specs[0].length == 2)
|
||||
{
|
||||
char[21] buffer;
|
||||
fieldAlias = "alias " ~ Specs[0][1] ~ " = expand["
|
||||
~ integral2String(n, buffer).idup ~ "];";
|
||||
}
|
||||
return fieldAlias ~ formatAliases!(n + 1, Specs[1 .. $])();
|
||||
}
|
||||
}
|
||||
|
||||
struct Tuple
|
||||
{
|
||||
/// Field types.
|
||||
alias Types = Map!(ChooseType, ParsedSpecs);
|
||||
|
||||
// Create field aliases.
|
||||
mixin(formatAliases!(0, ParsedSpecs[0 .. $])());
|
||||
|
||||
/// Represents the values of the $(D_PSYMBOL Tuple) as a list of values.
|
||||
Types expand;
|
||||
|
||||
alias expand this;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto pair = Tuple!(int, "first", string, "second")(1, "second");
|
||||
assert(pair.first == 1);
|
||||
assert(pair[0] == 1);
|
||||
assert(pair.second == "second");
|
||||
assert(pair[1] == "second");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new $(D_PSYMBOL Tuple).
|
||||
*
|
||||
* Params:
|
||||
* Names = Field names.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL Tuple).
|
||||
*/
|
||||
template tuple(Names...)
|
||||
{
|
||||
/**
|
||||
* Creates a new $(D_PSYMBOL Tuple).
|
||||
*
|
||||
* Params:
|
||||
* Args = Field types.
|
||||
* args = Field values.
|
||||
*
|
||||
* Returns: Newly created $(D_PSYMBOL Tuple).
|
||||
*/
|
||||
auto tuple(Args...)(auto ref Args args)
|
||||
if (Args.length >= Names.length && isTypeTuple!Args)
|
||||
{
|
||||
alias Zipped = ZipWith!(AliasSeq, Pack!Args, Pack!Names);
|
||||
alias Nameless = Args[Names.length .. $];
|
||||
|
||||
return Tuple!(Zipped, Nameless)(forward!args);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto t = tuple!("one", "two")(20, 5);
|
||||
assert(t.one == 20);
|
||||
assert(t.two == 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* $(D_PSYMBOL Option) is a type that contains an optional value.
|
||||
*
|
||||
* Params:
|
||||
* T = Type of the encapsulated value.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL option).
|
||||
*/
|
||||
struct Option(T)
|
||||
{
|
||||
private bool isNothing_ = true;
|
||||
private T value = void;
|
||||
|
||||
/**
|
||||
* Constructs a new option with $(D_PARAM value).
|
||||
*
|
||||
* Params:
|
||||
* value = Encapsulated value.
|
||||
*/
|
||||
this()(ref T value)
|
||||
{
|
||||
this.value = value;
|
||||
this.isNothing_ = false;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
this()(T value) @trusted
|
||||
{
|
||||
moveEmplace(value, this.value);
|
||||
this.isNothing_ = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if the option is just a value or nothing.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL Option) contains a nothing,
|
||||
* $(D_KEYWORD false) if it contains a value.
|
||||
*/
|
||||
@property bool isNothing() const
|
||||
{
|
||||
return this.isNothing_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the encapsulated value.
|
||||
*
|
||||
* Returns: Value encapsulated in this $(D_PSYMBOL Option).
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL or).
|
||||
*
|
||||
* Precondition: `!isNothing`.
|
||||
*/
|
||||
@property ref inout(T) get() inout
|
||||
in (!isNothing, "Option is nothing")
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
deprecated("Call Option.get explicitly instead of relying on alias this")
|
||||
@property ref inout(T) get_() inout
|
||||
in (!isNothing, "Option is nothing")
|
||||
{
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the encapsulated value if available or a default value
|
||||
* otherwise.
|
||||
*
|
||||
* Note that the contained value can be returned by reference only if the
|
||||
* default value is passed by reference as well.
|
||||
*
|
||||
* Params:
|
||||
* U = Type of the default value.
|
||||
* defaultValue = Default value.
|
||||
*
|
||||
* Returns: The value of this $(D_PSYMBOL Option) if available,
|
||||
* $(D_PARAM defaultValue) otherwise.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL isNothing), $(D_PSYMBOL get).
|
||||
*/
|
||||
@property U or(U)(U defaultValue) inout
|
||||
if (is(U == T) && isCopyable!T)
|
||||
{
|
||||
return isNothing ? defaultValue : this.value;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
@property ref inout(T) or(ref inout(T) defaultValue) inout
|
||||
{
|
||||
return isNothing ? defaultValue : this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts this $(D_PSYMBOL Option) to $(D_KEYWORD bool).
|
||||
*
|
||||
* An $(D_PSYMBOL Option) is $(D_KEYWORD true) if it contains a value,
|
||||
* ($D_KEYWORD false) if it contains nothing.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL Option) contains a value,
|
||||
* ($D_KEYWORD false) if it contains nothing.
|
||||
*/
|
||||
bool opCast(U : bool)()
|
||||
{
|
||||
return !isNothing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this $(D_PSYMBOL Option) with $(D_PARAM that).
|
||||
*
|
||||
* If both objects are options of the same type and they don't contain a
|
||||
* value, they are considered equal. If only one of them contains a value,
|
||||
* they aren't equal. Otherwise, the encapsulated values are compared for
|
||||
* equality.
|
||||
*
|
||||
* If $(D_PARAM U) is a type comparable with the type encapsulated by this
|
||||
* $(D_PSYMBOL Option), the value of this $(D_PSYMBOL Option) is compared
|
||||
* with $(D_PARAM that), this $(D_PSYMBOL Option) must have a value then.
|
||||
*
|
||||
* Params:
|
||||
* U = Type of the object to compare with.
|
||||
* that = Object to compare with.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL Option) and
|
||||
* $(D_PARAM that) are equal, $(D_KEYWORD false) if not.
|
||||
*
|
||||
* Precondition: `!isNothing` if $(D_PARAM U) is equality comparable with
|
||||
* $(D_PARAM T).
|
||||
*/
|
||||
bool opEquals(U)(auto ref const U that) const
|
||||
if (is(U == Option))
|
||||
{
|
||||
if (!isNothing && !that.isNothing)
|
||||
{
|
||||
return this.value == that.value;
|
||||
}
|
||||
return isNothing == that.isNothing;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
bool opEquals(U)(auto ref const U that) const
|
||||
if (ifTestable!(U, a => a == T.init) && !is(U == Option))
|
||||
in (!isNothing)
|
||||
{
|
||||
return get == that;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets this $(D_PSYMBOL Option) and destroys the contained value.
|
||||
*
|
||||
* $(D_PSYMBOL reset) can be safely called on an $(D_PSYMBOL Option) that
|
||||
* doesn't contain any value.
|
||||
*/
|
||||
void reset()
|
||||
{
|
||||
static if (hasElaborateDestructor!T)
|
||||
{
|
||||
destroy(this.value);
|
||||
}
|
||||
this.isNothing_ = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a new value.
|
||||
*
|
||||
* Params:
|
||||
* U = Type of the new value.
|
||||
* that = New value.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
ref typeof(this) opAssign(U)(ref U that)
|
||||
if (is(U : T) && !is(U == Option))
|
||||
{
|
||||
this.value = that;
|
||||
this.isNothing_ = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref typeof(this) opAssign(U)(U that)
|
||||
if (is(U == T))
|
||||
{
|
||||
move(that, this.value);
|
||||
this.isNothing_ = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref typeof(this) opAssign(U)(ref U that)
|
||||
if (is(U == Option))
|
||||
{
|
||||
if (that.isNothing)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.value = that.get;
|
||||
this.isNothing_ = false;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref typeof(this) opAssign(U)(U that)
|
||||
if (is(U == Option))
|
||||
{
|
||||
move(that.value, this.value);
|
||||
this.isNothing_ = that.isNothing_;
|
||||
return this;
|
||||
}
|
||||
|
||||
version (D_Ddoc)
|
||||
{
|
||||
/**
|
||||
* If $(D_PARAM T) has a `toHash()` method, $(D_PSYMBOL Option) defines
|
||||
* `toHash()` which returns `T.toHash()` if it is set or 0 otherwise.
|
||||
*
|
||||
* Returns: Hash value.
|
||||
*/
|
||||
size_t toHash() const;
|
||||
}
|
||||
else static if (is(typeof(T.init.toHash()) == size_t))
|
||||
{
|
||||
size_t toHash() const
|
||||
{
|
||||
return isNothing ? 0U : this.value.toHash();
|
||||
}
|
||||
}
|
||||
|
||||
alias get_ this;
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
Option!int option;
|
||||
assert(option.isNothing);
|
||||
assert(option.or(8) == 8);
|
||||
|
||||
option = 5;
|
||||
assert(!option.isNothing);
|
||||
assert(option.get == 5);
|
||||
assert(option.or(8) == 5);
|
||||
|
||||
option.reset();
|
||||
assert(option.isNothing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new $(D_PSYMBOL Option).
|
||||
*
|
||||
* Params:
|
||||
* T = Option type.
|
||||
* value = Initial value.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL Option).
|
||||
*/
|
||||
Option!T option(T)(auto ref T value)
|
||||
{
|
||||
return Option!T(forward!value);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
Option!T option(T)()
|
||||
{
|
||||
return Option!T();
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(option!int().isNothing);
|
||||
assert(option(5) == 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Type that can hold one of the types listed as its template parameters.
|
||||
*
|
||||
* $(D_PSYMBOL Variant) is a type similar to $(D_KEYWORD union), but
|
||||
* $(D_PSYMBOL Variant) keeps track of the actually used type and throws an
|
||||
* assertion error when trying to access an invalid type at runtime.
|
||||
*
|
||||
* Params:
|
||||
* Specs = Types this $(D_SPYBMOL Variant) can hold.
|
||||
*/
|
||||
template Variant(Specs...)
|
||||
if (isTypeTuple!Specs && NoDuplicates!Specs.length == Specs.length)
|
||||
{
|
||||
union AlignedUnion(Args...)
|
||||
{
|
||||
static if (Args.length > 0)
|
||||
{
|
||||
Args[0] value;
|
||||
}
|
||||
static if (Args.length > 1)
|
||||
{
|
||||
AlignedUnion!(Args[1 .. $]) rest;
|
||||
}
|
||||
}
|
||||
|
||||
private struct VariantAccessorInfo
|
||||
{
|
||||
string accessor;
|
||||
ptrdiff_t tag;
|
||||
}
|
||||
|
||||
template accessor(T, Union)
|
||||
{
|
||||
enum VariantAccessorInfo info = accessorImpl!(T, Union, 1);
|
||||
enum accessor = VariantAccessorInfo("this.values" ~ info.accessor, info.tag);
|
||||
}
|
||||
|
||||
template accessorImpl(T, Union, size_t tag)
|
||||
{
|
||||
static if (is(T == typeof(Union.value)))
|
||||
{
|
||||
enum accessorImpl = VariantAccessorInfo(".value", tag);
|
||||
}
|
||||
else
|
||||
{
|
||||
enum VariantAccessorInfo info = accessorImpl!(T, typeof(Union.rest), tag + 1);
|
||||
enum accessorImpl = VariantAccessorInfo(".rest" ~ info.accessor, info.tag);
|
||||
}
|
||||
}
|
||||
|
||||
struct Variant
|
||||
{
|
||||
/// Types can be present in this $(D_PSYMBOL Variant).
|
||||
alias Types = Specs;
|
||||
|
||||
private ptrdiff_t tag = -1;
|
||||
private AlignedUnion!Types values;
|
||||
|
||||
/**
|
||||
* Constructs this $(D_PSYMBOL Variant) with one of the types supported
|
||||
* in it.
|
||||
*
|
||||
* Params:
|
||||
* T = Type of the initial value.
|
||||
* value = Initial value.
|
||||
*/
|
||||
this(T)(ref T value)
|
||||
if (canFind!(T, Types))
|
||||
{
|
||||
copyAssign!T(value);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
this(T)(T value)
|
||||
if (canFind!(T, Types))
|
||||
{
|
||||
moveAssign!T(value);
|
||||
}
|
||||
|
||||
~this()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
this(this)
|
||||
{
|
||||
alias pred(U) = hasElaborateCopyConstructor!(U.Seq[1]);
|
||||
static foreach (Type; Filter!(pred, Enumerate!Types))
|
||||
{
|
||||
if (this.tag == Type.Seq[0])
|
||||
{
|
||||
get!(Type.Seq[1]).__postblit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether this $(D_PSYMBOL Variant) is initialized.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL Variant) contains a
|
||||
* value, $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool hasValue() const
|
||||
{
|
||||
return this.tag != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells whether this $(D_PSYMBOL Variant) holds currently a value of
|
||||
* type $(D_PARAM T).
|
||||
*
|
||||
* Params:
|
||||
* T = Examined type.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL Variant) currently
|
||||
* contains a value of type $(D_PARAM T), $(D_KEYWORD false)
|
||||
* otherwise.
|
||||
*/
|
||||
bool peek(T)() const
|
||||
if (canFind!(T, Types))
|
||||
{
|
||||
return this.tag == staticIndexOf!(T, Types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying value, assuming it is of the type $(D_PARAM T).
|
||||
*
|
||||
* Params:
|
||||
* T = Type of the value should be returned.
|
||||
*
|
||||
* Returns: The underyling value.
|
||||
*
|
||||
* Precondition: The $(D_PSYMBOL Variant) has a value.
|
||||
*
|
||||
* See_Also: $(D_PSYMBOL peek), $(D_PSYMBOL hasValue).
|
||||
*/
|
||||
ref inout(T) get(T)() inout
|
||||
if (canFind!(T, Types))
|
||||
in (this.tag == staticIndexOf!(T, Types), "Variant isn't initialized")
|
||||
{
|
||||
mixin("return " ~ accessor!(T, AlignedUnion!Types).accessor ~ ";");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reassigns the value.
|
||||
*
|
||||
* Params:
|
||||
* T = Type of the new value
|
||||
* that = New value.
|
||||
*
|
||||
* Returns: $(D_KEYWORD this).
|
||||
*/
|
||||
ref typeof(this) opAssign(T)(T that)
|
||||
if (canFind!(T, Types))
|
||||
{
|
||||
reset();
|
||||
return moveAssign!T(that);
|
||||
}
|
||||
|
||||
/// ditto
|
||||
ref typeof(this) opAssign(T)(ref T that)
|
||||
if (canFind!(T, Types))
|
||||
{
|
||||
reset();
|
||||
return copyAssign!T(that);
|
||||
}
|
||||
|
||||
private ref typeof(this) moveAssign(T)(ref T that) @trusted
|
||||
{
|
||||
this.tag = staticIndexOf!(T, Types);
|
||||
|
||||
enum string accessorMixin = accessor!(T, AlignedUnion!Types).accessor;
|
||||
moveEmplace(that, mixin(accessorMixin));
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private ref typeof(this) copyAssign(T)(ref T that) return
|
||||
{
|
||||
this.tag = staticIndexOf!(T, Types);
|
||||
|
||||
enum string accessorMixin = accessor!(T, AlignedUnion!Types).accessor;
|
||||
emplace!T((() @trusted => (&mixin(accessorMixin))[0 .. 1])(), that);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
private void reset()
|
||||
{
|
||||
alias pred(U) = hasElaborateDestructor!(U.Seq[1]);
|
||||
static foreach (Type; Filter!(pred, Enumerate!Types))
|
||||
{
|
||||
if (this.tag == Type.Seq[0])
|
||||
{
|
||||
destroy(get!(Type.Seq[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns $(D_PSYMBOL TypeInfo) corresponding to the current type.
|
||||
*
|
||||
* If this $(D_PSYMBOL Variant) isn't initialized, returns
|
||||
* $(D_KEYWORD null).
|
||||
*
|
||||
* Returns: $(D_PSYMBOL TypeInfo) of the current type.
|
||||
*/
|
||||
@property TypeInfo type()
|
||||
{
|
||||
static foreach (i, Type; Types)
|
||||
{
|
||||
if (this.tag == i)
|
||||
{
|
||||
return typeid(Type);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this $(D_PSYMBOL Variant) with another one with the same
|
||||
* specification for equality.
|
||||
*
|
||||
* $(UL
|
||||
* $(LI If both hold values of the same type, these values are
|
||||
* compared.)
|
||||
* $(LI If they hold values of different types, then the
|
||||
* $(D_PSYMBOL Variant)s aren't equal.)
|
||||
* $(LI If only one of them is initialized but another one not, they
|
||||
* aren't equal.)
|
||||
* $(LI If neither of them is initialized, they are equal.)
|
||||
* )
|
||||
*
|
||||
* Params:
|
||||
* that = The $(D_PSYMBOL Variant) to compare with.
|
||||
*
|
||||
* Returns: $(D_KEYWORD true) if this $(D_PSYMBOL Variant) is equal to
|
||||
* $(D_PARAM that), $(D_KEYWORD false) otherwise.
|
||||
*/
|
||||
bool opEquals()(auto ref inout Variant that) inout
|
||||
{
|
||||
if (this.tag != that.tag)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
static foreach (i, Type; Types)
|
||||
{
|
||||
if (this.tag == i)
|
||||
{
|
||||
return get!Type == that.get!Type;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
Variant!(int, double) variant = 5;
|
||||
assert(variant.peek!int);
|
||||
assert(variant.get!int == 5);
|
||||
|
||||
variant = 5.4;
|
||||
assert(!variant.peek!int);
|
||||
assert(variant.get!double == 5.4);
|
||||
}
|
13
sys/dub.json
13
sys/dub.json
@ -1,13 +0,0 @@
|
||||
{
|
||||
"name": "sys",
|
||||
"description": "Low-level operating system bindings and definitions",
|
||||
"targetType": "library",
|
||||
|
||||
"sourcePaths": [
|
||||
"."
|
||||
],
|
||||
"importPaths": [
|
||||
"."
|
||||
],
|
||||
"dflags-dmd": ["-dip1000"]
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.linux.syscall;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
extern ptrdiff_t syscall(ptrdiff_t, ptrdiff_t)
|
||||
@nogc nothrow @system;
|
||||
|
||||
extern ptrdiff_t syscall(ptrdiff_t, ptrdiff_t, ptrdiff_t)
|
||||
@nogc nothrow @system;
|
||||
|
||||
extern ptrdiff_t syscall(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t)
|
||||
@nogc nothrow @system;
|
||||
|
||||
extern ptrdiff_t syscall(ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t) @nogc nothrow @system;
|
||||
|
||||
// Same syscalls as above but pure.
|
||||
private template getOverloadMangling(size_t n)
|
||||
{
|
||||
enum string getOverloadMangling = __traits(getOverloads,
|
||||
tanya.sys.linux.syscall,
|
||||
"syscall")[n].mangleof;
|
||||
}
|
||||
|
||||
pragma(mangle, getOverloadMangling!0)
|
||||
extern ptrdiff_t syscall_(ptrdiff_t, ptrdiff_t)
|
||||
@nogc nothrow pure @system;
|
||||
|
||||
pragma(mangle, getOverloadMangling!1)
|
||||
extern ptrdiff_t syscall_(ptrdiff_t, ptrdiff_t, ptrdiff_t)
|
||||
@nogc nothrow pure @system;
|
||||
|
||||
pragma(mangle, getOverloadMangling!2)
|
||||
extern ptrdiff_t syscall_(ptrdiff_t, ptrdiff_t, ptrdiff_t, ptrdiff_t)
|
||||
@nogc nothrow pure @system;
|
||||
|
||||
pragma(mangle, getOverloadMangling!3)
|
||||
extern ptrdiff_t syscall_(ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t,
|
||||
ptrdiff_t) @nogc nothrow pure @system;
|
@ -1,76 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.posix.ioctl;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
enum
|
||||
{
|
||||
SIOCADDRT = 0x890B, // Add routing table entry.
|
||||
SIOCDELRT = 0x890C, // Delete routing table entry.
|
||||
SIOCRTMSG = 0x890D, // Call to routing system.
|
||||
|
||||
SIOCGIFNAME = 0x8910, // Get iface name.
|
||||
SIOCSIFLINK = 0x8911, // Set iface channel.
|
||||
SIOCGIFCONF = 0x8912, // Get iface list.
|
||||
SIOCGIFFLAGS = 0x8913, // Get flags.
|
||||
SIOCSIFFLAGS = 0x8914, // Set flags.
|
||||
SIOCGIFADDR = 0x8915, // Get PA address.
|
||||
SIOCSIFADDR = 0x8916, // Set PA address.
|
||||
SIOCGIFDSTADDR = 0x8917, // Get remote PA address.
|
||||
SIOCSIFDSTADDR = 0x8918, // Set remote PA address.
|
||||
SIOCGIFBRDADDR = 0x8919, // Get broadcast PA address.
|
||||
SIOCSIFBRDADDR = 0x891a, // Set broadcast PA address.
|
||||
SIOCGIFNETMASK = 0x891b, // Get network PA mask.
|
||||
SIOCSIFNETMASK = 0x891c, // Set network PA mask.
|
||||
SIOCGIFMETRIC = 0x891d, // Get metric.
|
||||
SIOCSIFMETRIC = 0x891e, // Set metric.
|
||||
SIOCGIFMEM = 0x891f, // Get memory address (BSD).
|
||||
SIOCSIFMEM = 0x8920, // Set memory address (BSD).
|
||||
SIOCGIFMTU = 0x8921, // Get MTU size.
|
||||
SIOCSIFMTU = 0x8922, // Set MTU size.
|
||||
SIOCSIFNAME = 0x8923, // Set interface name.
|
||||
SIOCSIFHWADDR = 0x8924, // Set hardware address.
|
||||
SIOCGIFENCAP = 0x8925, // Get/set encapsulations.
|
||||
SIOCSIFENCAP = 0x8926,
|
||||
SIOCGIFHWADDR = 0x8927, // Get hardware address.
|
||||
SIOCGIFSLAVE = 0x8929, // Driver slaving support.
|
||||
SIOCSIFSLAVE = 0x8930,
|
||||
SIOCADDMULTI = 0x8931, // Multicast address lists.
|
||||
SIOCDELMULTI = 0x8932,
|
||||
SIOCGIFINDEX = 0x8933, // Name -> if_index mapping.
|
||||
SIOGIFINDEX = SIOCGIFINDEX, // Misprint compatibility.
|
||||
SIOCSIFPFLAGS = 0x8934, // Set/get extended flags set.
|
||||
SIOCGIFPFLAGS = 0x8935,
|
||||
SIOCDIFADDR = 0x8936, // Delete PA address.
|
||||
SIOCSIFHWBROADCAST = 0x8937, // Set hardware broadcast address.
|
||||
SIOCGIFCOUNT = 0x8938, // Get number of devices.
|
||||
|
||||
SIOCGIFBR = 0x8940, // Bridging support.
|
||||
SIOCSIFBR = 0x8941, // Set bridging options.
|
||||
|
||||
SIOCGIFTXQLEN = 0x8942, // Get the tx queue length.
|
||||
SIOCSIFTXQLEN = 0x8943, // Set the tx queue length.
|
||||
|
||||
SIOCDARP = 0x8953, // Delete ARP table entry.
|
||||
SIOCGARP = 0x8954, // Get ARP table entry.
|
||||
SIOCSARP = 0x8955, // Set ARP table entry.
|
||||
|
||||
SIOCDRARP = 0x8960, // Delete RARP table entry.
|
||||
SIOCGRARP = 0x8961, // Get RARP table entry.
|
||||
SIOCSRARP = 0x8962, // Set RARP table entry.
|
||||
|
||||
SIOCGIFMAP = 0x8970, // Get device parameters.
|
||||
SIOCSIFMAP = 0x8971, // Set device parameters.
|
||||
|
||||
SIOCADDDLCI = 0x8980, // Create new DLCI device.
|
||||
SIOCDELDLCI = 0x8981, // Delete DLCI device.
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.posix.mman;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
enum
|
||||
{
|
||||
PROT_EXEC = 0x4, // Page can be executed.
|
||||
PROT_NONE = 0x0, // Page cannot be accessed.
|
||||
PROT_READ = 0x1, // Page can be read.
|
||||
PROT_WRITE = 0x2, // Page can be written.
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
MAP_FIXED = 0x10, // Interpret addr exactly.
|
||||
MAP_PRIVATE = 0x02, // Changes are private.
|
||||
MAP_SHARED = 0x01, // Share changes.
|
||||
MAP_ANONYMOUS = 0x20, // Don't use a file.
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.posix.net.if_;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
enum size_t IF_NAMESIZE = 16;
|
||||
|
||||
struct ifreq
|
||||
{
|
||||
char[IF_NAMESIZE] ifr_name;
|
||||
|
||||
union
|
||||
{
|
||||
int ifr_ifindex;
|
||||
}
|
||||
}
|
@ -1,150 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.posix.socket;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
/*
|
||||
* Protocol families.
|
||||
*/
|
||||
enum
|
||||
{
|
||||
PF_UNSPEC = 0, // Unspecified.
|
||||
PF_LOCAL = 1, // Local to host (pipes and file-domain).
|
||||
PF_UNIX = PF_LOCAL, // POSIX name for PF_LOCAL.
|
||||
PF_FILE = PF_LOCAL, // Another non-standard name for PF_LOCAL.
|
||||
PF_INET = 2, // IP protocol family.
|
||||
PF_AX25 = 3, // Amateur Radio AX.25.
|
||||
PF_IPX = 4, // Novell Internet Protocol.
|
||||
PF_APPLETALK = 5, // Appletalk DDP.
|
||||
PF_NETROM = 6, // Amateur radio NetROM.
|
||||
PF_BRIDGE = 7, // Multiprotocol bridge.
|
||||
PF_ATMPVC = 8, // ATM PVCs.
|
||||
PF_X25 = 9, // Reserved for X.25 project.
|
||||
PF_INET6 = 10, // IP version 6.
|
||||
PF_ROSE = 11, // Amateur Radio X.25 PLP.
|
||||
PF_DECnet = 12, // Reserved for DECnet project.
|
||||
PF_NETBEUI = 13, // Reserved for 802.2LLC project.
|
||||
PF_SECURITY = 14, // Security callback pseudo AF.
|
||||
PF_KEY = 15, // PF_KEY key management API.
|
||||
PF_NETLINK = 16, // Kernel user interface device.
|
||||
PF_ROUTE = PF_NETLINK, // Alias to emulate 4.4BSD.
|
||||
PF_PACKET = 17, // Packet family.
|
||||
PF_ASH = 18, // Ash.
|
||||
PF_ECONET = 19, // Acorn Econet.
|
||||
PF_ATMSVC = 20, // ATM SVCs.
|
||||
PF_RDS = 21, // RDS sockets.
|
||||
PF_SNA = 22, // Linux SNA Project.
|
||||
PF_IRDA = 23, // IRDA sockets.
|
||||
PF_PPPOX = 24, // PPPoX sockets.
|
||||
PF_WANPIPE = 25, // Wanpipe API sockets.
|
||||
PF_LLC = 26, // Linux LLC.
|
||||
PF_IB = 27, // Native InfiniBand address.
|
||||
PF_MPLS = 28, // MPLS.
|
||||
PF_CAN = 29, // Controller Area Network.
|
||||
PF_TIPC = 30, // TIPC sockets.
|
||||
PF_BLUETOOTH = 31, // Bluetooth sockets.
|
||||
PF_IUCV = 32, // IUCV sockets.
|
||||
PF_RXRPC = 33, // RxRPC sockets.
|
||||
PF_ISDN = 34, // mISDN sockets.
|
||||
PF_PHONET = 35, // Phonet sockets.
|
||||
PF_IEEE802154 = 36, // IEEE 802.15.4 sockets.
|
||||
PF_CAIF = 37, // CAIF sockets.
|
||||
PF_ALG = 38, // Algorithm sockets.
|
||||
PF_NFC = 39, // NFC sockets.
|
||||
PF_VSOCK = 40, // vSockets.
|
||||
PF_MAX = 41, // For now.
|
||||
}
|
||||
|
||||
/*
|
||||
* Address families.
|
||||
*/
|
||||
enum
|
||||
{
|
||||
AF_UNSPEC = PF_UNSPEC,
|
||||
AF_LOCAL = PF_LOCAL,
|
||||
AF_UNIX = PF_UNIX,
|
||||
AF_FILE = PF_FILE,
|
||||
AF_INET = PF_INET,
|
||||
AF_AX25 = PF_AX25,
|
||||
AF_IPX = PF_IPX,
|
||||
AF_APPLETALK = PF_APPLETALK,
|
||||
AF_NETROM = PF_NETROM,
|
||||
AF_BRIDGE = PF_BRIDGE,
|
||||
AF_ATMPVC = PF_ATMPVC,
|
||||
AF_X25 = PF_X25,
|
||||
AF_INET6 = PF_INET6,
|
||||
AF_ROSE = PF_ROSE,
|
||||
AF_DECnet = PF_DECnet,
|
||||
AF_NETBEUI = PF_NETBEUI,
|
||||
AF_SECURITY = PF_SECURITY,
|
||||
AF_KEY = PF_KEY,
|
||||
AF_NETLINK = PF_NETLINK,
|
||||
AF_ROUTE = PF_ROUTE,
|
||||
AF_PACKET = PF_PACKET,
|
||||
AF_ASH = PF_ASH,
|
||||
AF_ECONET = PF_ECONET,
|
||||
AF_ATMSVC = PF_ATMSVC,
|
||||
AF_RDS = PF_RDS,
|
||||
AF_SNA = PF_SNA,
|
||||
AF_IRDA = PF_IRDA,
|
||||
AF_PPPOX = PF_PPPOX,
|
||||
AF_WANPIPE = PF_WANPIPE,
|
||||
AF_LLC = PF_LLC,
|
||||
AF_IB = PF_IB,
|
||||
AF_MPLS = PF_MPLS,
|
||||
AF_CAN = PF_CAN,
|
||||
AF_TIPC = PF_TIPC,
|
||||
AF_BLUETOOTH = PF_BLUETOOTH,
|
||||
AF_IUCV = PF_IUCV,
|
||||
AF_RXRPC = PF_RXRPC,
|
||||
AF_ISDN = PF_ISDN,
|
||||
AF_PHONET = PF_PHONET,
|
||||
AF_IEEE802154 = PF_IEEE802154,
|
||||
AF_CAIF = PF_CAIF,
|
||||
AF_ALG = PF_ALG,
|
||||
AF_NFC = PF_NFC,
|
||||
AF_VSOCK = PF_VSOCK,
|
||||
AF_MAX = PF_MAX,
|
||||
}
|
||||
|
||||
/*
|
||||
* Types of sockets.
|
||||
*/
|
||||
enum
|
||||
{
|
||||
// Sequenced, reliable, connection-based byte streams.
|
||||
SOCK_STREAM = 1,
|
||||
// Connectionless, unreliable datagrams of fixed maximum length.
|
||||
SOCK_DGRAM = 2,
|
||||
// Raw protocol interface.
|
||||
SOCK_RAW = 3,
|
||||
// Reliably-delivered messages.
|
||||
SOCK_RDM = 4,
|
||||
// Sequenced, reliable, connection-based, datagrams of fixed maximum
|
||||
// length.
|
||||
SOCK_SEQPACKET = 5,
|
||||
// Datagram Congestion Control Protocol.
|
||||
SOCK_DCCP = 6,
|
||||
// Linux specific way of getting packets at the dev level. For writing rarp
|
||||
// and other similar things on the user level.
|
||||
SOCK_PACKET = 10,
|
||||
}
|
||||
|
||||
/*
|
||||
* Flags to be ORed into the type parameter of socket and socketpair and used
|
||||
* for the flags parameter of paccept.
|
||||
*/
|
||||
enum
|
||||
{
|
||||
SOCK_CLOEXEC = 0x80000, // Atomically set close-on-exec flag for the new descriptor(s).
|
||||
SOCK_NONBLOCK = 0x800, // Atomically mark descriptor(s) as non-blocking.
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Base type definitions and aliases.
|
||||
*
|
||||
* This module doesn't provide aliases for all types used by Windows, but only
|
||||
* for types that can vary on different platforms. For example there is no
|
||||
* need to define `INT32` alias for D, since $(D_KEYWORD int) is always a
|
||||
* 32-bit signed integer. But `int` and its Windows alias `INT` is not the
|
||||
* same on all platforms in C, so its size can be something differen than
|
||||
* 32 bit, therefore an $(D_PSYMBOL INT) alias is available in this module.
|
||||
* $(D_PARAM TCHAR) can be a $(D_KEYWORD char) if Unicode isn't supported or
|
||||
* $(D_KEYWORD wchar) if Unicode is supported, so $(D_PSYMBOL TCHAR) is
|
||||
* defined here.
|
||||
* Also aliases for specific types like $(D_PSYMBOL SOCKET) are defined here.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.windows.def;
|
||||
|
||||
version (Windows):
|
||||
|
||||
alias BYTE = ubyte;
|
||||
alias TBYTE = wchar; // If Unicode, otherwise char.
|
||||
alias CHAR = char; // Signed or unsigned char.
|
||||
alias WCHAR = wchar;
|
||||
alias TCHAR = wchar; // If Unicode, otherwise char.
|
||||
alias SHORT = short;
|
||||
alias USHORT = ushort;
|
||||
alias WORD = ushort;
|
||||
alias INT = int;
|
||||
alias UINT = uint;
|
||||
alias LONG = int;
|
||||
alias ULONG = uint;
|
||||
alias DWORD = uint;
|
||||
alias LONGLONG = long; // Or double.
|
||||
alias ULONGLONG = ulong; // Or double.
|
||||
alias DWORDLONG = ulong;
|
||||
alias FLOAT = float;
|
||||
alias BOOL = int;
|
||||
alias BOOLEAN = BYTE;
|
||||
|
||||
alias HANDLE = void*;
|
||||
enum HANDLE INVALID_HANDLE_VALUE = cast(HANDLE) -1;
|
||||
|
||||
enum TRUE = 1;
|
||||
enum FALSE = 0;
|
||||
|
||||
alias PSTR = CHAR*;
|
||||
alias PWSTR = WCHAR*;
|
||||
alias PTSTR = TCHAR*;
|
||||
|
||||
align(1) struct GUID
|
||||
{
|
||||
uint Data1;
|
||||
ushort Data2;
|
||||
ushort Data3;
|
||||
char[8] Data4;
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.windows.ifdef;
|
||||
|
||||
version (Windows):
|
||||
|
||||
import tanya.sys.windows.def;
|
||||
|
||||
union NET_LUID_LH
|
||||
{
|
||||
ulong Value;
|
||||
ulong Info;
|
||||
}
|
||||
|
||||
alias NET_LUID = NET_LUID_LH;
|
||||
alias IF_LUID = NET_LUID_LH;
|
||||
|
||||
alias NET_IFINDEX = ULONG;
|
||||
|
||||
enum size_t IF_MAX_STRING_SIZE = 256;
|
@ -1,37 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.windows.iphlpapi;
|
||||
|
||||
version (Windows):
|
||||
|
||||
import tanya.sys.windows.def;
|
||||
import tanya.sys.windows.ifdef;
|
||||
|
||||
extern(Windows)
|
||||
DWORD ConvertInterfaceNameToLuidA(const(CHAR)* InterfaceName,
|
||||
NET_LUID* InterfaceLuid)
|
||||
@nogc nothrow @system;
|
||||
|
||||
extern(Windows)
|
||||
DWORD ConvertInterfaceLuidToIndex(const(NET_LUID)* InterfaceLuid,
|
||||
NET_IFINDEX* InterfaceIndex)
|
||||
@nogc nothrow @system;
|
||||
|
||||
extern(Windows)
|
||||
DWORD ConvertInterfaceIndexToLuid(NET_IFINDEX InterfaceIndex,
|
||||
NET_LUID* InterfaceLuid)
|
||||
@nogc nothrow @system;
|
||||
|
||||
extern(Windows)
|
||||
DWORD ConvertInterfaceLuidToNameA(const(NET_LUID)* InterfaceLuid,
|
||||
PSTR InterfaceName,
|
||||
size_t Length)
|
||||
@nogc nothrow @system;
|
@ -1,19 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.windows;
|
||||
|
||||
version (Windows):
|
||||
|
||||
public import tanya.sys.windows.def;
|
||||
public import tanya.sys.windows.ifdef;
|
||||
public import tanya.sys.windows.iphlpapi;
|
||||
public import tanya.sys.windows.winbase;
|
||||
public import tanya.sys.windows.winsock2;
|
@ -1,53 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Definitions from winbase.h.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.windows.winbase;
|
||||
|
||||
version (Windows):
|
||||
|
||||
public import tanya.sys.windows.def;
|
||||
|
||||
struct OVERLAPPED
|
||||
{
|
||||
size_t Internal;
|
||||
size_t InternalHigh;
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
DWORD Offset;
|
||||
DWORD OffsetHigh;
|
||||
}
|
||||
void* Pointer;
|
||||
}
|
||||
HANDLE hEvent;
|
||||
}
|
||||
|
||||
extern(Windows)
|
||||
HANDLE CreateIoCompletionPort(HANDLE FileHandle,
|
||||
HANDLE ExistingCompletionPort,
|
||||
size_t CompletionKey,
|
||||
DWORD NumberOfConcurrentThreads)
|
||||
nothrow @system @nogc;
|
||||
|
||||
extern(Windows)
|
||||
BOOL GetQueuedCompletionStatus(HANDLE CompletionPort,
|
||||
DWORD* lpNumberOfBytes,
|
||||
size_t* lpCompletionKey,
|
||||
OVERLAPPED** lpOverlapped,
|
||||
DWORD dwMilliseconds) nothrow @system @nogc;
|
||||
|
||||
extern(Windows)
|
||||
BOOL GetOverlappedResult(HANDLE hFile,
|
||||
OVERLAPPED* lpOverlapped,
|
||||
DWORD* lpNumberOfBytesTransferred,
|
||||
BOOL bWait) nothrow @system @nogc;
|
@ -1,217 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/**
|
||||
* Definitions from winsock2.h, ws2def.h and MSWSock.h.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
*/
|
||||
module tanya.sys.windows.winsock2;
|
||||
|
||||
version (Windows):
|
||||
|
||||
public import tanya.sys.windows.def;
|
||||
public import tanya.sys.windows.winbase;
|
||||
|
||||
alias SOCKET = size_t;
|
||||
enum SOCKET INVALID_SOCKET = ~0;
|
||||
enum SOCKET_ERROR = -1;
|
||||
|
||||
enum
|
||||
{
|
||||
IOC_UNIX = 0x00000000,
|
||||
IOC_WS2 = 0x08000000,
|
||||
IOC_PROTOCOL = 0x10000000,
|
||||
IOC_VOID = 0x20000000, // No parameters.
|
||||
IOC_OUT = 0x40000000, // Copy parameters back.
|
||||
IOC_IN = 0x80000000, // Copy parameters into.
|
||||
IOC_VENDOR = 0x18000000,
|
||||
IOC_WSK = (IOC_WS2 | 0x07000000), // _WIN32_WINNT >= 0x0600.
|
||||
IOC_INOUT = (IOC_IN | IOC_OUT), // Copy parameter into and get back.
|
||||
}
|
||||
|
||||
template _WSAIO(int x, int y)
|
||||
{
|
||||
enum _WSAIO = IOC_VOID | x | y;
|
||||
}
|
||||
template _WSAIOR(int x, int y)
|
||||
{
|
||||
enum _WSAIOR = IOC_OUT | x | y;
|
||||
}
|
||||
template _WSAIOW(int x, int y)
|
||||
{
|
||||
enum _WSAIOW = IOC_IN | x | y;
|
||||
}
|
||||
template _WSAIORW(int x, int y)
|
||||
{
|
||||
enum _WSAIORW = IOC_INOUT | x | y;
|
||||
}
|
||||
|
||||
alias SIO_ASSOCIATE_HANDLE = _WSAIOW!(IOC_WS2, 1);
|
||||
alias SIO_ENABLE_CIRCULAR_QUEUEING = _WSAIO!(IOC_WS2, 2);
|
||||
alias SIO_FIND_ROUTE = _WSAIOR!(IOC_WS2, 3);
|
||||
alias SIO_FLUSH = _WSAIO!(IOC_WS2, 4);
|
||||
alias SIO_GET_BROADCAST_ADDRESS = _WSAIOR!(IOC_WS2, 5);
|
||||
alias SIO_GET_EXTENSION_FUNCTION_POINTER = _WSAIORW!(IOC_WS2, 6);
|
||||
alias SIO_GET_QOS = _WSAIORW!(IOC_WS2, 7);
|
||||
alias SIO_GET_GROUP_QOS = _WSAIORW!(IOC_WS2, 8);
|
||||
alias SIO_MULTIPOINT_LOOPBACK = _WSAIOW!(IOC_WS2, 9);
|
||||
alias SIO_MULTICAST_SCOPE = _WSAIOW!(IOC_WS2, 10);
|
||||
alias SIO_SET_QOS = _WSAIOW!(IOC_WS2, 11);
|
||||
alias SIO_SET_GROUP_QOS = _WSAIOW!(IOC_WS2, 12);
|
||||
alias SIO_TRANSLATE_HANDLE = _WSAIORW!(IOC_WS2, 13);
|
||||
alias SIO_ROUTING_INTERFACE_QUERY = _WSAIORW!(IOC_WS2, 20);
|
||||
alias SIO_ROUTING_INTERFACE_CHANGE = _WSAIOW!(IOC_WS2, 21);
|
||||
alias SIO_ADDRESS_LIST_QUERY = _WSAIOR!(IOC_WS2, 22);
|
||||
alias SIO_ADDRESS_LIST_CHANGE = _WSAIO!(IOC_WS2, 23);
|
||||
alias SIO_QUERY_TARGET_PNP_HANDLE = _WSAIOR!(IOC_WS2, 24);
|
||||
alias SIO_NSP_NOTIFY_CHANGE = _WSAIOW!(IOC_WS2, 25);
|
||||
|
||||
alias GROUP = uint;
|
||||
|
||||
enum
|
||||
{
|
||||
WSA_FLAG_OVERLAPPED = 0x01,
|
||||
WSA_FLAG_MULTIPOINT_C_ROOT = 0x02,
|
||||
WSA_FLAG_MULTIPOINT_C_LEAF = 0x04,
|
||||
WSA_FLAG_MULTIPOINT_D_ROOT = 0x08,
|
||||
WSA_FLAG_MULTIPOINT_D_LEAF = 0x10,
|
||||
WSA_FLAG_ACCESS_SYSTEM_SECURITY = 0x40,
|
||||
WSA_FLAG_NO_HANDLE_INHERIT = 0x80,
|
||||
WSA_FLAG_REGISTERED_IO = 0x100,
|
||||
}
|
||||
|
||||
enum MAX_PROTOCOL_CHAIN = 7;
|
||||
enum BASE_PROTOCOL = 1;
|
||||
enum LAYERED_PROTOCOL = 0;
|
||||
enum WSAPROTOCOL_LEN = 255;
|
||||
|
||||
struct WSAPROTOCOLCHAIN
|
||||
{
|
||||
int ChainLen;
|
||||
DWORD[MAX_PROTOCOL_CHAIN] ChainEntries;
|
||||
}
|
||||
|
||||
struct WSABUF
|
||||
{
|
||||
ULONG len;
|
||||
CHAR* buf;
|
||||
}
|
||||
|
||||
struct WSAPROTOCOL_INFO
|
||||
{
|
||||
DWORD dwServiceFlags1;
|
||||
DWORD dwServiceFlags2;
|
||||
DWORD dwServiceFlags3;
|
||||
DWORD dwServiceFlags4;
|
||||
DWORD dwProviderFlags;
|
||||
GUID ProviderId;
|
||||
DWORD dwCatalogEntryId;
|
||||
WSAPROTOCOLCHAIN ProtocolChain;
|
||||
int iVersion;
|
||||
int iAddressFamily;
|
||||
int iMaxSockAddr;
|
||||
int iMinSockAddr;
|
||||
int iSocketType;
|
||||
int iProtocol;
|
||||
int iProtocolMaxOffset;
|
||||
int iNetworkByteOrder;
|
||||
int iSecurityScheme;
|
||||
DWORD dwMessageSize;
|
||||
DWORD dwProviderReserved;
|
||||
TCHAR[WSAPROTOCOL_LEN + 1] szProtocol;
|
||||
}
|
||||
|
||||
const GUID WSAID_GETACCEPTEXSOCKADDRS = {
|
||||
0xb5367df2, 0xcbac, 0x11cf,
|
||||
[0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92],
|
||||
};
|
||||
|
||||
const GUID WSAID_ACCEPTEX = {
|
||||
0xb5367df1, 0xcbac, 0x11cf,
|
||||
[0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92],
|
||||
};
|
||||
|
||||
alias LPWSAOVERLAPPED_COMPLETION_ROUTINE = void function(DWORD dwError,
|
||||
DWORD cbTransferred,
|
||||
OVERLAPPED* lpOverlapped,
|
||||
DWORD dwFlags) nothrow @nogc;
|
||||
|
||||
extern(Windows)
|
||||
SOCKET WSASocket(int af,
|
||||
int type,
|
||||
int protocol,
|
||||
WSAPROTOCOL_INFO* lpProtocolInfo,
|
||||
GROUP g,
|
||||
DWORD dwFlags) nothrow @system @nogc;
|
||||
|
||||
extern(Windows)
|
||||
int WSARecv(SOCKET s,
|
||||
WSABUF* lpBuffers,
|
||||
DWORD dwBufferCount,
|
||||
DWORD* lpNumberOfBytesRecvd,
|
||||
DWORD* lpFlags,
|
||||
OVERLAPPED* lpOverlapped,
|
||||
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
||||
nothrow @system @nogc;
|
||||
|
||||
extern(Windows)
|
||||
int WSASend(SOCKET s,
|
||||
WSABUF* lpBuffers,
|
||||
DWORD dwBufferCount,
|
||||
DWORD* lpNumberOfBytesRecvd,
|
||||
DWORD lpFlags,
|
||||
OVERLAPPED* lpOverlapped,
|
||||
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
||||
nothrow @system @nogc;
|
||||
|
||||
extern(Windows)
|
||||
int WSAIoctl(SOCKET s,
|
||||
uint dwIoControlCode,
|
||||
void* lpvInBuffer,
|
||||
uint cbInBuffer,
|
||||
void* lpvOutBuffer,
|
||||
uint cbOutBuffer,
|
||||
uint* lpcbBytesReturned,
|
||||
OVERLAPPED* lpOverlapped,
|
||||
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
||||
nothrow @system @nogc;
|
||||
|
||||
alias ADDRESS_FAMILY = USHORT;
|
||||
|
||||
struct SOCKADDR
|
||||
{
|
||||
ADDRESS_FAMILY sa_family; // Address family.
|
||||
CHAR[14] sa_data; // Up to 14 bytes of direct address.
|
||||
}
|
||||
|
||||
alias LPFN_GETACCEPTEXSOCKADDRS = void function(void*,
|
||||
DWORD,
|
||||
DWORD,
|
||||
DWORD,
|
||||
SOCKADDR**,
|
||||
INT*,
|
||||
SOCKADDR**,
|
||||
INT*) nothrow @nogc;
|
||||
|
||||
alias LPFN_ACCEPTEX = extern(Windows) BOOL function(SOCKET,
|
||||
SOCKET,
|
||||
void*,
|
||||
DWORD,
|
||||
DWORD,
|
||||
DWORD,
|
||||
DWORD*,
|
||||
OVERLAPPED*) @nogc nothrow;
|
||||
|
||||
enum
|
||||
{
|
||||
SO_MAXDG = 0x7009,
|
||||
SO_MAXPATHDG = 0x700A,
|
||||
SO_UPDATE_ACCEPT_CONTEXT = 0x700B,
|
||||
SO_CONNECT_TIME = 0x700C,
|
||||
SO_UPDATE_CONNECT_CONTEXT = 0x7010,
|
||||
}
|
@ -13,7 +13,7 @@
|
||||
* The functions can cause segmentation fault if the module is compiled
|
||||
* in production mode and the condition fails.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Test suite for $(D_KEYWORD unittest)-blocks.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2017-2019.
|
||||
* Copyright: Eugene Wissner 2017-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
|
@ -5,7 +5,7 @@
|
||||
/**
|
||||
* Range and generic type generators.
|
||||
*
|
||||
* Copyright: Eugene Wissner 2018-2019.
|
||||
* Copyright: Eugene Wissner 2018-2020.
|
||||
* License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
|
||||
* Mozilla Public License, v. 2.0).
|
||||
* Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
|
||||
@ -146,7 +146,11 @@ mixin template InputRangeStub(E = int)
|
||||
}
|
||||
|
||||
void popFront() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
static if (!infinite)
|
||||
{
|
||||
@ -157,7 +161,11 @@ mixin template InputRangeStub(E = int)
|
||||
static if (withLvalueElements)
|
||||
{
|
||||
ref E front() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.element;
|
||||
}
|
||||
@ -165,7 +173,11 @@ mixin template InputRangeStub(E = int)
|
||||
else
|
||||
{
|
||||
E front() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return E.init;
|
||||
}
|
||||
@ -216,7 +228,11 @@ mixin template BidirectionalRangeStub(E = int)
|
||||
mixin ForwardRangeStub!E;
|
||||
|
||||
void popBack() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
static if (!infinite)
|
||||
{
|
||||
@ -227,7 +243,11 @@ mixin template BidirectionalRangeStub(E = int)
|
||||
static if (withLvalueElements)
|
||||
{
|
||||
ref E back() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return *this.element;
|
||||
}
|
||||
@ -235,7 +255,11 @@ mixin template BidirectionalRangeStub(E = int)
|
||||
else
|
||||
{
|
||||
E back() @nogc nothrow pure @safe
|
||||
in (!empty)
|
||||
in
|
||||
{
|
||||
assert(!empty);
|
||||
}
|
||||
do
|
||||
{
|
||||
return E.init;
|
||||
}
|
||||
|
@ -1,97 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.algorithm.tests.comparison;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import tanya.math;
|
||||
import tanya.range;
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(!is(typeof(min(1, 1UL))));
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(min(5, 3) == 3);
|
||||
assert(min(4, 4) == 4);
|
||||
assert(min(5.2, 3.0) == 3.0);
|
||||
|
||||
assert(min(5.2, double.nan) == 5.2);
|
||||
assert(min(double.nan, 3.0) == 3.0);
|
||||
assert(isNaN(min(double.nan, double.nan)));
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(min(cast(ubyte[]) []).empty);
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(!is(typeof(max(1, 1UL))));
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(max(5, 3) == 5);
|
||||
assert(max(4, 4) == 4);
|
||||
assert(max(5.2, 3.0) == 5.2);
|
||||
|
||||
assert(max(5.2, double.nan) == 5.2);
|
||||
assert(max(double.nan, 3.0) == 3.0);
|
||||
assert(isNaN(max(double.nan, double.nan)));
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
assert(max(cast(ubyte[]) []).empty);
|
||||
}
|
||||
|
||||
// min/max compare const and mutable structs.
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct S
|
||||
{
|
||||
int s;
|
||||
|
||||
int opCmp(typeof(this) that) const @nogc nothrow pure @safe
|
||||
{
|
||||
return this.s - that.s;
|
||||
}
|
||||
}
|
||||
{
|
||||
const s1 = S(1);
|
||||
assert(min(s1, S(2)).s == 1);
|
||||
assert(max(s1, S(2)).s == 2);
|
||||
}
|
||||
{
|
||||
auto s2 = S(2), s3 = S(3);
|
||||
assert(min(s2, s3).s == 2);
|
||||
assert(max(s2, s3).s == 3);
|
||||
}
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct OpCmp(int value)
|
||||
{
|
||||
int opCmp(OpCmp) @nogc nothrow pure @safe
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
{
|
||||
OpCmp!(-1)[1] range;
|
||||
assert(compare(range[], range[]) < 0);
|
||||
}
|
||||
{
|
||||
OpCmp!1[1] range;
|
||||
assert(compare(range[], range[]) > 0);
|
||||
}
|
||||
{
|
||||
OpCmp!0[1] range;
|
||||
assert(compare(range[], range[]) == 0);
|
||||
}
|
||||
}
|
@ -1,85 +1,13 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
module tanya.algorithm.tests.iteration;
|
||||
|
||||
import tanya.algorithm.iteration;
|
||||
import tanya.range;
|
||||
import tanya.test.stub;
|
||||
|
||||
// length is unknown when taking from a range without length
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static struct R
|
||||
{
|
||||
mixin InputRangeStub;
|
||||
}
|
||||
auto actual = take(R(), 100);
|
||||
|
||||
static assert(!hasLength!(typeof(actual)));
|
||||
}
|
||||
|
||||
// Takes minimum length if the range length > n
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
auto range = take(cast(int[]) null, 8);
|
||||
assert(range.length == 0);
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
const int[9] range = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
{
|
||||
auto slice = take(range[], 8)[1 .. 3];
|
||||
|
||||
assert(slice.length == 2);
|
||||
assert(slice.front == 2);
|
||||
assert(slice.back == 3);
|
||||
}
|
||||
{
|
||||
auto slice = takeExactly(range[], 8)[1 .. 3];
|
||||
|
||||
assert(slice.length == 2);
|
||||
assert(slice.front == 2);
|
||||
assert(slice.back == 3);
|
||||
}
|
||||
}
|
||||
|
||||
// Elements are accessible in reverse order
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
const int[3] given = [1, 2, 3];
|
||||
auto actual = retro(given[]);
|
||||
|
||||
assert(actual.back == given[].front);
|
||||
assert(actual[0] == 3);
|
||||
assert(actual[2] == 1);
|
||||
|
||||
actual.popBack();
|
||||
assert(actual.back == 2);
|
||||
assert(actual[1] == 2);
|
||||
|
||||
// Check slicing.
|
||||
auto slice = retro(given[])[1 .. $];
|
||||
assert(slice.length == 2 && slice.front == 2 && slice.back == 1);
|
||||
}
|
||||
|
||||
// Elements can be assigned
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
int[4] given = [1, 2, 3, 4];
|
||||
auto actual = retro(given[]);
|
||||
|
||||
actual.front = 5;
|
||||
assert(given[].back == 5);
|
||||
|
||||
actual.back = 8;
|
||||
assert(given[].front == 8);
|
||||
|
||||
actual[2] = 10;
|
||||
assert(given[1] == 10);
|
||||
}
|
||||
|
||||
// Singleton range is bidirectional and random-access
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ import tanya.test.stub;
|
||||
// Copies overlapping arrays
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
import std.algorithm.comparison : equal;
|
||||
|
||||
int[6] actual = [1, 2, 3, 4, 5, 6];
|
||||
const int[6] expected = [1, 2, 1, 2, 3, 4];
|
||||
@ -39,7 +39,11 @@ import tanya.test.stub;
|
||||
int value;
|
||||
|
||||
void opCall(int value) @nogc nothrow pure @safe
|
||||
in (this.value == 0)
|
||||
in
|
||||
{
|
||||
assert(this.value == 0);
|
||||
}
|
||||
do
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
@ -91,38 +95,3 @@ import tanya.test.stub;
|
||||
NonCopyable[] nonCopyable;
|
||||
initializeAll(nonCopyable);
|
||||
}
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
|
||||
const int[5] expected = [1, 2, 3, 4, 5];
|
||||
int[5] actual = [4, 5, 1, 2, 3];
|
||||
|
||||
rotate(actual[0 .. 2], actual[2 .. $]);
|
||||
assert(equal(actual[], expected[]));
|
||||
}
|
||||
|
||||
// Doesn't cause an infinite loop if back is shorter than the front
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
|
||||
const int[5] expected = [1, 2, 3, 4, 5];
|
||||
int[5] actual = [3, 4, 5, 1, 2];
|
||||
|
||||
rotate(actual[0 .. 3], actual[3 .. $]);
|
||||
assert(equal(actual[], expected[]));
|
||||
}
|
||||
|
||||
// Doesn't call .front on an empty front
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
import tanya.algorithm.comparison : equal;
|
||||
|
||||
const int[2] expected = [2, 8];
|
||||
int[2] actual = expected;
|
||||
|
||||
rotate(actual[0 .. 0], actual[]);
|
||||
assert(equal(actual[], expected[]));
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.algorithm.tests.searching;
|
||||
|
||||
import tanya.algorithm.searching;
|
||||
import tanya.test.stub;
|
||||
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
@Count(3)
|
||||
static struct Range
|
||||
{
|
||||
mixin InputRangeStub!int;
|
||||
}
|
||||
assert(count(Range()) == 3);
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.async.tests.loop;
|
||||
|
||||
import core.time;
|
||||
import tanya.async.loop;
|
||||
import tanya.async.watcher;
|
||||
import tanya.memory.allocator;
|
||||
|
||||
private final class DummyWatcher : Watcher
|
||||
{
|
||||
bool invoked;
|
||||
|
||||
override void invoke() @nogc
|
||||
{
|
||||
this.invoked = true;
|
||||
}
|
||||
}
|
||||
|
||||
private final class TestLoop : Loop
|
||||
{
|
||||
override protected bool reify(SocketWatcher watcher,
|
||||
EventMask oldEvents,
|
||||
EventMask events) @nogc
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
override protected void poll() @nogc
|
||||
{
|
||||
assert(!this.done);
|
||||
unloop();
|
||||
}
|
||||
|
||||
override protected @property uint maxEvents()
|
||||
const pure nothrow @safe @nogc
|
||||
{
|
||||
return 64U;
|
||||
}
|
||||
|
||||
@nogc @system unittest
|
||||
{
|
||||
auto loop = defaultAllocator.make!TestLoop;
|
||||
assert(loop.blockTime == 1.dur!"minutes");
|
||||
|
||||
loop.blockTime = 2.dur!"minutes";
|
||||
assert(loop.blockTime == 2.dur!"minutes");
|
||||
|
||||
defaultAllocator.dispose(loop);
|
||||
}
|
||||
|
||||
@nogc @system unittest
|
||||
{
|
||||
auto loop = defaultAllocator.make!TestLoop;
|
||||
assert(loop.done);
|
||||
|
||||
loop.run();
|
||||
assert(loop.done);
|
||||
|
||||
defaultAllocator.dispose(loop);
|
||||
}
|
||||
|
||||
@nogc @system unittest
|
||||
{
|
||||
auto loop = defaultAllocator.make!TestLoop;
|
||||
auto watcher = defaultAllocator.make!DummyWatcher;
|
||||
loop.pendings.insertBack(watcher);
|
||||
|
||||
assert(!watcher.invoked);
|
||||
loop.run();
|
||||
assert(watcher.invoked);
|
||||
|
||||
defaultAllocator.dispose(loop);
|
||||
defaultAllocator.dispose(watcher);
|
||||
}
|
||||
}
|
||||
|
||||
@nogc @system unittest
|
||||
{
|
||||
auto loop = defaultAllocator.make!TestLoop;
|
||||
assert(loop.maxEvents == 64);
|
||||
|
||||
defaultAllocator.dispose(loop);
|
||||
}
|
||||
|
||||
@nogc @system unittest
|
||||
{
|
||||
auto oldLoop = defaultLoop;
|
||||
auto loop = defaultAllocator.make!TestLoop;
|
||||
|
||||
defaultLoop = loop;
|
||||
assert(defaultLoop is loop);
|
||||
|
||||
defaultLoop = oldLoop;
|
||||
defaultAllocator.dispose(loop);
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.container.tests.array;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.algorithm.comparison;
|
||||
import tanya.container.array;
|
||||
import tanya.memory.allocator;
|
||||
import tanya.test.stub;
|
||||
@ -187,10 +187,3 @@ import tanya.test.stub;
|
||||
}
|
||||
func(array);
|
||||
}
|
||||
|
||||
// Can have non-copyable elements
|
||||
@nogc nothrow pure @safe unittest
|
||||
{
|
||||
static assert(is(Array!NonCopyable));
|
||||
static assert(is(typeof({ Array!NonCopyable.init[0] = NonCopyable(); })));
|
||||
}
|
||||
|
@ -3,11 +3,6 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.memory.tests.mallocator;
|
||||
|
||||
version (TanyaNative)
|
||||
{
|
||||
}
|
||||
else:
|
||||
|
||||
import tanya.memory.mallocator;
|
||||
|
||||
// Fails with false
|
||||
|
@ -3,8 +3,6 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.memory.tests.mmappool;
|
||||
|
||||
version (TanyaNative):
|
||||
|
||||
import tanya.memory.mmappool;
|
||||
|
||||
@nogc nothrow pure @system unittest
|
||||
|
@ -3,14 +3,15 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
module tanya.net.tests.iface;
|
||||
|
||||
import tanya.algorithm.comparison;
|
||||
import std.algorithm.comparison;
|
||||
import std.utf;
|
||||
import tanya.net.iface;
|
||||
|
||||
@nogc nothrow @safe unittest
|
||||
{
|
||||
version (linux)
|
||||
{
|
||||
assert(equal(indexToName(1)[], "lo"));
|
||||
assert(equal(indexToName(1)[], "lo".byChar));
|
||||
}
|
||||
else version (Windows)
|
||||
{
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user