/* ----------------------------------------------------------------------------- Copyright 2019-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] define TypeMap { @value SortedMap,GenericValue> map default () { return new() } new () { return TypeMap{ SortedMap,GenericValue>.new() } } duplicate () { return #self{ map.duplicate() } } set (k,v) { \ map.set(k,GenericValue:create(v)) return self } remove (k) { \ map.remove(k) return self } get (k) { scoped { optional GenericValue value <- map.get(k) } in if (present(value)) { return require(value).check<#x>() } else { return empty } } getAll (output) { traverse (map.defaultOrder() -> KeyValue> pair) { scoped { optional #x value <- pair.getValue().check<#x>() } in if (present(value)) { \ output.append(require(value)) } } return output } } define TypeKey { $ReadOnlyExcept[counter]$ @category Mutex counterMutex <- SpinlockMutex.new() @category Int counter <- 0 @value Int index new () { scoped { \ counterMutex.lock() } cleanup { \ counterMutex.unlock() } in return TypeKey<#x>{ (counter <- counter+1) } } formatted () { return String.builder() .append(typename<#self>()) .append("{") .append(index) .append("}") .build() } hashed () { return index.hashed() } lessThan (l,r) { return l.get() < r.get() } equals (l,r) { return l.get() == r.get() } @value get () -> (Int) get () { return index } } concrete GenericValue<|#x> { @category create<#x> (#x) -> (GenericValue<#x>) @value check<#y> () -> (optional #y) } define GenericValue { $ReadOnlyExcept[]$ @value #x value create (v) { return GenericValue<#x>{ v } } check () { return reduce<#x,#y>(value) } }