Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
This commit is contained in:
commit
817337af30
5 changed files with 130 additions and 5 deletions
2
koch.nim
2
koch.nim
|
|
@ -169,7 +169,7 @@ const
|
||||||
".idx"
|
".idx"
|
||||||
]
|
]
|
||||||
ignore = [
|
ignore = [
|
||||||
".bzrignore", "nimrod", "nimrod.exe", "koch", "koch.exe"
|
".bzrignore", "nimrod", "nimrod.exe", "koch", "koch.exe", ".gitignore"
|
||||||
]
|
]
|
||||||
|
|
||||||
proc cleanAux(dir: string) =
|
proc cleanAux(dir: string) =
|
||||||
|
|
|
||||||
|
|
@ -381,8 +381,8 @@ when defined(windows) or defined(nimdoc):
|
||||||
let err = osLastError()
|
let err = osLastError()
|
||||||
if err.int32 != ERROR_IO_PENDING:
|
if err.int32 != ERROR_IO_PENDING:
|
||||||
dealloc dataBuf.buf
|
dealloc dataBuf.buf
|
||||||
retFuture.fail(newException(EOS, osErrorMsg(err)))
|
|
||||||
dealloc(ol)
|
dealloc(ol)
|
||||||
|
retFuture.fail(newException(EOS, osErrorMsg(err)))
|
||||||
elif ret == 0 and bytesReceived == 0 and dataBuf.buf[0] == '\0':
|
elif ret == 0 and bytesReceived == 0 and dataBuf.buf[0] == '\0':
|
||||||
# We have to ensure that the buffer is empty because WSARecv will tell
|
# We have to ensure that the buffer is empty because WSARecv will tell
|
||||||
# us immediatelly when it was disconnected, even when there is still
|
# us immediatelly when it was disconnected, even when there is still
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ proc processClient(client: PAsyncSocket, address: string,
|
||||||
return
|
return
|
||||||
|
|
||||||
case reqMethod.normalize
|
case reqMethod.normalize
|
||||||
of "get":
|
of "get", "post", "head", "put", "delete", "trace", "options", "connect", "patch":
|
||||||
await callback(request)
|
await callback(request)
|
||||||
else:
|
else:
|
||||||
request.respond(Http400, "Invalid request method. Got: " & reqMethod)
|
request.respond(Http400, "Invalid request method. Got: " & reqMethod)
|
||||||
|
|
|
||||||
|
|
@ -80,14 +80,48 @@ proc connect*(socket: PAsyncSocket, address: string, port: TPort,
|
||||||
## or an error occurs.
|
## or an error occurs.
|
||||||
result = connect(socket.fd.TAsyncFD, address, port, af)
|
result = connect(socket.fd.TAsyncFD, address, port, af)
|
||||||
|
|
||||||
|
proc readIntoBuf(socket: PAsyncSocket, flags: int): PFuture[int] {.async.} =
|
||||||
|
var data = await recv(socket.fd.TAsyncFD, BufferSize, flags)
|
||||||
|
if data.len != 0:
|
||||||
|
copyMem(addr socket.buffer[0], addr data[0], data.len)
|
||||||
|
socket.bufLen = data.len
|
||||||
|
socket.currPos = 0
|
||||||
|
result = data.len
|
||||||
|
|
||||||
proc recv*(socket: PAsyncSocket, size: int,
|
proc recv*(socket: PAsyncSocket, size: int,
|
||||||
flags: int = 0): PFuture[string] =
|
flags: int = 0): PFuture[string] {.async.} =
|
||||||
## Reads ``size`` bytes from ``socket``. Returned future will complete once
|
## Reads ``size`` bytes from ``socket``. Returned future will complete once
|
||||||
## all of the requested data is read. If socket is disconnected during the
|
## all of the requested data is read. If socket is disconnected during the
|
||||||
## recv operation then the future may complete with only a part of the
|
## recv operation then the future may complete with only a part of the
|
||||||
## requested data read. If socket is disconnected and no data is available
|
## requested data read. If socket is disconnected and no data is available
|
||||||
## to be read then the future will complete with a value of ``""``.
|
## to be read then the future will complete with a value of ``""``.
|
||||||
result = recv(socket.fd.TAsyncFD, size, flags)
|
if socket.isBuffered:
|
||||||
|
result = newString(size)
|
||||||
|
|
||||||
|
template returnNow(readBytes: int) =
|
||||||
|
result.setLen(readBytes)
|
||||||
|
# Only increase buffer position when not peeking.
|
||||||
|
if (flags and MSG_PEEK) != MSG_PEEK:
|
||||||
|
socket.currPos.inc(readBytes)
|
||||||
|
return
|
||||||
|
|
||||||
|
if socket.bufLen == 0:
|
||||||
|
let res = await socket.readIntoBuf(flags and (not MSG_PEEK))
|
||||||
|
if res == 0: returnNow(0)
|
||||||
|
|
||||||
|
var read = 0
|
||||||
|
while read < size:
|
||||||
|
if socket.currPos >= socket.bufLen:
|
||||||
|
let res = await socket.readIntoBuf(flags and (not MSG_PEEK))
|
||||||
|
if res == 0: returnNow(read)
|
||||||
|
|
||||||
|
let chunk = min(socket.bufLen-socket.currPos, size-read)
|
||||||
|
copyMem(addr(result[read]), addr(socket.buffer[socket.currPos+read]), chunk)
|
||||||
|
read.inc(chunk)
|
||||||
|
|
||||||
|
returnNow(read)
|
||||||
|
else:
|
||||||
|
result = await recv(socket.fd.TAsyncFD, size, flags)
|
||||||
|
|
||||||
proc send*(socket: PAsyncSocket, data: string): PFuture[void] =
|
proc send*(socket: PAsyncSocket, data: string): PFuture[void] =
|
||||||
## Sends ``data`` to ``socket``. The returned future will complete once all
|
## Sends ``data`` to ``socket``. The returned future will complete once all
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,71 @@ proc zip*[S, T](seq1: seq[S], seq2: seq[T]): seq[tuple[a: S, b: T]] =
|
||||||
newSeq(result, m)
|
newSeq(result, m)
|
||||||
for i in 0 .. m-1: result[i] = (seq1[i], seq2[i])
|
for i in 0 .. m-1: result[i] = (seq1[i], seq2[i])
|
||||||
|
|
||||||
|
proc distribute*[T](s: seq[T], num: int, spread = true): seq[seq[T]] =
|
||||||
|
## Splits and distributes a sequence `s` into `num` sub sequences.
|
||||||
|
##
|
||||||
|
## Returns a sequence of `num` sequences. For some input values this is the
|
||||||
|
## inverse of the `concat <#concat>`_ proc. The proc will assert in debug
|
||||||
|
## builds if `s` is nil or `num` is less than one, and will likely crash on
|
||||||
|
## release builds. The input sequence `s` can be empty, which will produce
|
||||||
|
## `num` empty sequences.
|
||||||
|
##
|
||||||
|
## If `spread` is false and the length of `s` is not a multiple of `num`, the
|
||||||
|
## proc will max out the first sub sequences with ``1 + len(s) div num``
|
||||||
|
## entries, leaving the remainder of elements to the last sequence.
|
||||||
|
##
|
||||||
|
## On the other hand, if `spread` is true, the proc will distribute evenly
|
||||||
|
## the remainder of the division across all sequences, which makes the result
|
||||||
|
## more suited to multithreading where you are passing equal sized work units
|
||||||
|
## to a thread pool and want to maximize core usage.
|
||||||
|
##
|
||||||
|
## Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: nimrod
|
||||||
|
## let numbers = @[1, 2, 3, 4, 5, 6, 7]
|
||||||
|
## assert numbers.distribute(3) == @[@[1, 2, 3], @[4, 5], @[6, 7]]
|
||||||
|
## assert numbers.distribute(3, false) == @[@[1, 2, 3], @[4, 5, 6], @[7]]
|
||||||
|
## assert numbers.distribute(6)[0] == @[1, 2]
|
||||||
|
## assert numbers.distribute(6)[5] == @[7]
|
||||||
|
assert(not s.isNil, "`s` can't be nil")
|
||||||
|
assert(num > 0, "`num` has to be greater than zero")
|
||||||
|
if num < 2:
|
||||||
|
result = @[s]
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create the result and calculate the stride size and the remainder if any.
|
||||||
|
result = newSeq[seq[T]](num)
|
||||||
|
var
|
||||||
|
stride = s.len div num
|
||||||
|
first = 0
|
||||||
|
last = 0
|
||||||
|
extra = s.len mod num
|
||||||
|
|
||||||
|
if extra == 0 or spread == false:
|
||||||
|
# Use an algorithm which overcounts the stride and minimizes reading limits.
|
||||||
|
if extra > 0: inc(stride)
|
||||||
|
|
||||||
|
for i in 0 .. <num:
|
||||||
|
result[i] = newSeq[T]()
|
||||||
|
for g in first .. <min(s.len, first + stride):
|
||||||
|
result[i].add(s[g])
|
||||||
|
first += stride
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Use an undercounting algorithm which *adds* the remainder each iteration.
|
||||||
|
for i in 0 .. <num:
|
||||||
|
last = first + stride
|
||||||
|
if extra > 0:
|
||||||
|
extra -= 1
|
||||||
|
inc(last)
|
||||||
|
|
||||||
|
result[i] = newSeq[T]()
|
||||||
|
for g in first .. <last:
|
||||||
|
result[i].add(s[g])
|
||||||
|
first = last
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): T =
|
iterator filter*[T](seq1: seq[T], pred: proc(item: T): bool {.closure.}): T =
|
||||||
## Iterates through a sequence and yields every item that fulfills the
|
## Iterates through a sequence and yields every item that fulfills the
|
||||||
## predicate.
|
## predicate.
|
||||||
|
|
@ -420,4 +485,30 @@ when isMainModule:
|
||||||
nums.mapIt(it * 3)
|
nums.mapIt(it * 3)
|
||||||
assert nums[0] + nums[3] == 15
|
assert nums[0] + nums[3] == 15
|
||||||
|
|
||||||
|
block: # distribute tests
|
||||||
|
let numbers = @[1, 2, 3, 4, 5, 6, 7]
|
||||||
|
doAssert numbers.distribute(3) == @[@[1, 2, 3], @[4, 5], @[6, 7]]
|
||||||
|
doAssert numbers.distribute(6)[0] == @[1, 2]
|
||||||
|
doAssert numbers.distribute(6)[5] == @[7]
|
||||||
|
let a = @[1, 2, 3, 4, 5, 6, 7]
|
||||||
|
doAssert a.distribute(1, true) == @[@[1, 2, 3, 4, 5, 6, 7]]
|
||||||
|
doAssert a.distribute(1, false) == @[@[1, 2, 3, 4, 5, 6, 7]]
|
||||||
|
doAssert a.distribute(2, true) == @[@[1, 2, 3, 4], @[5, 6, 7]]
|
||||||
|
doAssert a.distribute(2, false) == @[@[1, 2, 3, 4], @[5, 6, 7]]
|
||||||
|
doAssert a.distribute(3, true) == @[@[1, 2, 3], @[4, 5], @[6, 7]]
|
||||||
|
doAssert a.distribute(3, false) == @[@[1, 2, 3], @[4, 5, 6], @[7]]
|
||||||
|
doAssert a.distribute(4, true) == @[@[1, 2], @[3, 4], @[5, 6], @[7]]
|
||||||
|
doAssert a.distribute(4, false) == @[@[1, 2], @[3, 4], @[5, 6], @[7]]
|
||||||
|
doAssert a.distribute(5, true) == @[@[1, 2], @[3, 4], @[5], @[6], @[7]]
|
||||||
|
doAssert a.distribute(5, false) == @[@[1, 2], @[3, 4], @[5, 6], @[7], @[]]
|
||||||
|
doAssert a.distribute(6, true) == @[@[1, 2], @[3], @[4], @[5], @[6], @[7]]
|
||||||
|
doAssert a.distribute(6, false) == @[
|
||||||
|
@[1, 2], @[3, 4], @[5, 6], @[7], @[], @[]]
|
||||||
|
doAssert a.distribute(8, false) == a.distribute(8, true)
|
||||||
|
doAssert a.distribute(90, false) == a.distribute(90, true)
|
||||||
|
var b = @[0]
|
||||||
|
for f in 1 .. 25: b.add(f)
|
||||||
|
doAssert b.distribute(5, true)[4].len == 5
|
||||||
|
doAssert b.distribute(5, false)[4].len == 2
|
||||||
|
|
||||||
echo "Finished doc tests"
|
echo "Finished doc tests"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue