<commit_before>//
//  StaticTableViewController.swift
//  CratesIOViewer
//
//  Created by <NAME>. on 11/22/14.
//
//

import Foundation
import UIKit


//private let	UILayoutPriorityRequired			=	1000
//private let	UILayoutPriorityDefaultHigh			=	750
//private let	UILayoutPriorityFittingSizeLevel	=	50
private let	REQUIRED	=	1000	as Float
private let	HIGH		=	750		as Float
private	let	FITTING		=	50		as Float


///	This class is designed for very small amount of cells.
///	Keeps all cells in memory.
public class StaticTableViewController: UITableViewController {
	public typealias	Table	=	StaticTable
	public typealias	Section	=	StaticTableSection
	public typealias	Row		=	StaticTableRow
	
	private var	_table	=	nil as Table?

	deinit {
		if let t = _table {
			unownTable(t)
		}
	}
	
	private func ownTable(targetTable:Table) {
		precondition(table.hostTableViewController == nil, "Supplied table object alread bound to another table view controller.")
		table.hostTableViewController	=	self
		tableView?.tableHeaderView	=	table.header
		tableView?.tableFooterView	=	table.footer
		tableView?.reloadData()
	}
	private func unownTable(targetTable:Table) {
		assert(table.hostTableViewController == self)
		table.hostTableViewController	=	nil
		tableView?.tableHeaderView	=	nil
		tableView?.tableFooterView	=	nil
	}

	///	Table must be always non-nil value.
	public final var table:Table {
		get {
			if _table == nil {
				_table	=	Table()
				ownTable(_table!)
			}
			return	_table!
		}
		set(v) {
			if _table != nil {
				unownTable(_table!)
			}
			_table	=	v
			ownTable(_table!)
		}
//		willSet {
//			assert(table.hostTableViewController == self)
//			table.hostTableViewController	=	nil
//			
//			tableView?.tableHeaderView	=	nil
//			tableView?.tableFooterView	=	nil
//		}
//		didSet {
//			precondition(table.hostTableViewController == nil, "Supplied table object alread bound to another table view controller.")
//			table.hostTableViewController	=	self
//			tableView?.tableHeaderView	=	table.header
//			tableView?.tableFooterView	=	table.footer
//			tableView?.reloadData()
//		}
	}
	
	
	
	public override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
		return	table.sections.count ||| 0
	}
	public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return	table.sections[section].rows.count
	}
	public override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
		let	sz	=	tableView.bounds.size
		let	sz1	=	CGSize(width: sz.width, height: 0)
		return	table.sections[section].header?.heightAsCellInTableView(tableView) ||| 0
	}
	public override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
		let	sz	=	tableView.bounds.size
		let	sz1	=	CGSize(width: sz.width, height: 0)
		return	table.sections[section].footer?.heightAsCellInTableView(tableView) ||| 0
	}
	public override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
		return	table.sections[section].header
	}
	public override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
		return	table.sections[section].footer
	}
	
	public override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
		return	table.rowAtIndexPath(indexPath).cell.heightAsCellInTableView(tableView)
	}
	public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
		return	table.rowAtIndexPath(indexPath).cell
	}
	
	public override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
		return	table.rowAtIndexPath(indexPath).willSelect().indexPath
	}
	public override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
		table.rowAtIndexPath(indexPath).didSelect()
	}
	public override func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
		return	table.rowAtIndexPath(indexPath).willDeselect().indexPath
	}
	public override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
		table.rowAtIndexPath(indexPath).didDeselect()
	}
}

private extension UIView {
	func heightForMaximumWidth(width:CGFloat) -> CGFloat {
		assert(width >= 0)
		let		sz1	=	systemLayoutSizeFittingSize(CGSize(width: width, height: 0), withHorizontalFittingPriority: HIGH, verticalFittingPriority: FITTING)
		return	sz1.height
//		let		sz2	=	intrinsicContentSize()
//		return	max(sz1.height, sz2.height)
	}
	func heightAsCellInTableView(tableView:UITableView) -> CGFloat {
		return	heightForMaximumWidth(tableView.bounds.size.width)
	}
}

















