From 4690ce81e079fc58cae8d6d583e5e3eb3ed81a83 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 9 Mar 2016 02:56:27 -0800 Subject: 2743 Looks like "TOhtml | " doesn't work on Mac OS X for some reason.. --- html/078table.mu.html | 64 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 24 deletions(-) (limited to 'html/078table.mu.html') diff --git a/html/078table.mu.html b/html/078table.mu.html index 6efef6d2..c6ecdaca 100644 --- a/html/078table.mu.html +++ b/html/078table.mu.html @@ -3,34 +3,27 @@ Mu - 078table.mu - - + + - - + - - -
+
 # A table is like an array, except that its keys are not integers but
 # arbitrary types.
 
@@ -45,6 +38,18 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; }
   ]
 ]
 
+scenario table-read-write-non-integer [
+  run [
+    1:address:shared:array:character <- new [abc def]
+    {2: (address shared table (address shared array character) number)} <- new-table 30
+    put {2: (address shared table (address shared array character) number)}, 1:address:shared:array:character, 34
+    3:number <- index {2: (address shared table (address shared array character) number)}, 1:address:shared:array:character
+  ]
+  memory-should-contain [
+    3 <- 34
+  ]
+]
+
 container table:_key:_value [
   length:number
   capacity:number
@@ -57,7 +62,7 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; }
   value:_value
 ]
 
-recipe new-table capacity:number -> result:address:shared:table:_key:_value [
+def new-table capacity:number -> result:address:shared:table:_key:_value [
   local-scope
   load-ingredients
   result <- new {(table _key _value): type}
@@ -67,12 +72,14 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; }
   *data <- new {(table_row _key _value): type}, capacity
 ]
 
-recipe put table:address:shared:table:_key:_value, key:_key, value:_value -> table:address:shared:table:_key:_value [
+def put table:address:shared:table:_key:_value, key:_key, value:_value -> table:address:shared:table:_key:_value [
   local-scope
   load-ingredients
   hash:number <- hash key
+  hash <- abs hash
   capacity:number <- get *table, capacity:offset
   _, hash <- divide-with-remainder hash, capacity
+  hash <- abs hash  # in case hash overflows into a negative integer
   table-data:address:shared:array:table_row:_key:_value <- get *table, data:offset
   x:address:table_row:_key:_value <- index-address *table-data, hash
   occupied?:boolean <- get *x, occupied?:offset
@@ -81,19 +88,28 @@ body { font-family: monospace; color: #eeeeee; background-color: #080808; }
   *x <- merge 1/true, key, value
 ]
 
-recipe index table:address:shared:table:_key:_value, key:_key -> result:_value [
+def abs n:number -> result:number [
+  local-scope
+  load-ingredients
+  positive?:boolean <- greater-or-equal n, 0
+  return-if positive?, n
+  result <- multiply n, -1
+]
+
+def index table:address:shared:table:_key:_value, key:_key -> result:_value [
   local-scope
   load-ingredients
   hash:number <- hash key
+  hash <- abs hash
   capacity:number <- get *table, capacity:offset
   _, hash <- divide-with-remainder hash, capacity
+  hash <- abs hash  # in case hash overflows into a negative integer
   table-data:address:shared:array:table_row:_key:_value <- get *table, data:offset
-  x:address:table_row:_key:_value <- index-address *table-data, hash
-  occupied?:boolean <- get *x, occupied?:offset
+  x:table_row:_key:_value <- index *table-data, hash
+  occupied?:boolean <- get x, occupied?:offset
   assert occupied?, [can't handle missing elements yet]
-  result <- get *x, value:offset
+  result <- get x, value:offset
 ]
 
- -- cgit 1.4.1-2-gfad0