Parent Directory
|
Revision Log
|
Patch
--- trunk/libxmltv/Core/PropertyDictionary.cs 2013/03/09 17:42:34 90 +++ trunk/libxmltv/Core/PropertyDictionary.cs 2013/03/10 08:44:53 93 @@ -4,6 +4,9 @@ using System.Text; using libxmltv.Interfaces; using System.Runtime.Serialization; +using System.Collections; +using System.Runtime.InteropServices; +using System.Reflection; namespace libxmltv.Core { @@ -23,19 +26,66 @@ public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } } - + public class PropertyDictionary : PropertyDictionary<string, object> + { + public PropertyDictionary() : base() + { + ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection); + } + public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { } + public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { } + public PropertyDictionary(int capacity) :base(capacity) { } + public PropertyDictionary(IPropertyDictionary dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer) { } + public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { } + protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } + } public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue> { + [StructLayout(LayoutKind.Sequential)] + private struct Entry + { + public int hashCode; + public int next; + public TKey name; + public TValue value; + } + private Entry[] entries + { + get{ + List<Entry> list = new List<Entry>(); + int index = 0; + foreach (var t in properties) + { + Entry entry = new Entry(); + entry.hashCode = this.properties.Comparer.GetHashCode(t.Key) & 0x7fffffff; + entry.next = index + 1; + entry.name = t.Key; + entry.value = t.Value; + list.Add(entry); + index++; + } + return list.ToArray(); + } + } + + private int version + { + get + { + Type t = typeof(Dictionary<TKey, TValue>); + var field = t.GetField("version", BindingFlags.NonPublic | BindingFlags.Instance); + var v = Convert.ToInt32(field.GetValue(properties)); + return v; + } + } private Dictionary<TKey, TValue> properties; public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); } - public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) { properties = new Dictionary<TKey, TValue>(); foreach (var d in dictionary) { properties.Add(d.Key, d.Value); } } + public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } } public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); } public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); } - public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Key, d.Value); } } + public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } } public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); } - protected PropertyDictionary(SerializationInfo info, StreamingContext context) - { - } + protected PropertyDictionary(SerializationInfo info, StreamingContext context) { } public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } } public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } } public TValue this[TKey key] @@ -70,17 +120,94 @@ public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); } public int PropertyCount { get { return properties.Count; } } public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } } - public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); } + public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); } public void ClearProperties() { properties.Clear(); } - public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); } - public void CopyPropertiesTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { var list = properties.ToList(); list.CopyTo(array, arrayIndex); } - public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); } - public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); } + public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); } + public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex) + { + var list = properties.ToList().Cast<PropertyValuePair<TKey, TValue>>().ToList(); + list.CopyTo(array, arrayIndex); + } + public bool RemoveProperty(PropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); } + IEnumerator<PropertyValuePair<TKey, TValue>> IEnumerable<PropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); } - - } - public class PropertyDictionary : PropertyDictionary<string, object> - { + #region enumerator support + public class PropertyDictionaryEnumerator: IEnumerator<PropertyValuePair<TKey,TValue>>, IDisposable + { + private PropertyDictionary<TKey, TValue> dictionary; + private int index; + private PropertyValuePair<TKey, TValue> current; + private int getEnumeratorRetType; + internal const int DictEntry = 1; + internal const int KeyValuePair = 2; + private int version; + public PropertyDictionaryEnumerator(PropertyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType) + { + this.dictionary = dictionary; + this.version = dictionary.version; + this.index = 0; + this.getEnumeratorRetType = getEnumeratorRetType; + this.current = new PropertyValuePair<TKey, TValue>(); + } + + public PropertyValuePair<TKey, TValue> Current + { + get { return this.current; } + } + + public void Dispose() + { + } + + object IEnumerator.Current + { + get + { + if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1))) + { + throw new InvalidOperationException("Operation can't happen"); + } + if (this.getEnumeratorRetType == 1) + { + return new DictionaryEntry(this.current.Name, this.current.Value); + } + return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value); + } + } + + public bool MoveNext() + { + if (this.version != this.dictionary.version) + { + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + } + while (this.index < this.dictionary.Count()) + { + if (this.dictionary.entries[this.index].hashCode >= 0) + { + this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value); + this.index++; + return true; + } + this.index++; + } + this.index = this.dictionary.Count() + 1; + this.current = new PropertyValuePair<TKey, TValue>(); + return false; + } + public void Reset() + { + if (this.version != this.dictionary.version) + { + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); + } + this.index = 0; + this.current = new KeyValuePair<TKey, TValue>(); + } + } + #endregion } + + }
ViewVC Help | |
Powered by ViewVC 1.1.22 |