about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-10-31 23:13:20 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-10-31 23:13:20 -0700
commit48835572a7c6922927a443c27985434fddaeacf5 (patch)
treec51a9c2e46193293a15ee541ea37f1c6f9be60a6
parent9d9da2adf96d783ae51edc1bf6c9cbe6017ead8f (diff)
downloadmu-48835572a7c6922927a443c27985434fddaeacf5.tar.gz
3619
-rw-r--r--070table.mu31
-rw-r--r--html/070table.mu.html31
2 files changed, 50 insertions, 12 deletions
diff --git a/070table.mu b/070table.mu
index 171bb2a8..ce51f317 100644
--- a/070table.mu
+++ b/070table.mu
@@ -6,10 +6,11 @@ scenario table-read-write [
   tab:&:table:num:num <- new-table 30
   run [
     put-index tab, 12, 34
-    1:num/raw <- index tab, 12
+    60:num/raw, 61:bool/raw <- index tab, 12
   ]
   memory-should-contain [
-    1 <- 34
+    60 <- 34
+    61 <- 1  # found
   ]
 ]
 
@@ -18,10 +19,23 @@ scenario table-read-write-non-integer [
   tab:&:table:text:num <- new-table 30
   run [
     put-index tab, [abc def], 34
-    1:num/raw <- index tab, [abc def]
+    1:num/raw, 2:bool/raw <- index tab, [abc def]
   ]
   memory-should-contain [
     1 <- 34
+    2 <- 1  # found
+  ]
+]
+
+scenario table-read-not-found [
+  local-scope
+  tab:&:table:text:num <- new-table 30
+  run [
+    1:num/raw, 2:bool/raw <- index tab, [abc def]
+  ]
+  memory-should-contain [
+    1 <- 0
+    2 <- 0  # not found
   ]
 ]
 
@@ -62,7 +76,7 @@ def put-index table:&:table:_key:_value, key:_key, value:_value -> table:&:table
   *table-data <- put-index *table-data, hash-key, new-row
 ]
 
-def index table:&:table:_key:_value, key:_key -> result:_value [
+def index table:&:table:_key:_value, key:_key -> result:_value, found?:bool [
   local-scope
   load-ingredients
   hash:num <- hash key
@@ -72,8 +86,13 @@ def index table:&:table:_key:_value, key:_key -> result:_value [
   hash-key <- abs hash-key  # in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above
   table-data:&:@:table-row:_key:_value <- get *table, data:offset
   x:table-row:_key:_value <- index *table-data, hash-key
-  occupied?:bool <- get x, occupied?:offset
-  assert occupied?, [can't handle missing elements yet]
+  empty:&:_value <- new _value:type
+  result <- copy *empty
+  found?:bool <- get x, occupied?:offset
+  return-unless found?
+  key2:_key <- get x, key:offset
+  found?:bool <- equal key, key2
+  return-unless found?
   result <- get x, value:offset
 ]
 
diff --git a/html/070table.mu.html b/html/070table.mu.html
index b843f50f..85df5029 100644
--- a/html/070table.mu.html
+++ b/html/070table.mu.html
@@ -40,10 +40,11 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
   tab:&amp;:table:num:num <span class="Special">&lt;-</span> new-table<span class="Constant"> 30</span>
   run [
     put-index tab,<span class="Constant"> 12</span>,<span class="Constant"> 34</span>
-    1:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> index tab,<span class="Constant"> 12</span>
+    60:num/<span class="Special">raw</span>, 61:bool/<span class="Special">raw</span> <span class="Special">&lt;-</span> index tab,<span class="Constant"> 12</span>
   ]
   memory-should-contain [
-   <span class="Constant"> 1</span> <span class="Special">&lt;-</span><span class="Constant"> 34</span>
+   <span class="Constant"> 60</span> <span class="Special">&lt;-</span><span class="Constant"> 34</span>
+   <span class="Constant"> 61</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>  <span class="Comment"># found</span>
   ]
 ]
 
@@ -52,10 +53,23 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
   tab:&amp;:table:text:num <span class="Special">&lt;-</span> new-table<span class="Constant"> 30</span>
   run [
     put-index tab, <span class="Constant">[abc def]</span>,<span class="Constant"> 34</span>
-    1:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> index tab, <span class="Constant">[abc def]</span>
+    1:num/<span class="Special">raw</span>, 2:bool/<span class="Special">raw</span> <span class="Special">&lt;-</span> index tab, <span class="Constant">[abc def]</span>
   ]
   memory-should-contain [
    <span class="Constant"> 1</span> <span class="Special">&lt;-</span><span class="Constant"> 34</span>
+   <span class="Constant"> 2</span> <span class="Special">&lt;-</span><span class="Constant"> 1</span>  <span class="Comment"># found</span>
+  ]
+]
+
+<span class="muScenario">scenario</span> table-read-not-found [
+  <span class="Constant">local-scope</span>
+  tab:&amp;:table:text:num <span class="Special">&lt;-</span> new-table<span class="Constant"> 30</span>
+  run [
+    1:num/<span class="Special">raw</span>, 2:bool/<span class="Special">raw</span> <span class="Special">&lt;-</span> index tab, <span class="Constant">[abc def]</span>
+  ]
+  memory-should-contain [
+   <span class="Constant"> 1</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>
+   <span class="Constant"> 2</span> <span class="Special">&lt;-</span><span class="Constant"> 0</span>  <span class="Comment"># not found</span>
   ]
 ]
 
@@ -96,7 +110,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
   *table-data <span class="Special">&lt;-</span> put-index *table-data, hash-key, new-row
 ]
 
-<span class="muRecipe">def</span> index table:&amp;:table:_key:_value, key:_key<span class="muRecipe"> -&gt; </span>result:_value [
+<span class="muRecipe">def</span> index table:&amp;:table:_key:_value, key:_key<span class="muRecipe"> -&gt; </span>result:_value, found?:bool [
   <span class="Constant">local-scope</span>
   <span class="Constant">load-ingredients</span>
   hash:num <span class="Special">&lt;-</span> hash key
@@ -106,8 +120,13 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
   hash-key <span class="Special">&lt;-</span> abs hash-key  <span class="Comment"># in case hash overflows from a double into a negative integer inside 'divide-with-remainder' above</span>
   table-data:&amp;:@:table-row:_key:_value <span class="Special">&lt;-</span> get *table, <span class="Constant">data:offset</span>
   x:table-row:_key:_value <span class="Special">&lt;-</span> index *table-data, hash-key
-  occupied?:bool <span class="Special">&lt;-</span> get x, <span class="Constant">occupied?:offset</span>
-  assert occupied?, <span class="Constant">[can't handle missing elements yet]</span>
+  empty:&amp;:_value <span class="Special">&lt;-</span> new <span class="Constant">_value:type</span>
+  result <span class="Special">&lt;-</span> copy *empty
+  found?:bool <span class="Special">&lt;-</span> get x, <span class="Constant">occupied?:offset</span>
+  <span class="muControl">return-unless</span> found?
+  key2:_key <span class="Special">&lt;-</span> get x, <span class="Constant">key:offset</span>
+  found?:bool <span class="Special">&lt;-</span> equal key, key2
+  <span class="muControl">return-unless</span> found?
   result <span class="Special">&lt;-</span> get x, <span class="Constant">value:offset</span>
 ]