<commit_before>use std::cmp::min;
use std::mem;

use runtime::Messageable;
use id::{class, Id, IdVector};
use super::{INSCopying, INSObject};

pub trait INSDictionary<K: Messageable, V> : INSObject {
	fn count(&self) -> uint {
		let result = unsafe {
			msg_send![self.as_ptr() count]
		};
		result as uint
	}

	fn object_for<'a>(&'a self, key: &K) -> Option<&'a V> {
		unsafe {
			let obj = msg_send![self.as_ptr() objectForKey:key.as_ptr()];
			if obj.is_null() {
				None
			} else {
				Some(mem::transmute(obj))
			}
		}
	}

	unsafe fn from_refs<T: INSCopying<K>>(keys: &[&T], vals: &[&V]) -> Id<Self> {
		let cls = class::<Self>();
		let count = min(keys.len(), vals.len());
		let obj = msg_send![cls.as_ptr() alloc];
		let obj = msg_send![obj initWithObjects:vals.as_ptr()
		                                forKeys:keys.as_ptr()
		                                  count:count];
		Id::from_retained_ptr(obj as *Self)
	}

	fn from_keys_and_objects<T: INSCopying<K>>(keys: &[&T], vals: Vec<Id<V>>) -> Id<Self> {
		let vals_refs = vals.as_refs_slice();
		unsafe {
			INSDictionary::from_refs(keys, vals_refs)
		}
	}
}

object_struct!(NSDictionary<K, V>)

impl<K: Messageable, V> INSDictionary<K, V> for NSDictionary<K, V> { }

impl<K: Messageable, V> Collection for NSDictionary<K, V> {
	fn len(&self) -> uint {
		self.count()
	}
}<commit_msg>Implemented Map for NSDictionary.<commit_after>use std::cmp::min;
use std::mem;

use runtime::Messageable;
use id::{class, Id, IdVector};
use super::{INSCopying, INSObject};

pub trait INSDictionary<K: Messageable, V> : INSObject {
	fn count(&self) -> uint {
		let result = unsafe {
			msg_send![self.as_ptr() count]
		};
		result as uint
	}

	fn object_for<'a>(&'a self, key: &K) -> Option<&'a V> {
		unsafe {
			let obj = msg_send![self.as_ptr() objectForKey:key.as_ptr()];
			if obj.is_null() {
				None
			} else {
				Some(mem::transmute(obj))
			}
		}
	}

	unsafe fn from_refs<T: INSCopying<K>>(keys: &[&T], vals: &[&V]) -> Id<Self> {
		let cls = class::<Self>();
		let count = min(keys.len(), vals.len());
		let obj = msg_send![cls.as_ptr() alloc];
		let obj = msg_send![obj initWithObjects:vals.as_ptr()
		                                forKeys:keys.as_ptr()
		                                  count:count];
		Id::from_retained_ptr(obj as *Self)
	}

	fn from_keys_and_objects<T: INSCopying<K>>(keys: &[&T], vals: Vec<Id<V>>) -> Id<Self> {
		let vals_refs = vals.as_refs_slice();
		unsafe {
			INSDictionary::from_refs(keys, vals_refs)
		}
	}
}

object_struct!(NSDictionary<K, V>)

impl<K: Messageable, V> INSDictionary<K, V> for NSDictionary<K, V> { }

impl<K: Messageable, V> Collection for NSDictionary<K, V> {
	fn len(&self) -> uint {
		self.count()
	}
}

impl<K: Messageable, V> Map<K, V> for NSDictionary<K, V> {
	fn find<'a>(&'a self, key: &K) -> Option<&'a V> {
		self.object_for(key)
	}
}
{'fraction_non_alphanumeric': 0.17946021731510692, 'fraction_numerical': 0.0, 'mean_word_length': 3.438569206842924, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4086672', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1134, 'n_words': 237}