Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tables.mgetOrPutLazy #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions stew/shims/tables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@ template init*[A, B](T: type OrderedTableRef[A, B]): auto = newOrderedTable[A, B
template init*[A](T: type CountTable[A]): auto = initCountTable[A]()
template init*[A](T: type CountTableRef[A]): auto = newCountTable[A]()

template mgetOrPutLazy*[A, B](t: Table[A, B], key: A, val: B): var B =
type R = B

proc setter(loc: var R): var R =
if loc == default(R):
Copy link
Member

@arnetheduck arnetheduck Aug 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if the collection had a value that was all zeroes (as opposed to not having a value), this will update it to val which doesn't seem to be the intent

Copy link
Contributor Author

@zah zah Aug 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this version of code comes with a number of caveats, but the idea of having a shim is that it allows us to capture the intention of the user until a more general and efficient version of the API is added to the tables module (as a template similar to withValue).

At the moment it's reasonable to use mostly with tables with ref values that don't need to store nil. The default function for the type should be cheap and not a valid value in the usage context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, the that's the point a bit - my intent is not to set if the value is the default - it's to set / create only if it's not in the collection already

loc = val
loc

setter(mgetOrPut(t, key, default(R)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks pretty odd - ie if B is an array[10000, uint64], this will.. zero-init such an array, zeroinit another such array (default(R)) then compare all those values?


export tables, objects

5 changes: 3 additions & 2 deletions tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import
test_bitops2,
test_bitseqs,
test_byteutils,
test_ctops,
test_endians2,
test_macros,
test_objects,
test_ptrops,
test_results,
test_varints,
test_ctops
test_tables,
test_varints
15 changes: 15 additions & 0 deletions tests/test_tables.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import
unittest,
../stew/shims/tables

suite "tables":
test "mgetOrPutLazy":
var t = {"a": 10, "b": 20}.toTable
check t.mgetOrPutLazy("a", 30) == 10
check t.mgetOrPutLazy("c", 40) == 40

check t.mgetOrPutLazy("c", 20) == 40

t.mgetOrPutLazy("c", 20) = 15
check t.mgetOrPutLazy("c", 40) == 15