Fossil SCM

fossil-scm / tools / cvs2fossil / lib / id.tcl
Blame History Raw 65 lines
1
# # ## ### ##### ######## #############
2
3
## A simple class for handling an in-memory index mapping from
4
## arbitrary strings to a small numeric id. Can be queried in reverse
5
## too, returning the string for the id.
6
7
## Id's are starting from 1.
8
9
# # ## ### ##### ######## #############
10
## Requirements.
11
12
package require Tcl ; # Runtime.
13
package require snit ; # OO runtime.
14
15
# # ## ### ##### ######## #############
16
## Implementation.
17
18
snit::type ::vc::tools::id {
19
# # ## ### ##### ######## #############
20
21
constructor {} {}
22
23
# # ## ### ##### ######## #############
24
## Public API.
25
## - Put data into the index, incl. query for id of key.
26
## - Lookup data for id.
27
28
method put {key} {
29
if {[info exists mydata($key)]} { return $mydata($key) }
30
incr mycounter
31
32
set mydata($key) $mycounter
33
set myinvert($mycounter) $key
34
35
return $mycounter
36
}
37
38
# Explicitly load the database with a mapping.
39
method map {id key} {
40
set mydata($key) $id
41
set myinvert($id) $key
42
}
43
44
method keyof {id} { return $myinvert($id) }
45
method get {} { return [array get mydata] }
46
47
# # ## ### ##### ######## #############
48
## Internal. State.
49
50
variable mydata -array {} ; # Map data -> id
51
variable myinvert -array {} ; # Map id -> data
52
variable mycounter 0 ; # Counter for id generation.
53
54
# # ## ### ##### ######## #############
55
}
56
57
namespace eval ::vc::tools {
58
namespace export id
59
}
60
61
# # ## ### ##### ######## #############
62
## Ready.
63
64
package provide vc::tools::id 1.0
65

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button