1function StringMap() {
2
3 this.stringToIndex = new Map();
4 this.indexToString = [];
5
6 this.addString = function(str) {
7
8 // If this string is known, just return its index
9 var index = this.stringToIndex.get(str);
10 if (index !== undefined) {
11 return index;
12 }
13
14 // This is a new string. Add it to both maps.
15 index = this.indexToString.length;
16 this.indexToString.push(str);
17 this.stringToIndex.set(str, index);
18 return index;
19 };
20
21 this.getString = function(index) {
22 return this.indexToString[index];
23 };
24
25 this.addStrings = function(strArray) {
26 var indices = [];
27 for (var i=0; i<strArray.length; i++) {
28 var s = strArray[i];
29 var index = this.addString(s);
30 indices.push(index);
31 }
32 return indices;
33 };
34
35 this.getStrings = function(indices) {
36 var strArray = [];
37 for (var i=0; i<indices.length; i++) {
38 var index = indices[i];
39 var str = this.getString(index);
40 strArray.push(str);
41 }
42 return strArray;
43 };
44};
45window.stringMap = new StringMap();
46
47// Adds an array of style strings to the dictionary.
48// Returns an array of indices into the global string dictionary.
49window.packStyles = function(styles) {
50 return window.stringMap.addStrings(styles);
51};
52
53// Gets an array of indices into the string dictionary pointing to style strings.
54// Returns an array containing the original strings.
55window.unpackStyles = function(styles) {
56
57 // Return identiy if styles is undefined or already unpacked
58 var isPacked = styles && (typeof styles[0] == "number");
59 if (!isPacked) {
60 return styles;
61 }
62 return window.stringMap.getStrings(styles);
63};