<commit_before>//
//  Siegel.swift
//  Pods
//
//  Created by Mihyaeru on 2015/07/24.
//
//

import Foundation

public class Siegel<T> {
    private let size: Int
    private let gcFactor: Int
    private var fifo: [(key: String, valueRef: Ref<T>?)]
    private var entries: [String: Weak<Ref<T>>?]

    public init(size: Int = 1024, gcFactor: Int = 10) {
        self.size     = size
        self.gcFactor = gcFactor
        self.fifo     = []
        self.entries  = [:]
    }

    public func set(#key: String, value: T) -> T {
        if var oldValue = self.entries[key] {
            oldValue = nil
        }

        // register
        let valueRef = Ref<T>(value: value)
        self.entries[key] = Weak<Ref<T>>(value: valueRef)
        self.updateFifo(key, valueRef: valueRef)

        // expire the oldest entry if full
        while self.entries.count > self.size {
            let expireKey = self.fifo.removeAtIndex(0).0
            if self.entries[expireKey]??.value == nil {
                self.entries.removeValueForKey(expireKey)
            }
        }

        return value
    }

    public func get(#key: String) -> T? {
        if let weakValueRef = self.entries[key] {
            self.updateFifo(key, valueRef: weakValueRef?.value)
            return weakValueRef?.value?.value
        }
        else {
            return nil
        }
    }

    public func remove(#key: String) -> T? {
        if var weakValueRef = self.entries.removeValueForKey(key) {
            let value = weakValueRef?.value?.value
            weakValueRef = nil
            return value
        }
        else {
            return nil
        }
    }

    public func clear() {
        self.fifo    = []
        self.entries = [:]
    }

    private func updateFifo(key: String, valueRef: Ref<T>?) {
        self.fifo.append((key: key, valueRef: valueRef))
        if self.fifo.count >= self.size * self.gcFactor {
            var newFifo: [(key: String, valueRef: Ref<T>?)] = []
            var need = self.entries
            while need.count > 0 {
                let fifoEntry = self.fifo.removeLast()
                if need.removeValueForKey(fifoEntry.0) != nil {
                    newFifo.append(fifoEntry)
                }<commit_msg>don't use optional type<commit_after>//
//  Siegel.swift
//  Pods
//
//  Created by Mihyaeru on 2015/07/24.
//
//

import Foundation

public class Siegel<T> {
    private let size: Int
    private let gcFactor: Int
    private var fifo: [(key: String, valueRef: Ref<T>)]
    private var entries: [String: Weak<Ref<T>>]

    public init(size: Int = 1024, gcFactor: Int = 10) {
        self.size     = size
        self.gcFactor = gcFactor
        self.fifo     = []
        self.entries  = [:]
    }

    public func set(#key: String, value: T) -> T {
        let valueRef = Ref<T>(value: value)
        self.entries[key] = Weak<Ref<T>>(value: valueRef)
        self.updateFifo(key, valueRef: valueRef)

        while self.entries.count > self.size {
            let expireKey = self.fifo.removeAtIndex(0).0
            if self.entries[expireKey]?.value == nil {
                self.entries.removeValueForKey(expireKey)
            }
        }

        return value
    }

    public func get(#key: String) -> T? {
        if let weakValueRef = self.entries[key] {
            self.updateFifo(key, valueRef: weakValueRef.value!)
            return weakValueRef.value?.value
        }
        return nil
    }

    public func remove(#key: String) -> T? {
        return self.entries.removeValueForKey(key)?.value?.value
    }

    public func clear() {
        self.fifo    = []
        self.entries = [:]
    }

    private func updateFifo(key: String, valueRef: Ref<T>) {
        self.fifo.append((key: key, valueRef: valueRef))
        if self.fifo.count >= self.size * self.gcFactor {
            var newFifo: [(key: String, valueRef: Ref<T>)] = []
            var need = self.entries
            while need.count > 0 {
                let fifoEntry = self.fifo.removeLast()
                if need.removeValueForKey(fifoEntry.0) != nil {
                    newFifo.append(fifoEntry)
                }
{'fraction_non_alphanumeric': 0.12066473988439307, 'fraction_numerical': 0.008670520231213872, 'mean_word_length': 1.7485109199205824, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1843630', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1264, 'n_words': 301}