Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -3,3 +3,5 @@ In this task, the goal is to create an [[associative array]] (also known as a di
|
|||
Related tasks:
|
||||
* [[Associative arrays/Iteration]]
|
||||
* [[Hash from two arrays]]
|
||||
|
||||
{{Template:See also lists}}
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
record r;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
r_put(r, "A", 33); # an integer value
|
||||
r_put(r, "C", 2.5); # a real value
|
||||
r_put(r, "B", "associative"); # a string value
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
// arr is an array of string to int. any type can be used in both places.
|
||||
var keys: domain(string);
|
||||
var arr: [keys] int;
|
||||
|
||||
// keys can be added to a domain using +, new values will be initialized to the default value (0 for int)
|
||||
keys += "foo";
|
||||
keys += "bar";
|
||||
keys += "baz";
|
||||
|
||||
// array access via [] or ()
|
||||
arr["foo"] = 1;
|
||||
arr["bar"] = 4;
|
||||
arr("baz") = 6;
|
||||
|
||||
// write auto-formats domains and arrays
|
||||
writeln("Keys: ", keys);
|
||||
writeln("Values: ", arr);
|
||||
|
||||
// keys can be deleted using -
|
||||
keys -= "bar";
|
||||
|
||||
writeln("Keys: ", keys);
|
||||
writeln("Values: ", arr);
|
||||
|
||||
// chapel also supports array literals
|
||||
var arr2 = [ "John" => 3, "Pete" => 14 ];
|
||||
|
||||
writeln("arr2 keys: ", arr2.domain);
|
||||
writeln("arr2 values: ", arr2);
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
DEFINITION Collections;
|
||||
|
||||
IMPORT Boxes;
|
||||
|
||||
CONST
|
||||
notFound = -1;
|
||||
|
||||
TYPE
|
||||
Hash = POINTER TO RECORD
|
||||
cap-, size-: INTEGER;
|
||||
(h: Hash) ContainsKey (k: Boxes.Object): BOOLEAN, NEW;
|
||||
(h: Hash) Get (k: Boxes.Object): Boxes.Object, NEW;
|
||||
(h: Hash) IsEmpty (): BOOLEAN, NEW;
|
||||
(h: Hash) Put (k, v: Boxes.Object): Boxes.Object, NEW;
|
||||
(h: Hash) Remove (k: Boxes.Object): Boxes.Object, NEW;
|
||||
(h: Hash) Reset, NEW
|
||||
END;
|
||||
|
||||
HashMap = POINTER TO RECORD
|
||||
cap-, size-: INTEGER;
|
||||
(hm: HashMap) ContainsKey (k: Boxes.Object): BOOLEAN, NEW;
|
||||
(hm: HashMap) ContainsValue (v: Boxes.Object): BOOLEAN, NEW;
|
||||
(hm: HashMap) Get (k: Boxes.Object): Boxes.Object, NEW;
|
||||
(hm: HashMap) IsEmpty (): BOOLEAN, NEW;
|
||||
(hm: HashMap) Keys (): POINTER TO ARRAY OF Boxes.Object, NEW;
|
||||
(hm: HashMap) Put (k, v: Boxes.Object): Boxes.Object, NEW;
|
||||
(hm: HashMap) Remove (k: Boxes.Object): Boxes.Object, NEW;
|
||||
(hm: HashMap) Reset, NEW;
|
||||
(hm: HashMap) Values (): POINTER TO ARRAY OF Boxes.Object, NEW
|
||||
END;
|
||||
|
||||
LinkedList = POINTER TO RECORD
|
||||
first-, last-: Node;
|
||||
size-: INTEGER;
|
||||
(ll: LinkedList) Add (item: Boxes.Object), NEW;
|
||||
(ll: LinkedList) Append (item: Boxes.Object), NEW;
|
||||
(ll: LinkedList) AsString (): POINTER TO ARRAY OF CHAR, NEW;
|
||||
(ll: LinkedList) Contains (item: Boxes.Object): BOOLEAN, NEW;
|
||||
(ll: LinkedList) Get (at: INTEGER): Boxes.Object, NEW;
|
||||
(ll: LinkedList) IndexOf (item: Boxes.Object): INTEGER, NEW;
|
||||
(ll: LinkedList) Insert (at: INTEGER; item: Boxes.Object), NEW;
|
||||
(ll: LinkedList) IsEmpty (): BOOLEAN, NEW;
|
||||
(ll: LinkedList) Remove (item: Boxes.Object), NEW;
|
||||
(ll: LinkedList) RemoveAt (at: INTEGER), NEW;
|
||||
(ll: LinkedList) Reset, NEW;
|
||||
(ll: LinkedList) Set (at: INTEGER; item: Boxes.Object), NEW
|
||||
END;
|
||||
|
||||
Vector = POINTER TO RECORD
|
||||
size-, cap-: LONGINT;
|
||||
(v: Vector) Add (item: Boxes.Object), NEW;
|
||||
(v: Vector) AddAt (item: Boxes.Object; i: INTEGER), NEW;
|
||||
(v: Vector) Contains (o: Boxes.Object): BOOLEAN, NEW;
|
||||
(v: Vector) Get (i: LONGINT): Boxes.Object, NEW;
|
||||
(v: Vector) IndexOf (o: Boxes.Object): LONGINT, NEW;
|
||||
(v: Vector) Remove (o: Boxes.Object), NEW;
|
||||
(v: Vector) RemoveIndex (i: LONGINT): Boxes.Object, NEW;
|
||||
(v: Vector) Set (i: LONGINT; o: Boxes.Object): Boxes.Object, NEW;
|
||||
(v: Vector) Trim, NEW
|
||||
END;
|
||||
|
||||
PROCEDURE NewHash (cap: INTEGER): Hash;
|
||||
PROCEDURE NewHashMap (cap: INTEGER): HashMap;
|
||||
PROCEDURE NewLinkedList (): LinkedList;
|
||||
PROCEDURE NewVector (cap: INTEGER): Vector;
|
||||
|
||||
END Collections.
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
MODULE BbtAssociativeArrays;
|
||||
IMPORT StdLog, Collections, Boxes;
|
||||
|
||||
PROCEDURE Do*;
|
||||
VAR
|
||||
hm : Collections.HashMap;
|
||||
o : Boxes.Object;
|
||||
keys, values: POINTER TO ARRAY OF Boxes.Object;
|
||||
i: INTEGER;
|
||||
|
||||
BEGIN
|
||||
hm := Collections.NewHashMap(1009);
|
||||
o := hm.Put(Boxes.NewString("first"),Boxes.NewInteger(1));
|
||||
o := hm.Put(Boxes.NewString("second"),Boxes.NewInteger(2));
|
||||
o := hm.Put(Boxes.NewString("third"),Boxes.NewInteger(3));
|
||||
o := hm.Put(Boxes.NewString("one"),Boxes.NewInteger(1));
|
||||
|
||||
StdLog.String("size: ");StdLog.Int(hm.size);StdLog.Ln;
|
||||
|
||||
END Do;
|
||||
|
||||
END BbtAssociativeArrays.
|
||||
|
|
@ -1,8 +1,12 @@
|
|||
#define std'dictionary'*.
|
||||
#define std'collections'*.
|
||||
#define system'collections.
|
||||
|
||||
#symbol Program =
|
||||
// --- Program ---
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
#var map := Dictionary.
|
||||
map append &dictionary_key:"foo" &content:"bar".
|
||||
// 1. Create
|
||||
#var aMap := Dictionary new.
|
||||
aMap @ "key" << "foox".
|
||||
aMap @ "key" << "foo".
|
||||
aMap @ "key2" << "foo2".
|
||||
].
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ stateC.ca='Sacramento'; stateN.ca='California'
|
|||
stateC.nd='Bismarck' ; stateN.nd='North Dakota'
|
||||
stateC.mn='St. Paul' ; stateN.mn='Minnesota'
|
||||
stateC.dc='Washington'; stateN.dc='District of Columbia'
|
||||
stateC.ri='Providence'; stateN.ri='Rhode Island and Providence Plantations
|
||||
stateC.ri='Providence'; stateN.ri='Rhode Island and Providence Plantations'
|
||||
|
||||
say 'capital of California is' stateC.ca
|
||||
say 'capital of Oklahoma is' stateC.ok
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// mutable maps (HashSets)
|
||||
import scala.collection.mutable.HashMap
|
||||
val hash = new HashMap[int, int]
|
||||
val hash = new HashMap[Int, Int]
|
||||
hash(1) = 2
|
||||
hash += (1 -> 2) // same as hash(1) = 2
|
||||
hash += (3 -> 4, 5 -> 6, 44 -> 99)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue