From 9009841d623d268c649f96bd0215b3013de35eeb Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Fri, 6 Jun 2014 19:53:39 +0200 Subject: Avoids temporal string in tables hashing example. --- lib/pure/collections/tables.nim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib/pure/collections/tables.nim') diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index 848f4b8ba..b5fc1737a 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -37,7 +37,8 @@ ## ## Piggyback on the already available string hash proc. ## ## ## ## Without this proc nothing works! -## result = hash(x.firstName & x.lastName) +## result = x.firstName.hash !& x.lastName.hash +## result = !$result ## ## var ## salaries = initTable[Person, int]() @@ -841,7 +842,8 @@ when isMainModule: ## Piggyback on the already available string hash proc. ## ## Without this proc nothing works! - result = hash(x.firstName & x.lastName) + result = x.firstName.hash !& x.lastName.hash + result = !$result var salaries = initTable[Person, int]() -- cgit 1.4.1-2-gfad0 From bde9d1ac0753e46c726dc63930539bb82d09f19d Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Fri, 6 Jun 2014 20:10:13 +0200 Subject: Adds to tables module example of reference type vs value type. --- lib/pure/collections/tables.nim | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'lib/pure/collections/tables.nim') diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index b5fc1737a..e51a04dd1 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -52,8 +52,25 @@ ## p2.lastName = "박" ## salaries[p2] = 45_000 ## -## **Note:** The data types declared here have *value semantics*: This means -## that ``=`` performs a copy of the hash table. +## **Note:** The data types declared here starting with the **T** prefix have +## *value semantics*: This means that ``=`` performs a copy of the hash table. +## On the other hand, types declared with the **P** prefix have *reference +## semantics*. Behaviour comparison: +## +## .. code-block:: nimrod +## var valueWords = initTable[string, string]() +## valueWords["teh"] = "the" +## var valueWordsClone = valueWords +## # Changing the clone won't change the original. +## valueWordsClone["teh"] = "thehehe" +## assert valueWords["teh"] != valueWordsClone["teh"] +## +## var refWords = newTable[string, string]() +## refWords["teh"] = "the" +## var refWordsShadow = refWords +## # Both the shadow and the original share the same data. +## refWordsShadow["teh"] = "thehehe" +## assert refWords["teh"] == refWordsShadow["teh"] import hashes, math @@ -861,3 +878,16 @@ when isMainModule: s2[p2] = 45_000 s3[p1] = 30_000 s3[p2] = 45_000 + + # Ref verification. + var valueWords = initTable[string, string]() + valueWords["teh"] = "the" + var valueWordsClone = valueWords + valueWordsClone["teh"] = "thehehe" + assert valueWords["teh"] != valueWordsClone["teh"] + + var refWords = newTable[string, string]() + refWords["teh"] = "the" + var refWordsShadow = refWords + refWordsShadow["teh"] = "thehehe" + assert refWords["teh"] == refWordsShadow["teh"] -- cgit 1.4.1-2-gfad0 From af6abac4911be18bd92a9190ccbe39aa72ab1a79 Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Tue, 10 Jun 2014 00:39:19 +0200 Subject: Revert "Adds to tables module example of reference type vs value type." This reverts commit bde9d1ac0753e46c726dc63930539bb82d09f19d. --- lib/pure/collections/tables.nim | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'lib/pure/collections/tables.nim') diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index e51a04dd1..b5fc1737a 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -52,25 +52,8 @@ ## p2.lastName = "박" ## salaries[p2] = 45_000 ## -## **Note:** The data types declared here starting with the **T** prefix have -## *value semantics*: This means that ``=`` performs a copy of the hash table. -## On the other hand, types declared with the **P** prefix have *reference -## semantics*. Behaviour comparison: -## -## .. code-block:: nimrod -## var valueWords = initTable[string, string]() -## valueWords["teh"] = "the" -## var valueWordsClone = valueWords -## # Changing the clone won't change the original. -## valueWordsClone["teh"] = "thehehe" -## assert valueWords["teh"] != valueWordsClone["teh"] -## -## var refWords = newTable[string, string]() -## refWords["teh"] = "the" -## var refWordsShadow = refWords -## # Both the shadow and the original share the same data. -## refWordsShadow["teh"] = "thehehe" -## assert refWords["teh"] == refWordsShadow["teh"] +## **Note:** The data types declared here have *value semantics*: This means +## that ``=`` performs a copy of the hash table. import hashes, math @@ -878,16 +861,3 @@ when isMainModule: s2[p2] = 45_000 s3[p1] = 30_000 s3[p2] = 45_000 - - # Ref verification. - var valueWords = initTable[string, string]() - valueWords["teh"] = "the" - var valueWordsClone = valueWords - valueWordsClone["teh"] = "thehehe" - assert valueWords["teh"] != valueWordsClone["teh"] - - var refWords = newTable[string, string]() - refWords["teh"] = "the" - var refWordsShadow = refWords - refWordsShadow["teh"] = "thehehe" - assert refWords["teh"] == refWordsShadow["teh"] -- cgit 1.4.1-2-gfad0