hash utilities #289

Parent #72Owner #361Flags readSource hellcore/hellcore.db

Aliases: hash utilities

5 verbs · 11 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
to_alist flattenthis none thisrxd#28913
topNthis none thisrxd#28930
bottomNthis none thisrxd#28930
slicethis none thisrxd#2899
mergethis none thisrxd#28914

Properties

PropertyDefinerFlagsOwnerValue
help_msg#72rc#361
list of 11{"This is a set of utilities for parsing and manipulating hashes. See also 'help hashes'.", " ", " :topN(h, ?n = 10) => unsorted list of top n keys, by value", " :bottomN(h, ?n = 10) => unsorted list of bottom n keys, by value", "", " :slice(h, ?i = 1) => $lu:slice(values(h), i)", " :merge(h1, h2, ...) => new hash with all keys/values", "", " :to_alist(h) => {{key1, value1}, {key2, value2}, ...}", " (aka :flatten)", ""}
aliases#1rc#361{"hash utilities"}
description#1rc#361<clear>
object_size#1r#29{3858, 1298434110}
hidden_verbs#1rc#361<clear>
phelp_msg#1rc#361<clear>
weight#1rc#361<clear>
owner_verbs#1rc#361<clear>
plural_name#1rc#361<clear>
client_image#1rc#361<clear>
listening#1rc#361<clear>

Ancestry

Ancestors (nearest first): #72 Generic Utilities Package#1 root

Children: none

Call graph

calls n289_1 #289:topN n289_0 #289:to_alist n289_1->n289_0 n289_2 #289:bottomN n289_2->n289_0 n289_3 #289:slice n47_16 #47:slice n289_3->n47_16

Source

to_alist flatten

Spec this none thisFlags rxdOwner #361Definer #289

Referenced by

Source

1":to_alist(hash) => List of keys and values e.g. {{key1, value1}, {key2, value2}, ...}";
2{h, ?sublist = 0} = args;
3l = {};
4"kvp = key value pair";
5for kvp in (h)
6{k, v} = kvp;
7if (sublist)
8l = {@l, {k, @v}};
9else
10l = {@l, {k, v}};
11endif
12endfor
13return l;

topN

Spec this none thisFlags rxdOwner #361Definer #289

Referenced by

Source

1":topN(HASH h [, INT n]) the top n keys, by value, of h.";
2{h, ?n = 10, ?ytime = 0} = args;
3if (length(h) <= n)
4return this:to_alist(h);
5endif
6topn = {};
7for entry in (h)
8{key, value} = entry;
9counted = length(topn);
10i = counted;
11while (i)
12yield ytime;
13if (value > topn[i][2])
14break;
15else
16i = i - 1;
17endif
18endwhile
19if (i)
20if (counted == n)
21topn = {@topn[2..i], entry, @topn[i + 1..$]};
22else
23topn = {@topn[1..i], entry, @topn[i + 1..$]};
24endif
25elseif (counted < n)
26topn = {entry, @topn};
27endif
28yield ytime;
29endfor
30return topn;

bottomN

Spec this none thisFlags rxdOwner #361Definer #289

Referenced by

Source

1":bottomN(HASH h [, INT n]) the bottom n keys, by value, of h.";
2{h, ?n = 10, ?ytime = 0} = args;
3if (length(h) <= n)
4return this:to_alist(h);
5endif
6bottomn = {};
7for entry in (h)
8{key, value} = entry;
9counted = length(bottomn);
10i = counted;
11while (i)
12yield ytime;
13if (value < bottomn[i][2])
14break;
15else
16i = i - 1;
17endif
18endwhile
19if (i)
20if (counted == n)
21bottomn = {@bottomn[2..i], entry, @bottomn[i + 1..$]};
22else
23bottomn = {@bottomn[1..i], entry, @bottomn[i + 1..$]};
24endif
25elseif (counted < n)
26bottomn = {entry, @bottomn};
27endif
28yield ytime;
29endfor
30return bottomn;

slice

Spec this none thisFlags rxdOwner #361Definer #289

Referenced by

none

Source

1"slice(hash[,index]) returns a list of the index-th elements of the elements of hash, e.g., ";
2"    slice([\"foo\" -> {\"z\",1}, \"bar\" -> {\"y\",2}, \"baz\" -> {\"x\",5}], 2) => {1, 2, 5}.";
3"index defaults to 1 and may also be a nonempty list, e.g., ";
4"    slice({{\"z\",1,3},{\"y\",2,4}},{2,1}) => {{1,\"z\"},{2,\"y\"}}";
5{thehash, ?ind = 1} = args;
6if (typeof(thehash) != HASH)
7raise(E_TYPE);
8endif
9return $lu:slice(values(thehash), ind);

merge

Spec this none thisFlags rxdOwner #361Definer #289

Referenced by

Source

1":merge(HASH a, HASH b, ...) -> HASH with keys/values from all given hashes. If there are duplicate keys, the values from later hashes override values from earlier hashes in the arg list.";
2result = [];
3for h in (args)
4if (typeof(h) != HASH)
5raise(E_TYPE);
6endif
7if (!h)
8continue;
9endif
10for kv in (h)
11result[kv[1]] = kv[2];
12endfor
13endfor
14return result;