1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/* <license>
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
* For full copyright and license information please refer to doc/license.txt.
* </license>
*/
class HashUtil
{
/**
* Converts an array into a hash, the structure of the array should be:
* [
* [ name, item ],
* ....
* ]
*/
static public function fromArray<T>( arr : Array<Array<Dynamic>> ) : Hash<T>
{
var r = new Hash<T>();
for( i in arr )
r.set( i[0], i[1] ); //HAXE: although it would be nice, a cast( i[1], T ) ) is not allowed here
return r;
}
/**
* Creates a new hash as the result of merging two other hashes.
*
* @param prime [in] in the case of conflict, values in this class will be used
* @param second [in] in conflict, these values are lost
*/
static public function merge<T>( prime : Hash<T>, second : Hash<T> ) : Hash<T>
{
var r = new Hash<T>();
for( k in second.keys() )
r.set( k, second.get(k));
for( k in prime.keys() )
r.set( k, prime.get(k));
return r;
}
/**
* A variant of fromArray that takes a 1-dimensional array and assigns all
* items in the hash a single value.
*/
static public function fromArrayValue<T>( arr : Array<String>, value : T ) : Hash<T>
{
var r = new Hash<T>();
for( k in arr )
r.set( k, value );
return r;
}
/**
* Inverts the key/value pairs in a Hash. If several values are identical
* then the resulting Hash will have less entries than the original.
*/
static public function invert( hash : Hash<String> ) : Hash<String>
{
var r = new Hash<String>();
for( key in hash.keys() )
r.set( hash.get(key), key );
return r;
}
}
|