summaryrefslogtreecommitdiff
path: root/tools/cvs2fossil/lib/id.tcl
blob: 331f9a61b3db24bac39ebaae1f6efb616604b446 (plain) (blame)
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
63
64
# # ## ### ##### ######## #############

## A simple class for handling an in-memory index mapping from
## arbitrary strings to a small numeric id. Can be queried in reverse
## too, returning the string for the id.

## Id's are starting from 1.

# # ## ### ##### ######## #############
## Requirements.

package require Tcl  ; # Runtime.
package require snit ; # OO runtime.

# # ## ### ##### ######## #############
## Implementation.

snit::type ::vc::tools::id {
    # # ## ### ##### ######## #############

    constructor {} {}

    # # ## ### ##### ######## #############
    ## Public API.
    ## - Put data into the index, incl. query for id of key.
    ## - Lookup data for id.

    method put {key} {
	if {[info exists mydata($key)]} { return $mydata($key) }
	incr mycounter

	set mydata($key)   $mycounter
	set myinvert($mycounter) $key

	return $mycounter
    }

    # Explicitly load the database with a mapping.
    method map {id key} {
	set mydata($key)   $id
	set myinvert($id) $key
    }

    method keyof {id} { return $myinvert($id) }
    method get   {}   { return [array get mydata] }

    # # ## ### ##### ######## #############
    ## Internal. State.

    variable mydata   -array {} ; # Map data -> id
    variable myinvert -array {} ; # Map id -> data
    variable mycounter        0 ; # Counter for id generation.

    # # ## ### ##### ######## #############
}

namespace eval ::vc::tools {
    namespace export id
}

# # ## ### ##### ######## #############
## Ready.

package provide vc::tools::id 1.0