<commit_before>//
//  CompositeDataSource.swift
//  DataSource
//
//  Created by <NAME> on 04/06/15.
//  Copyright (c) 2015 Fueled. All rights reserved.
//

import Foundation
import ReactiveCocoa

/// `DataSource` implementation that is composed of an array
/// of other dataSources (called inner dataSources).
///
/// Sections of inner dataSources become the sections of the compositeDataSource
/// in the following order: first all the sections of the first inner dataSource,
/// then all the sections of the second inner dataSource, and so on.
///
/// CompositeDataSource listens to dataChanges in all of its inner dataSources
/// and emits them as its own changes, after mapping section indices in them
/// to correspond to the structure of the compositeDataSource.
public final class CompositeDataSource: DataSource {

	public let changes: Signal<DataChange, NoError>
	private let observer: Signal<DataChange, NoError>.Observer
	private let disposable = CompositeDisposable()

	public let innerDataSources: [DataSource]

	public init(_ inner: [DataSource]) {
		(self.changes, self.observer) = Signal<DataChange, NoError>.pipe()
		self.innerDataSources = inner
		for (index, dataSource) in inner.enumerate() {
			self.disposable += dataSource.changes
				.map { $0.mapSections(mapOutside(inner, index)) }
				.observe(self.observer)
		}
	}

	deinit {
		self.observer.sendCompleted()
		self.disposable.dispose()
	}

	public var numberOfSections: Int {
		return self.innerDataSources.reduce(0) {
			subtotal, dataSource in
			return subtotal + dataSource.numberOfSections
		}
	}<commit_msg>Fix memory leak in CompositeDataSource<commit_after>//
//  CompositeDataSource.swift
//  DataSource
//
//  Created by <NAME> on 04/06/15.
//  Copyright (c) 2015 Fueled. All rights reserved.
//

import Foundation
import ReactiveCocoa

/// `DataSource` implementation that is composed of an array
/// of other dataSources (called inner dataSources).
///
/// Sections of inner dataSources become the sections of the compositeDataSource
/// in the following order: first all the sections of the first inner dataSource,
/// then all the sections of the second inner dataSource, and so on.
///
/// CompositeDataSource listens to dataChanges in all of its inner dataSources
/// and emits them as its own changes, after mapping section indices in them
/// to correspond to the structure of the compositeDataSource.
public final class CompositeDataSource: DataSource {

	public let changes: Signal<DataChange, NoError>
	private let observer: Signal<DataChange, NoError>.Observer
	private let disposable = CompositeDisposable()

	public let innerDataSources: [DataSource]

	public init(_ inner: [DataSource]) {
		(self.changes, self.observer) = Signal<DataChange, NoError>.pipe()
		self.innerDataSources = inner
		for (index, dataSource) in inner.enumerate() {
			self.disposable += dataSource.changes.observeNext {
				[weak self] change in
				if let this = self {
					let map = mapOutside(this.innerDataSources, index)
					let mapped = change.mapSections(map)
					this.observer.sendNext(mapped)
				}
			}
		}
	}

	deinit {
		self.observer.sendCompleted()
		self.disposable.dispose()
	}

	public var numberOfSections: Int {
		return self.innerDataSources.reduce(0) {
			subtotal, dataSource in
			return subtotal + dataSource.numberOfSections
		}
	}
{'fraction_non_alphanumeric': 0.0943961642193587, 'fraction_numerical': 0.0068924183398261915, 'mean_word_length': 3.9160530191458025, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1883557', 'n_tokens_mistral': 1028, 'n_tokens_neox': 915, 'n_words': 332}