<commit_before>import Foundation

public class Notification<T>  {
  typealias NotificationClosure = (Notification<T>) -> Void
  let name:String
  private(set) weak var sender:AnyObject?
  private(set) var userInfo:T?
  private let closure:NotificationClosure
  
  
  private weak var hub:NotificationHub<T>?
  
  
  init(name:String, sender:AnyObject?, handler:NotificationClosure) {
    self.name     = name
    self.closure  = handler
    self.sender   = sender
  }
  
  
  final func publishUserInfo(userInfo:T?) -> Bool {
    if(self.hub == nil) { return false }
    self.userInfo = userInfo
    self.closure(self)
    self.userInfo = nil
    return true
  }
  
  final func remove() -> Bool {
    if(self.hub == nil) { return false }
    self.hub?.removeNotification(self)
    self.userInfo = nil
    self.hub = nil
    return true
  }
  
  
}


private struct Static {
  static var hubToken : dispatch_once_t = 0
  static var hub : NotificationHub<[String:Any]>? = nil
}


//public var NotificationHubDefault : NotificationHub<[String:Any]> {
//  return NotificationHub<[String:Any]>()
//}


public class NotificationHub<T>  {
  final private var internalNotifications    =  NSMutableDictionary(capacity: 1000)
  final var notifications:[String: [Notification<T>]] {
    return self.internalNotifications as AnyObject as! [String: [Notification<T>]]
  }
  
  private class var defaultHub:NotificationHub<[String:Any]> {
    dispatch_once(&Static.hubToken) {
      Static.hub = NotificationHub<[String:Any]>()
    }
    return Static.hub!
  }
  
  init() {}
  
  
  func subscribeNotificationForName(name: String, sender: AnyObject? = nil, handler: (Notification<T>) -> Void) -> Notification<T> {    
    let notification = Notification(name: name, sender: sender, handler: handler)
    return self.subscribeNotification(notification)
  }
  
  func subscribeNotification(notification:Notification<T>) -> Notification<T> {
    #if DEBUG
      NotificationHubMock.onSubscribeMockHandler?(name:notification.name,sender:notification.sender)
    #endif
    
    if notification.hub !== nil { notification.hub?.removeNotification(notification) }
    notification.hub = self
    
    let name = notification.name
    if let notifications: NSMutableArray = self.internalNotifications[notification.name] as! NSMutableArray? {
      notifications.addObject(notification)
    }
    else {
      var array = NSMutableArray(capacity: 10)
      array.addObject(notification)
      self.internalNotifications[name] = NSMutableArray(object: notification)
    }
    
    return notification
  }
  
  
  func publishNotificationName(name: String, sender: AnyObject? = nil, userInfo:T? = nil) -> Bool {
    #if DEBUG
      NotificationHubMock.onPublishMockHandler?(name: name, sender: sender, userInfo: userInfo)
    #endif
    
    
    var didPublish = false
    var notifications = self.internalNotifications[name] as? NSMutableArray
    if let notifications = notifications {
      if sender != nil {
        for notification in notifications {
          let not:Notification = notification as! Notification<T>
          if not.sender == nil {
            not.sender = sender
            didPublish = not.publishUserInfo(userInfo)
            not.sender = nil
          }
          else if  not.sender === sender { didPublish = not.publishUserInfo(userInfo) }
        }
      }
      else {
        for notification in notifications {<commit_msg>Shave off some nanoseconds.<commit_after>import Foundation

public class Notification<T>  {
  typealias NotificationClosure = (Notification<T>) -> Void
  let name:String
  private(set) weak var sender:AnyObject?
  private(set) var userInfo:T?
  private let closure:NotificationClosure
  
  
  private weak var hub:NotificationHub<T>?
  
  
  init(name:String, sender:AnyObject?, handler:NotificationClosure) {
    self.name     = name
    self.closure  = handler
    self.sender   = sender
  }
  
  
  final func publishUserInfo(userInfo:T?) -> Bool {
    if(self.hub == nil) { return false }
    self.userInfo = userInfo
    self.closure(self)
    self.userInfo = nil
    return true
  }
  
  final func remove() -> Bool {
    if(self.hub == nil) { return false }
    self.hub?.removeNotification(self)
    self.userInfo = nil
    self.hub = nil
    return true
  }
  
  
}


private struct Static {
  static var hubToken : dispatch_once_t = 0
  static var hub : NotificationHub<[String:Any]>? = nil
}


//public var NotificationHubDefault : NotificationHub<[String:Any]> {
//  return NotificationHub<[String:Any]>()
//}


public class NotificationHub<T>  {
  final private var internalNotifications    =  NSMutableDictionary(capacity: 1000)
  final var notifications:[String: [Notification<T>]] {
    return self.internalNotifications as AnyObject as! [String: [Notification<T>]]
  }
  
  private class var defaultHub:NotificationHub<[String:Any]> {
    dispatch_once(&Static.hubToken) {
      Static.hub = NotificationHub<[String:Any]>()
    }
    return Static.hub!
  }
  
  init() {}
  
  
  func subscribeNotificationForName(name: String, sender: AnyObject? = nil, handler: (Notification<T>) -> Void) -> Notification<T> {    
    let notification = Notification(name: name, sender: sender, handler: handler)
    return self.subscribeNotification(notification)
  }
  
  func subscribeNotification(notification:Notification<T>) -> Notification<T> {
    #if DEBUG
      NotificationHubMock.onSubscribeMockHandler?(name:notification.name,sender:notification.sender)
    #endif
    
    if notification.hub !== nil { notification.hub?.removeNotification(notification) }
    notification.hub = self
    
    let name = notification.name
    if let notifications = self.internalNotifications[notification.name] as? NSMutableArray {
      notifications.addObject(notification)
    }
    else {
      var array = NSMutableArray(capacity: 50)
      array.addObject(notification)
      self.internalNotifications[name] = array
    }
    
    return notification
  }
  
  
  func publishNotificationName(name: String, sender: AnyObject? = nil, userInfo:T? = nil) -> Bool {
    #if DEBUG
      NotificationHubMock.onPublishMockHandler?(name: name, sender: sender, userInfo: userInfo)
    #endif
    
    
    var didPublish = false
    if let notifications = self.internalNotifications[name] as? NSMutableArray {
      if sender != nil {
        for notification in notifications {
          let not:Notification = notification as! Notification<T>
          if not.sender == nil {
            not.sender = sender
            didPublish = not.publishUserInfo(userInfo)
            not.sender = nil
          }
          else if  not.sender === sender { didPublish = not.publishUserInfo(userInfo) }
        }
      }
      else {
        for notification in notifications {
{'fraction_non_alphanumeric': 0.10014705882352941, 'fraction_numerical': 0.002058823529411765, 'mean_word_length': 3.174953959484346, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 49, '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': '3382493', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1912, 'n_words': 458}