UNPKG

1.06 kBJavaScriptView Raw
1/* @flow weak */
2"use strict";
3
4var utils = require("./utils.js");
5
6/*
7 #### FMap (eq : a -> a -> bool) : FMap a
8
9 Finite map, with any object a key.
10
11 Short summary of member functions:
12
13 - FMap.insert (key : a) (value : any) : void
14 - FMap.get (key : a) : any
15 - FMap.contains (key : a) : obool
16*/
17function FMap(eq) {
18 this.eq = eq || utils.isEqual;
19 this.data = [];
20}
21
22FMap.prototype.contains = function FMapContains(key) {
23 for (var i = 0; i < this.data.length; i++) {
24 if (this.eq(this.data[i][0], key)) {
25 return true;
26 }
27 }
28
29 return false;
30};
31
32FMap.prototype.insert = function FMapInsert(key, value) {
33 for (var i = 0; i < this.data.length; i++) {
34 if (this.eq(this.data[i][0], key)) {
35 this.data[i] = [key, value];
36 return;
37 }
38 }
39
40 this.data.push([key, value]);
41};
42
43FMap.prototype.get = function FMapGet(key) { // eslint-disable-line consistent-return
44 for (var i = 0; i < this.data.length; i++) {
45 if (this.eq(this.data[i][0], key)) {
46 return this.data[i][1];
47 }
48 }
49};
50
51module.exports = FMap;