/[xmltv_parser]/trunk/libxmltv/Core/PropertyDictionary.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/PropertyDictionary.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 90 by william, Sat Mar 9 17:42:34 2013 UTC revision 110 by william, Sun Mar 10 14:28:57 2013 UTC
# Line 4  using System.Linq; Line 4  using System.Linq;
4  using System.Text;  using System.Text;
5  using libxmltv.Interfaces;  using libxmltv.Interfaces;
6  using System.Runtime.Serialization;  using System.Runtime.Serialization;
7    using System.Collections;
8    using System.Runtime.InteropServices;
9    using System.Reflection;
10    
11  namespace libxmltv.Core  namespace libxmltv.Core
12  {  {
13        [Serializable]
14      public class PropertyCollection<T> : IPropertyCollection<T>      public class PropertyCollection<T> : IPropertyCollection<T>
15      {      {
16          private List<T> items = new List<T>();          private List<T> items = new List<T>();
17          public PropertyCollection() : this(new List<T>()) { }          public PropertyCollection() : this(new List<T>()) { }
18          public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }          public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }
19          public int PropertyCount { get { return items.Count; } }          public int PropertyCount { get { return items.Count; } }
20          public bool IsReadOnly { get { return (items as ICollection<T>).IsReadOnly; } }  
21            public bool IsReadOnly
22            {
23                get
24                {
25                    ICollection<T> collection = (items as ICollection<T>);
26                    if (collection == null)
27                    {
28                        throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", items.GetType().Name, typeof(ICollection<T>).Name));
29                    }
30                    return collection.IsReadOnly;
31                }
32            }
33    
34          public void AddProperty(T item) { items.Add(item); }          public void AddProperty(T item) { items.Add(item); }
35          public void ClearProperties() { items.Clear(); }          public void ClearProperties() { items.Clear(); }
36          public bool ContainsProperty(T item) { return items.Contains(item); }          public bool ContainsProperty(T item) { return items.Contains(item); }
# Line 22  namespace libxmltv.Core Line 38  namespace libxmltv.Core
38          public bool RemoveProperty(T item) { return items.Remove(item); }          public bool RemoveProperty(T item) { return items.Remove(item); }
39          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
40          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
     }  
41    
42            public override string ToString()
43            {
44                return string.Format("Property Count: {0}", PropertyCount);
45            }
46        }
47        [Serializable]
48        public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary
49        {
50            public PropertyDictionary() : base() { }
51            public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { }
52            public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { }
53            public PropertyDictionary(int capacity) :base(capacity) { }
54            public PropertyDictionary(IPropertyDictionary dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer) { }
55            public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { }
56            protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { }
57        }
58        [Serializable]
59      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
60      {      {
61            [StructLayout(LayoutKind.Sequential)]
62            private struct Entry
63            {
64                public int hashCode;
65                public int next;
66                public TKey name;
67                public TValue value;
68                public override string ToString()
69                {
70                    return new PropertyValuePair<TKey, TValue>(name, value).ToString();
71                }
72            }
73            private Entry[] entries
74            {
75               get{
76                   List<Entry> list = new List<Entry>();
77                   int index = 0;
78                   foreach (var t in properties)
79                   {
80                       Entry entry = new Entry();
81                       entry.hashCode = this.properties.Comparer.GetHashCode(t.Key) & 0x7fffffff;
82                       entry.next = index + 1;
83                       entry.name = t.Key;
84                       entry.value = t.Value;
85                       list.Add(entry);
86                       index++;
87                   }
88                   return list.ToArray();
89               }
90            }
91    
92            private int version
93            {
94                get
95                {
96                    Type t = typeof(Dictionary<TKey, TValue>);
97                    var field = t.GetField("version", BindingFlags.NonPublic | BindingFlags.Instance);
98                    var v = Convert.ToInt32(field.GetValue(properties));
99                    return v;
100                }
101            }
102          private Dictionary<TKey, TValue> properties;          private Dictionary<TKey, TValue> properties;
103          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
104          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); } }
105          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
106          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
107          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); } }
108          public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }          public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
109          protected PropertyDictionary(SerializationInfo info, StreamingContext context)          protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
         {  
         }  
