Changeset f35805c2c56c0dc7b89bf5c8b341f450093fa7c9

User picture

Commiter: Charles Childers

Author: Charles Childers

Parent: e42c7ed451

(2010/03/09 00:08) Almost 2 years ago

keys stuff from Marc to data/aa.retro

Affected files

Updated library/data/aa.retro Download diff

e42c7ed4519520155d7f614052f669b4479ad9d0f35805c2c56c0dc7b89bf5c8b341f450093fa7c9
4
( Copyright [c] 2010, Marc Simpson                             )
4
( Copyright [c] 2010, Marc Simpson                             )
5
( License: ISC                                                 )
5
( License: ISC                                                 )
6
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
6
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
7
( NOTE:                                                        )
8
( -----                                                        )
9
(                                                              )
10
( Since altering 'hash-prime' would affect previously written  )
11
( tables, we shall introduce the following condition: you may  )
12
( modify 'hash-prime' before creating hash-tables, but once    )
13
( this has been done, do NOT change its value afterward.       )
14
(                                                              )
15
( Of course, we could make 'hash' more flexible and have it    )
16
( read a prime value [or define a second word that respects a  )
17
( stack value for modding].  For now, let's just regard our    )
18
( stipulation as well justified; either tweak the provided     )
19
( hashing routines _before_ use, or leave them be.             )
20
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
7
21
8
include library/data/ll.retro
22
include library/data/ll.retro
9
include library/data/hash.retro
23
include library/data/hash.retro
24
include library/data/arrays.retro
10
25
11
' hashing open
26
' hashing open
12
27
...
...
47
  : @"   (  t"- ) compiler @ if
62
  : @"   (  t"- ) compiler @ if
48
                  ahead " keepString here rot ! literal, else "
63
                  ahead " keepString here rot ! literal, else "
49
                  then ` swap ` get-entry ; immediate
64
                  then ` swap ` get-entry ; immediate
65
66
  : 'type'   ( $- ) char: ' emit type char: ' emit space ;
67
68
  {{
69
    : bkt-keys ( l- ) repeat 0; dup 1+ @ @ , @ again drop ;
70
    : (keys)   ( t- ) hash-prime @ for @+ bkt-keys next drop ;
71
    ---reveal---
72
    : keys  ( t-a )
73
      here 0 , swap  (keys)  here over - 1- over ! ;
74
  }}
75
76
  : keys{ ( R: t- ) ` keys  ` dup ` push `  foreach{ ; compile-only
77
  : }keys ( R:  - ) ` }loop ` pop ` here ` - ` allot ; compile-only
78
79
  : .keys ( t-  )
80
    char: { emit space  keys{ 'type' }keys  char: } emit ;
81
82
  ( xyz -> xyzx: like 'rot', but _copies_ rotated item )
83
  : rover ` push ` over ` pop ` swap ; compile-only
84
85
  : .table ( t- )
86
    dup
87
    keys{ dup cr 'type' ." => " rover get-entry drop . }keys
88
    drop ;
50
))
89
))
51
90
52
' hashing shut
91
' hashing shut

Updated library/data/hash.retro Download diff

e42c7ed4519520155d7f614052f669b4479ad9d0f35805c2c56c0dc7b89bf5c8b341f450093fa7c9
19
( need to allocate too much heap.  This can be adjusted by     )
19
( need to allocate too much heap.  This can be adjusted by     )
20
( revectoring 'hash' in the [unlikely] event of large tables.  )
20
( revectoring 'hash' in the [unlikely] event of large tables.  )
21
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
21
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
22
( NOTE:                                                        )
23
( -----                                                        )
24
(                                                              )
25
( Since altering 'hash-prime' would affect previously written  )
26
( tables, we shall introduce the following condition: you may  )
27
( modify 'hash-prime' before creating hash-tables, but once    )
28
( this has been done, do NOT change its value afterward.       )
29
(                                                              )
30
( Of course, we could make 'hash' more flexible and have it    )
31
( read a prime value [or define a second word that respects a  )
32
( stack value for modding].  For now, let's just regard our    )
33
( stipulation as well justified; either tweak the provided     )
34
( hashing routines _before_ use, or leave them be.             )
35
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
22
36
23
vocab hashing
37
vocab hashing
24
((
38
((

Updated test/library/hash_and_aa/aa-test.retro Download diff

e42c7ed4519520155d7f614052f669b4479ad9d0f35805c2c56c0dc7b89bf5c8b341f450093fa7c9
38
      %h1 @" not there!" .? cr
38
      %h1 @" not there!" .? cr
39
    ;
39
    ;
40
  }}
40
  }}
41
42
    : new-hash: new-hash constant ;
43
    new-hash: h1
44
    new-hash: h2
45
46
    h1 %{ x = 10 }%
47
    h1 %{ y = 20 }%
48
    h1 %{ z = 30 }%
49
    h1 %{ more = " hello" keepString }%
50
51
    ( Be careful with visual alignment; leading spaces are   )
52
    ( part of the key string...                              )
53
    h2   %{ one = 1 }%
54
    h2   %{ two = 2 }%
55
    h2 %{ three = 3 }%
56
57
    ( NOTE: these operations are expensive: )
58
    : t1 h1 .keys ;
59
    : t2 h1 .table ;
60
    : t3 h2 .keys ;
61
    : t4 h2 .table ;
41
))
62
))
42
63
43
( ~~[ Testing ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
64
( ~~[ Testing ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
44
65
45
clear aa.tests
66
clear aa.tests t1 t2 t3 t4
46
67
47
' associative-tests shut
68
' associative-tests shut
48
' associative shut
69
' associative shut