99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: ## A simple class for handling an in-memory index mapping from 99e165d5c4 2007-10-21 aku: ## arbitrary strings to a small numeric id. Can be queried in reverse 99e165d5c4 2007-10-21 aku: ## too, returning the string for the id. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: ## Id's are starting from 1. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: ## Requirements. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: package require Tcl ; # Runtime. 99e165d5c4 2007-10-21 aku: package require snit ; # OO runtime. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: ## Implementation. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: snit::type ::vc::tools::id { 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: constructor {} {} 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: ## Public API. 99e165d5c4 2007-10-21 aku: ## - Put data into the index, incl. query for id of key. 99e165d5c4 2007-10-21 aku: ## - Lookup data for id. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: method put {key} { 99e165d5c4 2007-10-21 aku: if {[info exists mydata($key)]} { return $mydata($key) } 99e165d5c4 2007-10-21 aku: incr mycounter 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: set mydata($key) $mycounter 99e165d5c4 2007-10-21 aku: set myinvert($mycounter) $key 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: return $mycounter 99e165d5c4 2007-10-21 aku: } 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: method keyof {id} { return $myinvert($id) } 89e9b357ed 2007-10-25 aku: method get {} { return [array get mydata] } 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: ## Internal. State. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: variable mydata -array {} ; # Map data -> id 99e165d5c4 2007-10-21 aku: variable myinvert -array {} ; # Map id -> data 99e165d5c4 2007-10-21 aku: variable mycounter 0 ; # Counter for id generation. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: } 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: namespace eval ::vc::tools { 99e165d5c4 2007-10-21 aku: namespace export id 99e165d5c4 2007-10-21 aku: } 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: # # ## ### ##### ######## ############# 99e165d5c4 2007-10-21 aku: ## Ready. 99e165d5c4 2007-10-21 aku: 99e165d5c4 2007-10-21 aku: package provide vc::tools::id 1.0