<commit_before>//
// Created by <NAME> on 18/04/15.
// Copyright (c) 2015 <NAME>. All rights reserved.
//

public class WeakSet<T: Hashable where T: AnyObject> {
    private var _array: [WeakReference<T>]
    private let lock = Spinlock()
    
    public init() {
        _array = Array()
    }

    public init(_ array: [T]) {
        self._array = array.map {
            WeakReference($0)
        }
    }
    
    public var set: Set<T> {
        return Set(self.array())
    }

    public func insert(member: T) {
        lock.around {
            var found = false
            for e in self._array {
                if e.hashValue == member.hashValue {
                    found = true
                    break
                }
            }
            if !found {
                self._array.append(WeakReference(member))
            }
        }
    }

    public func filter(@noescape includeElement: (T) -> Bool) -> WeakSet<T> {
        return WeakSet(self.array().filter(includeElement))
    }

    public func hasElementPassingTest(@noescape filterFunc: (T) -> Bool) -> Bool {
        return self.array().hasElementPassingTest(filterFunc)
    }

    public func map<U>(@noescape transform: (T) -> U) -> WeakSet<U> {
        return WeakSet<U>(self.array().map(transform))
    }
    
    public func flatMap<U>(@noescape transform: (T) -> U?) -> WeakSet<U> {
        return WeakSet<U>(self.array().flatMap(transform))
    }
    
    private func array() -> Array<T> {
        return lock.around {
            return self._array.filter {
                $0.value != nil
            }.map {
                $0.value!
            }
        }

    }

}<commit_msg>Thread safe weakset<commit_after>//
// Created by <NAME> on 18/04/15.
// Copyright (c) 2015 <NAME>. All rights reserved.
//

public class WeakSet<T: Hashable where T: AnyObject> {
    private var _array: [WeakReference<T>]
    
    public init() {
        _array = Array()
    }

    public init(_ array: [T]) {
        _array = array.map {
            WeakReference($0)
        }
    }
    
    public var set: Set<T> {
        return Set(self.array())
    }

    public func insert(member: T) {
        for e in _array {
            if e.hashValue == member.hashValue {
                return
            }
        }
        _array.append(WeakReference(member))
    }

    public func filter(@noescape includeElement: (T) -> Bool) -> WeakSet<T> {
        return WeakSet(self.array().filter(includeElement))
    }

    public func hasElementPassingTest(@noescape filterFunc: (T) -> Bool) -> Bool {
        return self.array().hasElementPassingTest(filterFunc)
    }

    public func map<U>(@noescape transform: (T) -> U) -> WeakSet<U> {
        return WeakSet<U>(self.array().map(transform))
    }
    
    public func flatMap<U>(@noescape transform: (T) -> U?) -> WeakSet<U> {
        return WeakSet<U>(self.array().flatMap(transform))
    }
    
    private func array() -> Array<T> {
        var result = Array<T>()
        for item in _array {
            if let v = item.value {
                result.append(v)
            }
        }
        return result
    }

}
{'fraction_non_alphanumeric': 0.13076678332803054, 'fraction_numerical': 0.007636016544702514, 'mean_word_length': 1.7267996530789245, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 48, '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': '7227587', 'n_tokens_mistral': 1000, 'n_tokens_neox': 932, 'n_words': 224}