// Compute a hash for objects of any type. // // The way it's currently implemented, two objects will have the same hash if // all their non-address fields (all the way down) expand to the same sequence // of scalar values. In particular, a container with all zero addresses hashes // to 0. Hopefully this won't be an issue because we are usually hashing // objects of a single type in any given hash table. // // Based on http://burtleburtle.net/bob/hash/hashfaq.html :(before "End Primitive Recipe Declarations") HASH, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "hash", HASH); :(before "End Primitive Recipe Checks") case HASH: { if (SIZE(inst.ingredients) != 1) { raise << maybe(get(Recipe, r).name) << "'hash' takes exactly one ingredient rather than '" << to_original_string(inst) << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case HASH: { const reagent& input = current_instruction().ingredients.at(0); products.resize(1); products.at(0).push_back(hash(0, input)); break; } //: in all the code below, the intermediate results of hashing are threaded through 'h' :(code) size_t hash(size_t h, reagent/*copy*/ r) { canonize(r); if (is_mu_text(r)) // optimization return hash_mu_text(h, r); else if (is_mu_address(r)) return hash_mu_address(h, r); else if (is_mu_scalar(r)) return hash_mu_scalar(h, r); else if (is_mu_array(r)) return hash_mu_array(h, r); else if (is_mu_container(r)) return hash_mu_container(h, r); else if (is_mu_exclusive_container(r)) return hash_mu_exclusive_container(h, r); assert(false); } size_t hash_mu_scalar(size_t h, const reagent& r) { double input = is_literal(r) ? r.value : get_or_insert(Memory, r.value); return hash_iter(h, static_cast(input)); } size_t hash_mu_address(size_t h, reagent& r) { if (r.value == 0) return 0; trace(Callstack_depth+1, "mem") << "location " << r.value << " is " << no_scientific(get_or_insert(Memory, r.value)) << end(); r.set_value(get_or_insert(Memory, r.value)); drop_from_type(r, "address"); return hash(h, r); } size_t hash_mu_text(size_t h, const reagent& r) { string input = read_mu_text(get_or_insert(Memory, r.value+/*skip alloc id*/1)); for (int i = 0; i < SIZE(input); ++i) { h = hash_iter(h, static_cast(input.at(i))); //? cerr << i << ": " << h << '\n'; } return h; } size_t hash_mu_array(size_t h, const reagent& r) { int size = get_or_insert(Memory, r.value); reagent/*copy*/ elem = r; delete elem.type; elem.type = copy_array_element(r.type); for (int i=0, address = r.value+1; i < size; ++i, address += size_of(elem)) { reagent/*copy*/ tmp = elem; tmp.set_value(address); h = hash(h, tmp); //? cerr << i << " (" << address << "): " << h << '\n'; } return h; } size_t hash_mu_container(size_t h, const reagent& r) { type_info& info = get(Type, get_base_type(r.type)->value); int address = r.value; int offset = 0; for (int i = 0; i < SIZE(info.elements); ++i) { reagent/*copy*/ element = element_type(r.type, i); if (has_property(element, "ignore-for-hash")) continue; element.set_value(address+offset); h = hash(h, element); //? cerr << i << ": " << h << '\n'; offset += size_of(info.elements.at(i).type); } return h; } size_t hash_mu_exclusive_container(size_t h, const reagent& r) { const type_tree* type = get_base_type(r.type); assert(type->value); int tag = get(Memory, r.value); reagent/*copy*/ variant = variant_type(r, tag); // todo: move this error to container definition time if (has_property(variant, "ignore-for-hash")) raise << get(Type, type->value).name << ": /ignore-for-hash won't work in exclusive containers\n" << end(); variant.set_value(r.value + /*skip tag*/1); h = hash(h, variant); return h; } size_t hash_iter(size_t h, size_t input) { h += input; h += (h<<10); h ^= (h>>6); h += (h<<3); h ^= (h>>11); h += (h<<15); return h; } void test_hash_container_checks_all_elements() { run( "container foo [\n" " x:num\n" " y:char\n" "]\n" "def main [\n" " 1:foo <- merge 34, 97/a\n" " 3:num <- hash 1:foo\n" " return-unless 3:num\n" " 4:foo <- merge 34, 98/a\n" " 6:num <- hash 4:foo\n" " return-unless 6:num\n" " 7:bool <- equal 3:num, 6:num\n" "]\n" ); // hash on containers includes all elements CHECK_TRACE_CONTENTS( "mem: storing 0 in location 7\n" ); } void test_hash_exclusive_container_checks_all_elements() { run( "exclusive-container foo [\n" " x:bar\n" " y:num\n" "]\n" "container bar [\n" " a:num\n" " b:num\n" "]\n" "def main [\n" " 1:foo <- merge 0/x, 34, 35\n" " 4:num <- hash 1:foo\n" " return-unless
# Tested with ranger 1.7.2
#
# This plugin creates a bunch of keybindings used to mount and unmount
# the devices using pmount(1).
#
# alt+m       <letter>            <digit>: mount /dev/sd<letter><digit>
# alt+m       <uppercase letter>         : mount /dev/sd<letter>
# alt+shift+m <letter>            <digit>: unmount /dev/sd<letter><digit>
# alt+shift+m <uppercase letter>         : unmount /dev/sd<letter>
# alt+shift+n                            : list the devices

import ranger.api

MOUNT_KEY = '<alt>m'
UMOUNT_KEY = '<alt>M'
LIST_MOUNTS_KEY = '<alt>N'

old_hook_init = ranger.api.hook_init
def