Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
This commit is contained in:
commit
d07d83d8fc
6 changed files with 77 additions and 33 deletions
|
|
@ -528,7 +528,7 @@ containers:
|
||||||
proc newNode*[T](data: T): PBinaryTree[T] =
|
proc newNode*[T](data: T): PBinaryTree[T] =
|
||||||
# constructor for a node
|
# constructor for a node
|
||||||
new(result)
|
new(result)
|
||||||
result.dat = data
|
result.data = data
|
||||||
|
|
||||||
proc add*[T](root: var PBinaryTree[T], n: PBinaryTree[T]) =
|
proc add*[T](root: var PBinaryTree[T], n: PBinaryTree[T]) =
|
||||||
# insert a node into the tree
|
# insert a node into the tree
|
||||||
|
|
@ -569,7 +569,7 @@ containers:
|
||||||
|
|
||||||
var
|
var
|
||||||
root: PBinaryTree[string] # instantiate a PBinaryTree with ``string``
|
root: PBinaryTree[string] # instantiate a PBinaryTree with ``string``
|
||||||
add(root, newNode("hallo")) # instantiates ``newNode`` and ``add``
|
add(root, newNode("hello")) # instantiates ``newNode`` and ``add``
|
||||||
add(root, "world") # instantiates the second ``add`` proc
|
add(root, "world") # instantiates the second ``add`` proc
|
||||||
for str in preorder(root):
|
for str in preorder(root):
|
||||||
stdout.writeln(str)
|
stdout.writeln(str)
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ proc open*(host: string = defaultHost, port: int = defaultPort): TDbConn {.
|
||||||
## be established.
|
## be established.
|
||||||
init(result)
|
init(result)
|
||||||
|
|
||||||
let x = connect(result, host, port.cint)
|
let x = client(result, host, port.cint)
|
||||||
if x != 0'i32:
|
if x != 0'i32:
|
||||||
dbError(result, "cannot open: " & host)
|
dbError(result, "cannot open: " & host)
|
||||||
|
|
||||||
|
|
@ -119,7 +119,7 @@ proc insertId*(db: var TDbConn, namespace: string, data: PJsonNode): TOid {.
|
||||||
## the generated OID for the ``_id`` field.
|
## the generated OID for the ``_id`` field.
|
||||||
result = genOid()
|
result = genOid()
|
||||||
var x = jsonToBSon(data, result)
|
var x = jsonToBSon(data, result)
|
||||||
insert(db, namespace, x)
|
insert(db, namespace, x, nil)
|
||||||
destroy(x)
|
destroy(x)
|
||||||
|
|
||||||
proc insert*(db: var TDbConn, namespace: string, data: PJsonNode) {.
|
proc insert*(db: var TDbConn, namespace: string, data: PJsonNode) {.
|
||||||
|
|
|
||||||
|
|
@ -189,6 +189,16 @@ template dollarImpl(): stmt {.dirty.} =
|
||||||
proc `$`*[A, B](t: TTable[A, B]): string =
|
proc `$`*[A, B](t: TTable[A, B]): string =
|
||||||
## The `$` operator for hash tables.
|
## The `$` operator for hash tables.
|
||||||
dollarImpl()
|
dollarImpl()
|
||||||
|
|
||||||
|
proc `==`*[A, B](s, t: TTable[A, B]): bool =
|
||||||
|
s.counter == t.counter and s.data == t.data
|
||||||
|
|
||||||
|
proc indexBy*[A, B, C](collection: A, index: proc(x: B): C): TTable[C, B] =
|
||||||
|
## Index the collection with the proc provided.
|
||||||
|
# TODO: As soon as supported, change collection: A to collection: A[B]
|
||||||
|
result = initTable[C, B]()
|
||||||
|
for item in collection:
|
||||||
|
result[index(item)] = item
|
||||||
|
|
||||||
# ------------------------------ ordered table ------------------------------
|
# ------------------------------ ordered table ------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -109,11 +109,12 @@ type
|
||||||
cur*: cstring
|
cur*: cstring
|
||||||
dataSize*: cint
|
dataSize*: cint
|
||||||
finished*: TBsonBool
|
finished*: TBsonBool
|
||||||
stack*: array[0..32 - 1, cint]
|
ownsData*: TBsonBool
|
||||||
|
err*: cint
|
||||||
|
stackSize*: cint
|
||||||
stackPos*: cint
|
stackPos*: cint
|
||||||
err*: cint ## Bitfield representing errors or warnings on this buffer
|
stackPtr*: ptr csize
|
||||||
errstr*: cstring ## A string representation of the most recent error
|
stack*: array[0..32 - 1, csize]
|
||||||
## or warning.
|
|
||||||
|
|
||||||
TDate* = int64
|
TDate* = int64
|
||||||
|
|
||||||
|
|
@ -141,6 +142,7 @@ proc print*(TBson: cstring, depth: cint) {.stdcall,
|
||||||
importc: "bson_print_raw", dynlib: bsondll.}
|
importc: "bson_print_raw", dynlib: bsondll.}
|
||||||
## Print a string representation of a BSON object up to `depth`.
|
## Print a string representation of a BSON object up to `depth`.
|
||||||
|
|
||||||
|
|
||||||
proc data*(b: var TBson): cstring{.stdcall, importc: "bson_data",
|
proc data*(b: var TBson): cstring{.stdcall, importc: "bson_data",
|
||||||
dynlib: bsondll.}
|
dynlib: bsondll.}
|
||||||
## Return a pointer to the raw buffer stored by this bson object.
|
## Return a pointer to the raw buffer stored by this bson object.
|
||||||
|
|
@ -590,19 +592,30 @@ type
|
||||||
hosts*: ptr THostPort ## List of host/ports given by the replica set
|
hosts*: ptr THostPort ## List of host/ports given by the replica set
|
||||||
name*: cstring ## Name of the replica set.
|
name*: cstring ## Name of the replica set.
|
||||||
primary_connected*: TBsonBool ## Primary node connection status.
|
primary_connected*: TBsonBool ## Primary node connection status.
|
||||||
|
|
||||||
|
TWriteConcern*{.pure, final.} = object ## mongo_write_concern
|
||||||
|
w*: cint
|
||||||
|
wtimeout*: cint
|
||||||
|
j*: cint
|
||||||
|
fsync*: cint
|
||||||
|
mode*: cstring
|
||||||
|
cmd*: TBSon
|
||||||
|
|
||||||
TMongo*{.pure, final.} = object ## mongo
|
TMongo*{.pure, final.} = object ## mongo
|
||||||
primary*: ptr THostPort ## Primary connection info.
|
primary*: ptr THostPort ## Primary connection info.
|
||||||
replset*: ptr TReplSet ## replset object if connected to a replica set.
|
replset*: ptr TReplSet ## replset object if connected to a replica set.
|
||||||
sock*: cint ## Socket file descriptor.
|
sock*: cint ## Socket file descriptor.
|
||||||
flags*: cint ## Flags on this connection object.
|
flags*: cint ## Flags on this connection object.
|
||||||
conn_timeout_ms*: cint ## Connection timeout in milliseconds.
|
conn_timeout_ms*: cint ## Connection timeout in milliseconds.
|
||||||
op_timeout_ms*: cint ## Read and write timeout in milliseconds.
|
op_timeout_ms*: cint ## Read and write timeout in milliseconds.
|
||||||
connected*: TBsonBool ## Connection status.
|
max_bson_size*: cint ## Largest BSON object allowed on this connection.
|
||||||
err*: TError ## Most recent driver error code.
|
connected*: TBsonBool ## Connection status.
|
||||||
errstr*: array[0..128 - 1, char] ## String version of most recent driver error code.
|
write_concern*: TWriteConcern ## The default write concern.
|
||||||
lasterrcode*: cint ## getlasterror code given by the server on error.
|
err*: TError ## Most recent driver error code.
|
||||||
lasterrstr*: cstring ## getlasterror string generated by server.
|
errcode*: cint ## Most recent errno or WSAGetLastError().
|
||||||
|
errstr*: array[0..128 - 1, char] ## String version of most recent driver error code.
|
||||||
|
lasterrcode*: cint ## getlasterror code given by the server on error.
|
||||||
|
lasterrstr*: array[0..128 - 1, char] ## getlasterror string generated by server.
|
||||||
|
|
||||||
TCursor*{.pure, final.} = object ## cursor
|
TCursor*{.pure, final.} = object ## cursor
|
||||||
reply*: ptr TReply ## reply is owned by cursor
|
reply*: ptr TReply ## reply is owned by cursor
|
||||||
|
|
@ -654,7 +667,11 @@ proc init*(conn: var TMongo){.stdcall, importc: "mongo_init", dynlib: mongodll.}
|
||||||
|
|
||||||
proc connect*(conn: var TMongo, host: cstring = defaultHost,
|
proc connect*(conn: var TMongo, host: cstring = defaultHost,
|
||||||
port: cint = defaultPort): cint {.stdcall,
|
port: cint = defaultPort): cint {.stdcall,
|
||||||
importc: "mongo_connect", dynlib: mongodll.}
|
importc: "mongo_connect", dynlib: mongodll, deprecated.}
|
||||||
|
## Connect to a single MongoDB server.
|
||||||
|
proc client*(conn: var TMongo, host: cstring = defaultHost,
|
||||||
|
port: cint = defaultPort): cint {.stdcall,
|
||||||
|
importc: "mongo_client", dynlib: mongodll.}
|
||||||
## Connect to a single MongoDB server.
|
## Connect to a single MongoDB server.
|
||||||
|
|
||||||
proc replsetInit*(conn: var TMongo, name: cstring){.stdcall,
|
proc replsetInit*(conn: var TMongo, name: cstring){.stdcall,
|
||||||
|
|
@ -714,7 +731,8 @@ proc destroy*(conn: var TMongo){.stdcall, importc: "mongo_destroy",
|
||||||
## You must always call this function when finished with the connection
|
## You must always call this function when finished with the connection
|
||||||
## object.
|
## object.
|
||||||
|
|
||||||
proc insert*(conn: var TMongo, ns: cstring, data: var TBson): cint{.stdcall,
|
proc insert*(conn: var TMongo, ns: cstring, data: var TBson,
|
||||||
|
custom_write_concern: ptr TWriteConcern): cint{.stdcall,
|
||||||
importc: "mongo_insert", dynlib: mongodll, discardable.}
|
importc: "mongo_insert", dynlib: mongodll, discardable.}
|
||||||
## Insert a BSON document into a MongoDB server. This function
|
## Insert a BSON document into a MongoDB server. This function
|
||||||
## will fail if the supplied BSON struct is not UTF-8 or if
|
## will fail if the supplied BSON struct is not UTF-8 or if
|
||||||
|
|
|
||||||
22
tests/collections/ttables.nim
Normal file
22
tests/collections/ttables.nim
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import tables
|
||||||
|
|
||||||
|
doAssert indexBy(newSeq[int](), proc(x: int):int = x) == initTable[int, int](), "empty int table"
|
||||||
|
|
||||||
|
var tbl1 = initTable[int, int]()
|
||||||
|
tbl1.add(1,1)
|
||||||
|
tbl1.add(2,2)
|
||||||
|
doAssert indexBy(@[1,2], proc(x: int):int = x) == tbl1, "int table"
|
||||||
|
|
||||||
|
type
|
||||||
|
TElem = object
|
||||||
|
foo: int
|
||||||
|
bar: string
|
||||||
|
|
||||||
|
let
|
||||||
|
elem1 = TElem(foo: 1, bar: "bar")
|
||||||
|
elem2 = TElem(foo: 2, bar: "baz")
|
||||||
|
|
||||||
|
var tbl2 = initTable[string, TElem]()
|
||||||
|
tbl2.add("bar", elem1)
|
||||||
|
tbl2.add("baz", elem2)
|
||||||
|
doAssert indexBy(@[elem1,elem2], proc(x: TElem): string = x.bar) == tbl2, "element table"
|
||||||
|
|
@ -1,14 +1,8 @@
|
||||||
import unittest
|
|
||||||
import algorithm
|
import algorithm
|
||||||
|
|
||||||
suite "product":
|
doAssert product[int](newSeq[seq[int]]()) == newSeq[seq[int]](), "empty input"
|
||||||
test "empty input":
|
doAssert product[int](@[newSeq[int](), @[], @[]]) == newSeq[seq[int]](), "bit more empty input"
|
||||||
check product[int](newSeq[seq[int]]()) == newSeq[seq[int]]()
|
doAssert product(@[@[1,2]]) == @[@[1,2]], "a simple case of one element"
|
||||||
test "bit more empty input":
|
doAssert product(@[@[1,2], @[3,4]]) == @[@[2,4],@[1,4],@[2,3],@[1,3]], "two elements"
|
||||||
check product[int](@[newSeq[int](), @[], @[]]) == newSeq[seq[int]]()
|
doAssert product(@[@[1,2], @[3,4], @[5,6]]) == @[@[2,4,6],@[1,4,6],@[2,3,6],@[1,3,6], @[2,4,5],@[1,4,5],@[2,3,5],@[1,3,5]], "three elements"
|
||||||
test "a simple case of one element":
|
doAssert product(@[@[1,2], @[]]) == newSeq[seq[int]](), "two elements, but one empty"
|
||||||
check product(@[@[1,2]]) == @[@[1,2]]
|
|
||||||
test "two elements":
|
|
||||||
check product(@[@[1,2], @[3,4]]) == @[@[2,4],@[1,4],@[2,3],@[1,3]]
|
|
||||||
test "three elements":
|
|
||||||
check product(@[@[1,2], @[3,4], @[5,6]]) == @[@[2,4,6],@[1,4,6],@[2,3,6],@[1,3,6], @[2,4,5],@[1,4,5],@[2,3,5],@[1,3,5]]
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue