Initial map implementation.

Still lots of methods missing and clean up and tests to do.
Also still no literal syntax.

But the core hash table code is there and working. The supported
key types are all, uh, supported.
This commit is contained in:
Bob Nystrom
2015-01-24 22:27:35 -08:00
parent cff08f989b
commit abe80e6d4b
11 changed files with 541 additions and 54 deletions

12
test/map/count.wren Normal file
View File

@ -0,0 +1,12 @@
var map = new Map
IO.print(map.count) // expect: 0
map["one"] = "value"
IO.print(map.count) // expect: 1
map["two"] = "value"
IO.print(map.count) // expect: 2
map["three"] = "value"
IO.print(map.count) // expect: 3
// Adding existing key does not increase count.
map["two"] = "new value"
IO.print(map.count) // expect: 3

73
test/map/grow.wren Normal file
View File

@ -0,0 +1,73 @@
// Make sure it can grow to some size.
var fishes = [
"Aeneus corydoras", "African glass catfish", "African lungfish",
"Aholehole", "Airbreathing catfish", "Airsac catfish", "Alaska blackfish",
"Albacore", "Alewife", "Alfonsino", "Algae eater", "Alligatorfish",
"Alligator gar", "American sole", "Amur pike", "Anchovy", "Anemonefish",
"Angelfish", "Angler", "Angler catfish", "Anglerfish", "Antarctic cod",
"Antarctic icefish", "Antenna codlet", "Arapaima", "Archerfish",
"Arctic char", "Armored gurnard", "Armored searobin", "Armorhead",
"Armorhead catfish", "Armoured catfish", "Arowana", "Arrowtooth eel",
"Aruana", "Asian carps", "Asiatic glassfish", "Atka mackerel",
"Atlantic cod", "Atlantic eel", "Atlantic herring", "Atlantic salmon",
"Atlantic saury", "Atlantic silverside", "Atlantic Trout",
"Australasian salmon", "Australian grayling", "Australian herring",
"Australian lungfish", "Australian prowfish", "Ayu", "Alooh",
"Baikal oilfish", "Bala shark", "Ballan wrasse", "Bamboo shark",
"Banded killifish", "Bandfish", "Banjo", "Bangus", "Banjo catfish", "Barb",
"Barbel", "Barbeled dragonfish", "Barbeled houndshark", "Barblless catfish",
"Barfish", "Barracuda", "Barracudina", "Barramundi", "Barred danio",
"Barreleye", "Basking shark", "Bass", "Basslet", "Batfish", "Bat ray",
"Beachsalmon", "Beaked salmon", "Beaked sandfish", "Beardfish",
"Beluga sturgeon", "Bengal danio", "Bent tooth", "Betta", "Bichir",
"Bicolor goat fish", "Bigeye", "Bigeye squaretail", "Bighead carp",
"Bigmouth buffalo", "Bigscale", "Bigscale pomfret", "Billfish", "Bitterling",
"Black angelfish", "Black bass", "Black dragonfish", "Blackchin",
"Blackfish", "black neon tetra", "Blacktip reef shark", "Black mackerel",
"Black pickerel", "Black prickleback", "Black scalyfin", "Black sea bass",
"Black scabbardfish", "Blacksmelt", "Black swallower", "Black tetra",
"Black triggerfish", "Bleak", "Blenny", "Blind goby", "Blind shark",
"Blobfish", "Blowfish", "Blue catfish", "Blue danio", "Blue-redstripe danio",
"Blue eye", "Bluefin tuna", "Bluefish", "Bluegill", "Blue gourami",
"Blue shark", "Blue triggerfish", "Blue whiting", "Bluntnose knifefish",
"Bluntnose minnow", "Boafish", "Boarfish", "Bobtail snipe eel", "Bocaccio",
"Boga", "Bombay duck", "Bonefish", "Bonito", "Bonnetmouth", "Bonytail chub",
"Bonytongue", "Bowfin", "Boxfish", "Bramble shark", "Bream", "Brill",
"Bristlemouth", "Bristlenose catfish", "Broadband dogfish", "Brook lamprey",
"Brook trout", "Brotula", "Brown trout", "Buffalo fish", "Bullhead",
"Bullhead shark", "Bull shark", "Bull trout", "Burbot", "Bumblebee goby",
"Buri", "Burma danio", "Burrowing goby", "Butterfly ray", "Butterflyfish",
"California flyingfish", "California halibut", "California smoothtongue",
"Canary rockfish", "Candiru", "Candlefish", "Capelin", "Cardinalfish",
"Cardinal tetra", "Carp", "Carpetshark", "Carpsucker", "Catalufa", "Catfish",
"Catla", "Cat shark", "Cavefish", "Celebes rainbowfish", "Central mudminnow",
"Cepalin", "Chain pickerel", "Channel bass", "Channel catfish", "Char",
"Cherry salmon", "Chimaera", "Chinook salmon", "Cherubfish", "Chub",
"Chubsucker", "Chum salmon", "Cichlid", "Cisco", "Climbing catfish",
"Climbing gourami", "Climbing perch", "Clingfish", "Clownfish",
"Clown loach", "Clown triggerfish", "Cobbler", "Cobia", "Cod", "Cod icefish",
"Codlet", "Codling", "Coelacanth", "Coffinfish", "Coho salmon", "Coley",
"Collared carpetshark", "Collared dogfish", "Colorado squawfish", "Combfish",
"Combtail gourami", "Combtooth blenny", "Common carp", "Common tunny",
"Conger eel", "Convict blenny", "Convict cichlid", "Cookie-cutter shark",
"Coolie loach", "Cornish Spaktailed Bream", "Cornetfish", "Cowfish",
"Cownose ray", "Cow shark", "Crappie", "Creek chub", "Crestfish",
"Crevice kelpfish", "Croaker", "Crocodile icefish", "Crocodile shark",
"Crucian carp", "Cuchia", "Cuckoo wrasse", "Cusk-eel", "Cuskfish",
"Cutlassfish", "Cutthroat eel", "Cutthroat trout"
]
var map = new Map
for (fish in fishes) {
map[fish] = fish.count
}
IO.print(map.count) // expect: 249
// Re-add some keys.
for (n in 20..50) {
map[fishes[n]] = n
}
IO.print(map.count) // expect: 249

