~ubuntu-branches/ubuntu/wily/julia/wily

« back to all changes in this revision

Viewing changes to extras/lru.jl

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2013-02-06 17:54:29 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130206175429-13br5kqpkfjqdmre
Tags: 0.0.0+20130206.git32ff5759-1
* New upstream snapshot.
* debian/copyright: reflect upstream changes
* debian/rules: update get-orig-source to reflect upstream changes
   + Don't ship nginx
   + Adapt for new configure-random target in deps/Makefile
* Enable build of Tk wrapper.
   + debian/control: add build dependency on tk-dev
   + debian/rules: add tk rule to build-arch
* debian/julia.install: install VERSION and COMMIT files
* no-webrepl.patch: new patch
* Refresh other patches
* Add source override for config.status file under deps/random/

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# collections, the difference in performance is small, and this implmentation
17
17
# is simpler and easier to understand.
18
18
 
19
 
import Base.isempty, Base.numel, Base.length, Base.sizeof
 
19
import Base.isempty, Base.length, Base.sizeof
20
20
import Base.start, Base.next, Base.done
21
21
import Base.has, Base.get
22
 
import Base.assign, Base.ref, Base.del, Base.del_all
 
22
import Base.assign, Base.ref, Base.delete!, Base.empty!
23
23
import Base.show
24
24
 
25
25
abstract LRU{K,V} <: Associative{K,V}
54
54
## collections ##
55
55
 
56
56
isempty(lru::LRU) = isempty(lru.q)
57
 
numel(lru::LRU) = numel(lru.q)
58
57
length(lru::LRU) = length(lru.q)
59
58
has(lru::LRU, key) = has(lru.ht, key)
60
59
 
65
64
 
66
65
get(lru::LRU, key, default) = has(lru, key) ? lru[key] : default
67
66
 
68
 
function del_all(lru::LRU)
69
 
    del_all(lru.ht)
70
 
    del_all(lru.q)
 
67
function empty!(lru::LRU)
 
68
    empty!(lru.ht)
 
69
    empty!(lru.q)
71
70
end
72
71
 
73
72
 
74
 
show(io, lru::UnboundedLRU) = print("UnboundedLRU()")
75
 
show(io, lru::BoundedLRU) = print("BoundedLRU($(lru.maxsize))")
 
73
show(io::IO, lru::UnboundedLRU) = print(io,"UnboundedLRU()")
 
74
show(io::IO, lru::BoundedLRU) = print(io,"BoundedLRU($(lru.maxsize))")
76
75
 
77
76
## indexable ##
78
77
 
89
88
function ref(lru::LRU, key)
90
89
    item = lru.ht[key]
91
90
    idx = locate(lru.q, item)
92
 
    del(lru.q, idx)
93
 
    enqueue(lru.q, item)
 
91
    delete!(lru.q, idx)
 
92
    unshift!(lru.q, item)
94
93
    item.v
95
94
end
96
95
 
99
98
        item = lru.ht[key]
100
99
        idx = locate(lru.q, item)
101
100
        item.v = v
102
 
        del(lru.q, idx)
 
101
        delete!(lru.q, idx)
103
102
    else
104
103
        item = CacheItem(key, v)
105
104
        lru.ht[key] = item
106
105
    end
107
 
    enqueue(lru.q, item)
 
106
    unshift!(lru.q, item)
108
107
end
109
108
 
110
109
# Eviction
112
111
    invoke(assign, (LRU, V, K), lru, v, key)
113
112
    nrm = length(lru) - lru.maxsize
114
113
    for i in 1:nrm
115
 
        rm = pop(lru.q)
116
 
        del(lru.ht, rm.k)
 
114
        rm = pop!(lru.q)
 
115
        delete!(lru.ht, rm.k)
117
116
    end
118
117
end
119
118
 
120
119
## associative ##
121
120
 
122
 
function del(lru::LRU, key)
 
121
function delete!(lru::LRU, key)
123
122
    item = lru.ht[key]
124
123
    idx = locate(lru.q, item)
125
 
    del(lru.ht, key)
126
 
    del(lru.q, idx)
 
124
    delete!(lru.ht, key)
 
125
    delete!(lru.q, idx)
127
126
end