Frank Shearar uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-fbs.544.mcz ==================== Summary ==================== Name: Collections-fbs.544 Author: fbs Time: 24 November 2013, 12:47:44.329 pm UUID: 15771139-bae8-3347-8d63-a02d324e1e48 Ancestors: Collections-nice.543 Move LRUCache from System to Collections, because that's a more sensible home. This also has the nice property that it breaks the Balloon->System dependency. =============== Diff against Collections-nice.543 =============== Item was changed: SystemOrganization addCategory: #'Collections-Abstract'! SystemOrganization addCategory: #'Collections-Arrayed'! + SystemOrganization addCategory: #'Collections-Cache'! SystemOrganization addCategory: #'Collections-Exceptions'! SystemOrganization addCategory: #'Collections-Sequenceable'! SystemOrganization addCategory: #'Collections-Stack'! SystemOrganization addCategory: #'Collections-Streams'! SystemOrganization addCategory: #'Collections-Strings'! SystemOrganization addCategory: #'Collections-Support'! SystemOrganization addCategory: #'Collections-Text'! SystemOrganization addCategory: #'Collections-Unordered'! SystemOrganization addCategory: #'Collections-Weak'! Item was added: + Object subclass: #LRUCache + instanceVariableNames: 'size factory calls hits values' + classVariableNames: '' + poolDictionaries: '' + category: 'Collections-Cache'! + + !LRUCache commentStamp: '<historical>' prior: 0! + I'm a cache of values, given a key I return a Value from the cache or from the factory! Item was added: + ----- Method: LRUCache class>>size:factory: (in category 'instance creation') ----- + size: aNumber factory: aBlock + "answer an instance of the receiver" + ^ self new initializeSize: aNumber factory: aBlock! Item was added: + ----- Method: LRUCache class>>test (in category 'testing') ----- + test + " + LRUCache test + " + | c | + c := LRUCache + size: 5 + factory: [:key | key * 2]. + c at: 1. + c at: 2. + c at: 3. + c at: 4. + c at: 1. + c at: 5. + c at: 6. + c at: 7. + c at: 8. + c at: 1. + ^ c! Item was added: + ----- Method: LRUCache class>>test2 (in category 'testing') ----- + test2 + " + LRUCache test2. + Time millisecondsToRun:[LRUCache test2]. + MessageTally spyOn:[LRUCache test2]. + " + | c | + c := LRUCache + size: 600 + factory: [:key | key * 2]. + 1 + to: 6000 + do: [:each | c at: each]. + ^ c! Item was added: + ----- Method: LRUCache>>at: (in category 'accessing') ----- + at: aKey + "answer the object for aKey, if not present in the cache creates it" + | element keyHash | + calls := calls + 1. + keyHash := aKey hash. + 1 + to: size + do: [:index | + element := values at: index. + (keyHash + = (element at: 2) + and: [aKey + = (element at: 1)]) + ifTrue: ["Found!!" + hits := hits + 1. + values + replaceFrom: 2 + to: index + with: (values first: index - 1). + values at: 1 put: element. + ^ element at: 3]]. + "Not found!!" + element := {aKey. keyHash. factory value: aKey}. + values + replaceFrom: 2 + to: size + with: values allButLast. + values at: 1 put: element. + ^ element at: 3! Item was added: + ----- Method: LRUCache>>initializeSize:factory: (in category 'initialization') ----- + initializeSize: aNumber factory: aBlock + "initialize the receiver's size and factory" + size := aNumber. + values := Array new: aNumber withAll: {nil. nil. nil}. + factory := aBlock. + calls := 0. + hits := 0! Item was added: + ----- Method: LRUCache>>printOn: (in category 'printing') ----- + printOn: aStream + "Append to the argument, aStream, a sequence of characters + that identifies the receiver." + aStream nextPutAll: self class name; + nextPutAll: ' size:'; + nextPutAll: size asString; + nextPutAll: ', calls:'; + nextPutAll: calls asString; + nextPutAll: ', hits:'; + nextPutAll: hits asString; + nextPutAll: ', ratio:'; + nextPutAll: ((hits isNumber and: [calls isNumber and: [calls ~= 0]]) + ifTrue: [hits / calls] + ifFalse: [0]) asFloat asString! |
Free forum by Nabble | Edit this page |