Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh |
2 | * Jagannathan, and Stephen Weeks. | |
3 | * | |
4 | * MLton is released under a BSD-style license. | |
5 | * See the file MLton-LICENSE for details. | |
6 | *) | |
7 | ||
8 | structure StringMap: STRING_MAP = | |
9 | struct | |
10 | ||
11 | datatype 'a t = T of {map: {name: string, | |
12 | value: 'a} list ref, | |
13 | default: unit -> 'a} | |
14 | ||
15 | fun new default = T{map = ref [], default = default} | |
16 | ||
17 | fun clear (T {map, ...}) = map := [] | |
18 | ||
19 | fun lookup (T {map, default}, name) = | |
20 | case List.peek (!map, fn {name = name', ...} => name = name') of | |
21 | NONE => let | |
22 | val value = default () | |
23 | in List.push (map, {name = name, value = value}) | |
24 | ; value | |
25 | end | |
26 | | SOME {value, ...} => value | |
27 | ||
28 | fun domain (T {map, ...}) = List.revMap (!map, fn {name, ...} => name) | |
29 | ||
30 | fun keepAll (T{map, ...}, pred) = | |
31 | List.keepAllMap (!map, fn {name, value} => | |
32 | if pred value then SOME name else NONE) | |
33 | ||
34 | fun foreach (T{map, ...}, f) = | |
35 | List.foreach (!map, fn {value, ...} => f value) | |
36 | ||
37 | end |