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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (hide annotations) (download)
Sun Mar 10 14:28:57 2013 UTC (7 years, 7 months ago) by william
File size: 11505 byte(s)

1 william 90 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using libxmltv.Interfaces;
6     using System.Runtime.Serialization;
7 william 92 using System.Collections;
8     using System.Runtime.InteropServices;
9     using System.Reflection;
10 william 90
11     namespace libxmltv.Core
12     {
13 william 110 [Serializable]
14 william 90 public class PropertyCollection<T> : IPropertyCollection<T>
15     {
16     private List<T> items = new List<T>();
17     public PropertyCollection() : this(new List<T>()) { }
18     public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }
19     public int PropertyCount { get { return items.Count; } }
20 william 98
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 william 90 public void AddProperty(T item) { items.Add(item); }
35     public void ClearProperties() { items.Clear(); }
36     public bool ContainsProperty(T item) { return items.Contains(item); }
37     public void CopyPropertiesTo(T[] array, int arrayIndex) { items.CopyTo(array, arrayIndex); }
38     public bool RemoveProperty(T item) { return items.Remove(item); }
39     public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
40     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
41 william 106
42     public override string ToString()
43     {
44     return string.Format("Property Count: {0}", PropertyCount);
45     }
46 william 90 }
47 william 110 [Serializable]
48 william 95 public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary
49 william 92 {
50 william 94 public PropertyDictionary() : base() { }
51 william 92 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 william 110 [Serializable]
59 william 90 public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
60     {
61 william 92 [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 william 106 public override string ToString()
69     {
70     return new PropertyValuePair<TKey, TValue>(name, value).ToString();
71     }
72 william 92 }
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 william 90 private Dictionary<TKey, TValue> properties;
103     public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
104 william 92 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
105 william 90 public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
106     public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
107 william 92 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
108 william 90 public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
109 william 92 protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
110 william 90 public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
111     public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
112     public TValue this[TKey key]
113     {
114     get
115     {
116     if (this.ContainsProperty(key))
117     {
118     return properties[key];
119     }
120     else
121     {
122     throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
123     }
124     }
125     set
126     {
127     if (this.ContainsProperty(key))
128     {
129     properties[key] = value;
130     }
131     else
132     {
133     throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
134     }
135     }
136     }
137    
138     public void AddProperty(TKey key, TValue value) { if (properties.ContainsKey(key)) { this[key] = value; } else { properties.Add(key, value); } }
139     public bool ContainsProperty(TKey key) { return properties.ContainsKey(key); }
140     public bool RemoveProperty(TKey key) { return properties.Remove(key); }
141     public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
142     public int PropertyCount { get { return properties.Count; } }
143 william 98 public bool IsReadOnly
144     {
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 william 105 public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
156 william 90 public void ClearProperties() { properties.Clear(); }
157 william 105 public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
158     public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex)
159 william 92 {
160 william 105 var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
161 william 92 list.CopyTo(array, arrayIndex);
162     }
163 william 105 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 william 90 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
166 william 92
167 william 106 public override string ToString()
168     {
169     return string.Format("Property Count: {0}", PropertyCount);
170     }
171 william 92 #region enumerator support
172 william 105 public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable
173 william 92 {
174     private PropertyDictionary<TKey, TValue> dictionary;
175     private int index;
176 william 105 private IPropertyValuePair<TKey, TValue> current;
177 william 92 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 william 93 this.version = dictionary.version;
185     this.index = 0;
186 william 92 this.getEnumeratorRetType = getEnumeratorRetType;
187 william 93 this.current = new PropertyValuePair<TKey, TValue>();
188 william 92 }
189    
190 william 105 public IPropertyValuePair<TKey, TValue> Current
191 william 92 {
192 william 93 get { return this.current; }
193 william 92 }
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 william 105 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 william 92 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 william 105 this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>();
243 william 92 }
244     }
245     #endregion
246 william 90 }
247    
248 william 92
249 william 90 }

  ViewVC Help
Powered by ViewVC 1.1.22