public class StaticTable {
	public typealias	Section		=	StaticTableSection
	public typealias	Row			=	StaticTableRow
	<commit_msg>Added a comment to static table class.<commit_after>//
//  StaticTableViewController.swift
//  CratesIOViewer
//
//  Created by <NAME>. on 11/22/14.
//
//

import Foundation
import UIKit


//private let	UILayoutPriorityRequired			=	1000
//private let	UILayoutPriorityDefaultHigh			=	750
//private let	UILayoutPriorityFittingSizeLevel	=	50
private let	REQUIRED	=	1000	as Float
private let	HIGH		=	750		as Float
private	let	FITTING		=	50		as Float


///	This class is designed for very small amount of cells.
///	Keeps all cells in memory.
public class StaticTableViewController: UITableViewController {
	public typealias	Table	=	StaticTable
	public typealias	Section	=	StaticTableSection
	public typealias	Row		=	StaticTableRow
	
	private var	_table	=	nil as Table?

	deinit {
		if let t = _table {
			unownTable(t)
		}
	}
	
	private func ownTable(targetTable:Table) {
		precondition(table.hostTableViewController == nil, "Supplied table object alread bound to another table view controller.")
		table.hostTableViewController	=	self
		tableView?.tableHeaderView	=	table.header
		tableView?.tableFooterView	=	table.footer
		tableView?.reloadData()
	}
	private func unownTable(targetTable:Table) {
		assert(table.hostTableViewController == self)
		table.hostTableViewController	=	nil
		tableView?.tableHeaderView	=	nil
		tableView?.tableFooterView	=	nil
	}

	///	Table must be always non-nil value.
	public final var table:Table {
		get {
			if _table == nil {
				_table	=	Table()
				ownTable(_table!)
			}
			return	_table!
		}
		set(v) {
			if _table != nil {
				unownTable(_table!)
			}
			_table	=	v
			ownTable(_table!)
		}
//		willSet {
//			assert(table.hostTableViewController == self)
//			table.hostTableViewController	=	nil
//			
//			tableView?.tableHeaderView	=	nil
//			tableView?.tableFooterView	=	nil
//		}
//		didSet {
//			precondition(table.hostTableViewController == nil, "Supplied table object alread bound to another table view controller.")
//			table.hostTableViewController	=	self
//			tableView?.tableHeaderView	=	table.header
//			tableView?.tableFooterView	=	table.footer
//			tableView?.reloadData()
//		}
	}
	
	
	
	public override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
		return	table.sections.count ||| 0
	}
	public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
		return	table.sections[section].rows.count
	}
	public override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
		let	sz	=	tableView.bounds.size
		let	sz1	=	CGSize(width: sz.width, height: 0)
		return	table.sections[section].header?.heightAsCellInTableView(tableView) ||| 0
	}
	public override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
		let	sz	=	tableView.bounds.size
		let	sz1	=	CGSize(width: sz.width, height: 0)
		return	table.sections[section].footer?.heightAsCellInTableView(tableView) ||| 0
	}
	public override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
		return	table.sections[section].header
	}
	public override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
		return	table.sections[section].footer
	}
	
	public override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
		return	table.rowAtIndexPath(indexPath).cell.heightAsCellInTableView(tableView)
	}
	public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
		return	table.rowAtIndexPath(indexPath).cell
	}
	
	public override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
		return	table.rowAtIndexPath(indexPath).willSelect().indexPath
	}
	public override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
		table.rowAtIndexPath(indexPath).didSelect()
	}
	public override func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
		return	table.rowAtIndexPath(indexPath).willDeselect().indexPath
	}
	public override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
		table.rowAtIndexPath(indexPath).didDeselect()
	}
}

private extension UIView {
	func heightForMaximumWidth(width:CGFloat) -> CGFloat {
		assert(width >= 0)
		let		sz1	=	systemLayoutSizeFittingSize(CGSize(width: width, height: 0), withHorizontalFittingPriority: HIGH, verticalFittingPriority: FITTING)
		return	sz1.height
//		let		sz2	=	intrinsicContentSize()
//		return	max(sz1.height, sz2.height)
	}
	func heightAsCellInTableView(tableView:UITableView) -> CGFloat {
		return	heightForMaximumWidth(tableView.bounds.size.width)
	}
}
















///	If you need to modify multiple elements at once, then the best practice is
///	making a new table and replacing whole table with it instead replacing each elements.
public class StaticTable {
	public typealias	Section		=	StaticTableSection
	public typealias	Row			=	StaticTableRow
	
{'fraction_non_alphanumeric': 0.09349795877725779, 'fraction_numerical': 0.007567459922333964, 'mean_word_length': 4.58, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '4939064', 'n_tokens_mistral': 3505, 'n_tokens_neox': 3175, 'n_words': 758}