Abstract parameterized class for collections that associate a key with an element.
Description
Notes
| Gets or sets the efficient-size of self. |
| Checks if the collection is empty. |
| Returns the data type of the keys. |
| Returns the number of elements in the collection. |
| Deletes all elements. |
| Returns a clone of the collection. |
| Filters elements, using the elements themselves in the filter operation. |
| Returns a clone of the collection, with elements filtered out (using the elements themselves in the filter operation). |
| Filters elements, using the keys in the filter operation. |
| Returns a clone of the collection, with elements filtered out (using the keys in the filter operation). |
| Returns a specific element. |
| Returns the element indexed by key, along with a boolean that signifies whether the indicated element was found. |
| Returns a specific key. |
| Returns a specific key, along with a boolean that signifies whether the indicated key was found. |
| Check if a key exists. |
| Returns an |
| Removes an element. |
| Sets the value of an element. |
Gets or sets the efficient-size of self.
Description
efficiently. This is defined on an implementation-basis, but will generally reflect the size of underlying data structures. Growth beyond their allocated sizes will require allocating new underlying data structures and copying data from the old structures; this is what is considered
inefficient.
Notes
Checks if the collection is empty.
Returns
Example
| Example | |
|| Declare and initialize an empty hash table.
{let my-table:{HashTable-of int, String} =
{new {HashTable-of int, String}}}
|| Check if the hash table is empty and display an
|| appropriate message.
{if my-table.empty? then
{text The hash table is empty!}
else
{text The hash table has elements!}
}
|
| Example | |
|| Declare and initialize a hash table with
|| int keys and String elements.
{let my-table:{HashTable-of int, String} =
{new {HashTable-of int, String},
1, "apple"
}
}
|| Check if the hash table is empty and display an
|| appropriate message.
{if my-table.empty? then
{text The hash table is empty!}
else
{text The hash table has elements!}
}
|
Returns the data type of the keys.
Returns
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements
let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
|| Display the data type of key in price.
price.key-type
}
|
Returns the number of elements in the collection.
Returns
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
|| Display a message indicating the size of
|| the hash table.
{text There are {value price.size} elements in
the hash table.}
}
|
Notes
Deletes all elements.
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| int keys and String elements.
let my-table:{HashTable-of int, String} =
{new {HashTable-of int, String},
162094, "tom",
439853, "dick",
098627, "harry"
}
|| Clear the hash table.
{my-table.clear}
|| Check if the hash table is empty.
{text The assertion that the hash table is empty is...
{value my-table.empty?}}
}
|
Returns a clone of the collection.
Returns
Description
Example
| Example | |
{value
|| Declare and initialize set-1 (the original set).
let set-1:{Set-of String} =
{new {Set-of String}, "apple", "banana", "cherry"}
|| Initialize set-2 with a clone of the contents of
|| set-1.
let set-2:{Set-of String} = {set-1.clone}
|| Use a VBox to display the contents of set-2.
|| Iterate over the contents of set-2, adding them
|| to the VBox. Then display the VBox.
let message:VBox = {VBox}
{for each-element:String in set-2 do
{message.add each-element}
}
message
}
|
Notes
Notes
Filters elements, using the elements themselves in the filter operation.
Returns
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| int keys and String elements.
let my-table:{HashTable-of int, String} =
{new {HashTable-of int, String},
162094, "tom",
439853, "dick",
098627, "harry"
}
|| Filter elements that begin with the
|| letter 'd'.
{my-table.filter
{proc {str:String}:bool
{return str[0] != 'd'}
}
}
|| Use a VBox to display the contents of my-table.
|| Add each element to the VBox and then display it.
let message:VBox = {VBox}
{for each-element:String in my-table do
{message.add each-element}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Returns a clone of the collection, with elements filtered out (using the elements themselves in the filter operation).
Returns
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| int keys and String elements.
let my-table-1:{HashTable-of int, String} =
{new {HashTable-of int, String},
162094, "tom",
439853, "dick",
098627, "harry"
}
|| Create a clone my-table-2 that contains the elements
|| of my-table-1 with strings that begin with the letter
|| 'd' filtered out.
let my-table-2:{HashTable-of int, String} =
{my-table-1.filter-clone
{proc {str:String}:bool
{return str[0] != 'd'}
}
}
|| Use a VBox to display the contents of my-table-2.
|| Iterate over the contents of my-table-2, adding
|| them to the VBox. Then display the VBox.
let message:VBox = {VBox}
{for each-element:String in my-table-2 do
{message.add each-element}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Notes
Filters elements, using the keys in the filter operation.
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let quantity:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 3,
"banana", 0,
"cherry", 8
}
|| Filter elements whose keys begin with 'a'.
{quantity.filter-keys
{proc {str:String}:bool
{return str[0] != 'a'}
}
}
|| Use a VBox to display the contents of quantity.
|| For each key in quantity, add the key to the VBox.
|| Then display the VBox.
let message:VBox = {VBox}
{for key each-element:String in quantity do
{message.add each-element}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Returns a clone of the collection, with elements filtered out (using the keys in the filter operation).
Returns
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let quantity:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 3,
"banana", 0,
"cherry", 8
}
|| Create a clone that contains the elements of the
|| original with keys that begin with the letter
|| 'a' filtered out.
let new-quantity:{HashTable-of String, int} =
{quantity.filter-keys-clone
{proc {str:String}:bool
{return str[0] != 'a'}
}
}
|| Use a VBox to display the contents of quantity.
|| For each key in quantity, add the key to the VBox.
|| Then display the VBox.
let message:VBox = {VBox}
{for key each-element:String in new-quantity do
{message.add each-element}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Notes
Returns a specific element.
Returns
Throws
Should throwDescription
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
|| Check for the presence of "banana" and "cherry",
|| showing how [] syntax can be used to call "get"
|| methods.
{text The price of bananas is {price.get "banana"},
the price of cherries is {value price["cherry"]}.}
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Returns the element indexed by key, along with a boolean that signifies whether the indicated element was found.
Returns
Returns a specific key.
Returns
Description
Example
| Example | |
|| Declare and initialize a hash table with
|| String keys and int elements.
{let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
}
|| Use the get-key method to get the original keys.
The hash table contains prices for...
{price.get-key "apple"}
{price.get-key "banana"}
{price.get-key "cherry"}
|
Returns a specific key, along with a boolean that signifies whether the indicated key was found.
Returns
Description
Example
| Example | |
|| Declare and initialize a hash table with
|| String keys and int elements.
{let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
}
|| Use the get-key-if-exists method to check for the
|| presence of keys.
The hash table contains prices for...
{price.get-key-if-exists "apple"}
{price.get-key-if-exists "banana"}
{price.get-key-if-exists "pear"}
{price.get-key-if-exists "cherry"}
{price.get-key-if-exists "orange"}
|
Notes
Check if a key exists.
Returns
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
|| Check if there is an element with the
|| key "banana" in the collection "price".
{if {price.key-exists? "banana"} then
{text It is there!}
else
{text It is not there.}
}
}
|
Returns an
Returns
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| int keys and String elements.
let my-table:{HashTable-of int, String} =
{new {HashTable-of int, String},
162094, "tom",
439853, "dick",
098627, "harry"
}
|| Create an Iterator-of from the set.
let my-iterator:{Iterator-of int} =
{my-table.keys-to-Iterator}
|| Use a VBox to display the contents of my-iterator.
|| Iterate over the contents of my-iterator, adding
|| them to the VBox. Then display the VBox.
let message:VBox = {VBox}
{for each-element:int in my-iterator do
{message.add each-element}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Notes
Notes
Removes an element.
Description
Example
| Example | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
|| Remove the element at key "banana".
{price.remove "banana"}
|| Use a VBox to display the contents of price.
|| For each key in price, add a string to the VBox.
|| The string contains the relevant key and element.
|| Then display the VBox.
let message:VBox = {VBox}
{for key each-element:String in price do
{message.add each-element & " " & {price.get each-element}}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Notes
Sets the value of an element.
Description
Example
| Example | |
![]() | |
{value
|| Declare and initialize a hash table with
|| String keys and int elements.
let price:{HashTable-of String, int} =
{new {HashTable-of String, int},
"apple", 56,
"banana", 87,
"cherry", 34
}
|| Change the element at key "banana".
{price.set "banana", 72}
|| Add an element for "pear".
{price.set "pear", 62}
|| Use a VBox to display the contents of price.
|| For each key in price, add a string to the VBox.
|| The string contains the relevant key and element.
|| Then display the VBox.
let message:VBox = {VBox}
{for key each-element:String in price do
{message.add each-element & " " & {price.get each-element}}
}
message
|| Note that the order of the elements in a hash
|| table is arbitrary.
}
|
Notes