110          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
111          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
112          public TValue this[TKey key]          public TValue this[TKey key]
# Line 69  namespace libxmltv.Core Line 140  namespace libxmltv.Core
140          public bool RemoveProperty(TKey key) { return properties.Remove(key); }          public bool RemoveProperty(TKey key) { return properties.Remove(key); }
141          public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }          public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
142          public int PropertyCount { get { return properties.Count; } }          public int PropertyCount { get { return properties.Count; } }
143          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }          public bool IsReadOnly
144          public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); }          {
145                get
146                {
147                    IDictionary<TKey, TValue> collection = (properties as IDictionary<TKey, TValue>);
148                    if (collection == null)
149                    {
150                        throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", properties.GetType().Name, typeof(IDictionary<TKey, TValue>).Name));
151                    }
152                    return collection.IsReadOnly;
153                }
154            }
155            public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
156          public void ClearProperties() { properties.Clear(); }          public void ClearProperties() { properties.Clear(); }
157          public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); }          public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
158          public void CopyPropertiesTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { var list = properties.ToList(); list.CopyTo(array, arrayIndex); }          public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex)
159          public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); }          {
160          public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); }              var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
161                list.CopyTo(array, arrayIndex);
162            }
163            public bool RemoveProperty(IPropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }
164            IEnumerator<IPropertyValuePair<TKey, TValue>> IEnumerable<IPropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); }
165          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
         
     }  
166    
167      public class PropertyDictionary : PropertyDictionary<string, object>          public override string ToString()
168      {          {
169                return string.Format("Property Count: {0}", PropertyCount);
170            }
171            #region enumerator support
172            public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable
173            {
174                private PropertyDictionary<TKey, TValue> dictionary;
175                private int index;
176                private IPropertyValuePair<TKey, TValue> current;
177                private int getEnumeratorRetType;
178                internal const int DictEntry = 1;
179                internal const int KeyValuePair = 2;
180                private int version;
181                public PropertyDictionaryEnumerator(PropertyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType)
182                {
183                    this.dictionary = dictionary;
184                    this.version = dictionary.version;
185                    this.index = 0;
186                    this.getEnumeratorRetType = getEnumeratorRetType;
187                    this.current = new PropertyValuePair<TKey, TValue>();
188                }
189    
190                public IPropertyValuePair<TKey, TValue> Current
191                {
192                    get { return this.current; }
193                }
194    
195                public void Dispose()
196                {
197                }
198    
199                object IEnumerator.Current
200                {
201                    get
202                    {
203                        if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1)))
204                        {
205                            throw new InvalidOperationException("Operation can't happen");
206                        }
207                        if (this.getEnumeratorRetType == 1)
208                        {
209                            return new DictionaryEntry(this.current.Name, this.current.Value);
210                        }
211                        return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value);
212                    }
213                }
214    
215                public bool MoveNext()
216                {
217                    if (this.version != this.dictionary.version)
218                    {
219                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
220                    }
221                    while (this.index < this.dictionary.Count())
222                    {
223                        if (this.dictionary.entries[this.index].hashCode >= 0)
224                        {
225                            this.current =(IPropertyValuePair<TKey,TValue>)(PropertyValuePair<TKey,TValue>)new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
226                            this.index++;
227                            return true;
228                        }
229                        this.index++;
230                    }
231                    this.index = this.dictionary.Count() + 1;
232                    this.current = new PropertyValuePair<TKey, TValue>();
233                    return false;
234                }
235                public void Reset()
236                {
237                    if (this.version != this.dictionary.version)
238                    {
239                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
240                    }
241                    this.index = 0;
242                    this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>();
243                }
244            }
245            #endregion  
246      }      }
247    
248      
249  }  }

Legend:
Removed from v.90  
changed lines
  Added in v.110

  ViewVC Help
Powered by ViewVC 1.1.22