43
test/map/key_types.wren Normal file
View File

@ -0,0 +1,43 @@
var map = new Map
map[null] = "null value"
map[true] = "true value"
map[false] = "false value"
map[0] = "zero"
map[1.2] = "1 point 2"
map[List] = "list class"
map["null"] = "string value"
map[1..3] = "1 to 3"
IO.print(map[null]) // expect: null value
IO.print(map[true]) // expect: true value
IO.print(map[false]) // expect: false value
IO.print(map[0]) // expect: zero
IO.print(map[1.2]) // expect: 1 point 2
IO.print(map[List]) // expect: list class
IO.print(map["null"]) // expect: string value
IO.print(map[1..3]) // expect: 1 to 3
IO.print(map.count) // expect: 8
// Use the same keys (but sometimes different objects) to ensure keys have the
// right equality semantics.
map[null] = "new null value"
map[!false] = "new true value"
map[!true] = "new false value"
map[2 - 2] = "new zero"
map[1.2] = "new 1 point 2"
map[[].type] = "new list class"
map["nu" + "ll"] = "new string value"
map[(3 - 2)..(1 + 2)] = "new 1 to 3"
IO.print(map[null]) // expect: new null value
IO.print(map[true]) // expect: new true value
IO.print(map[false]) // expect: new false value
IO.print(map[0]) // expect: new zero
IO.print(map[1.2]) // expect: new 1 point 2
IO.print(map[List]) // expect: new list class
IO.print(map["null"]) // expect: new string value
IO.print(map[1..3]) // expect: new 1 to 3
IO.print(map.count) // expect: 8

4
test/map/new.wren Normal file
View File

@ -0,0 +1,4 @@
var map = new Map
IO.print(map.count) // expect: 0
IO.print(map) // expect: {}

7
test/map/type.wren Normal file
View File

@ -0,0 +1,7 @@
// TODO: Use map literal.
IO.print(new Map is Map) // expect: true
// TODO: Abstract base class for associations.
IO.print(new Map is Object) // expect: true
IO.print(new Map is Bool) // expect: false
IO.print((new Map).type == Map) // expect: true