/* ----------------------------------------------------------------------------- Copyright 2021 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] // Container with stack semantics. // // Params: // - #x: The value type to stack. @value interface Stack<#x> { refines Container // Pushes a new value and returns self. push (#x) -> (#self) // Pops the top value. Crashes if empty. (Check size() before calling.) pop () -> (#x) } // Container with queue semantics. // // Params: // - #x: The value type to stack. @value interface Queue<#x> { refines Container // Pushes a new value and returns self. push (#x) -> (#self) // Pops the top value. Crashes if empty. (Check size() before calling.) pop () -> (#x) } // Writing to key-value storage. // // Params: // - #k: The key type. // - #v: The value type. @value interface KVWriter<#k, #v|> { // Sets or replaces the value associated with the key and returns self. set (#k, #v) -> (#self) // Removes the value associated with the key if present and returns self. remove (#k) -> (#self) } // Reading from key-value storage. // // Params: // - #k: The key type. // - #v: The value type. @value interface KVReader<#k|#v> { // Returns the value associated with the key if present. get (#k) -> (optional #v) } // Reading keys from key-value storage. @value interface KeyOrder<|#k> { // Returns an iterator for the container's keys. keyOrder () -> (optional Order<#k>) } // Reading values from key-value storage. @value interface ValueOrder<|#v> { // Returns an iterator for the container's values. valueOrder () -> (optional Order<#v>) } // Exchanging values in key-value storage. // // Params: // - #k: The key type. // - #v: The value type. @value interface KVExchange<#k|#v|> { // Replaces the value if it does not exist and returns the final value. weakSet (#k, #v) -> (#v) // Swap the value associated with the key. // // Args: // - #k: Key to look up. // - optional #v: Replacement value. If empty, remove the entry. // // Returns: // - optional #v: Previous value, or empty if none existed. swap (#k, optional #v) -> (optional #v) } // Writing to a set of values. // // Params: // - #k: The type stored in the set. @value interface SetWriter<#k|> { // Adds the value to the set. add (#k) -> (#self) // Removes the value. remove (#k) -> (#self) } // Checking a set of values. // // Params: // - #k: The type stored in the set. @value interface SetReader<#k|> { // Returns true if the set contains the value. member (#k) -> (Bool) } // Factory for creating a new key-value pair. @type interface KVFactory<#k, #v|> { // Create a new pair from the provided key and value. newNode (#k, #v) -> (#self) } // A single key-value pair from a KVReader. @value interface KeyValue<|#k, #v> { // Get the key. // // Notes: // - Calling mutating functions on the key could invalidate the structure of // the KVReader. getKey () -> (#k) // Get the value. getValue () -> (#v) }