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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 105 - (show annotations) (download)
Sun Mar 10 11:35:26 2013 UTC (7 years, 6 months ago) by william
File size: 11007 byte(s)

1 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 using System.Collections;
8 using System.Runtime.InteropServices;
9 using System.Reflection;
10
11 namespace libxmltv.Core
12 {
13
14 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
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); }
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 }
42 public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary
43 {
44 public PropertyDictionary() : base() { }
45 public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { }
46 public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { }
47 public PropertyDictionary(int capacity) :base(capacity) { }
48 public PropertyDictionary(IPropertyDictionary dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer) { }
49 public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { }
50 protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { }
51 }
52 public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
53 {
54 [StructLayout(LayoutKind.Sequential)]
55 private struct Entry
56 {
57 public int hashCode;
58 public int next;
59 public TKey name;
60 public TValue value;
61 }
62 private Entry[] entries
63 {
64 get{
65 List<Entry> list = new List<Entry>();
66 int index = 0;
67 foreach (var t in properties)
68 {
69 Entry entry = new Entry();
70 entry.hashCode = this.properties.Comparer.GetHashCode(t.Key) & 0x7fffffff;
71 entry.next = index + 1;
72 entry.name = t.Key;
73 entry.value = t.Value;
74 list.Add(entry);
75 index++;
76 }
77 return list.ToArray();
78 }
79 }
80
81 private int version
82 {
83 get
84 {
85 Type t = typeof(Dictionary<TKey, TValue>);
86 var field = t.GetField("version", BindingFlags.NonPublic | BindingFlags.Instance);
87 var v = Convert.ToInt32(field.GetValue(properties));
88 return v;
89 }
90 }
91 private Dictionary<TKey, TValue> properties;
92 public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
93 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
94 public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
95 public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
96 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
97 public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
98 protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
99 public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
100 public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
101 public TValue this[TKey key]
102 {
103 get
104 {
105 if (this.ContainsProperty(key))
106 {
107 return properties[key];
108 }
109 else
110 {
111 throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
112 }
113 }
114 set
115 {
116 if (this.ContainsProperty(key))
117 {
118 properties[key] = value;
119 }
120 else
121 {
122 throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
123 }
124 }
125 }
126
127 public void AddProperty(TKey key, TValue value) { if (properties.ContainsKey(key)) { this[key] = value; } else { properties.Add(key, value); } }
128 public bool ContainsProperty(TKey key) { return properties.ContainsKey(key); }
129 public bool RemoveProperty(TKey key) { return properties.Remove(key); }
130 public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
131 public int PropertyCount { get { return properties.Count; } }
132 public bool IsReadOnly
133 {
134 get
135 {
136 IDictionary<TKey, TValue> collection = (properties as IDictionary<TKey, TValue>);
137 if (collection == null)
138 {
139 throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", properties.GetType().Name, typeof(IDictionary<TKey, TValue>).Name));
140 }
141 return collection.IsReadOnly;
142 }
143 }
144 public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
145 public void ClearProperties() { properties.Clear(); }
146 public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
147 public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex)
148 {
149 var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
150 list.CopyTo(array, arrayIndex);
151 }
152 public bool RemoveProperty(IPropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }
153 IEnumerator<IPropertyValuePair<TKey, TValue>> IEnumerable<IPropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); }
154 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
155
156 #region enumerator support
157 public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable
158 {
159 private PropertyDictionary<TKey, TValue> dictionary;
160 private int index;
161 private IPropertyValuePair<TKey, TValue> current;
162 private int getEnumeratorRetType;
163 internal const int DictEntry = 1;
164 internal const int KeyValuePair = 2;
165 private int version;
166 public PropertyDictionaryEnumerator(PropertyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType)
167 {
168 this.dictionary = dictionary;
169 this.version = dictionary.version;
170 this.index = 0;
171 this.getEnumeratorRetType = getEnumeratorRetType;
172 this.current = new PropertyValuePair<TKey, TValue>();
173 }
174
175 public IPropertyValuePair<TKey, TValue> Current
176 {
177 get { return this.current; }
178 }
179
180 public void Dispose()
181 {
182 }
183
184 object IEnumerator.Current
185 {
186 get
187 {
188 if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1)))
189 {
190 throw new InvalidOperationException("Operation can't happen");
191 }
192 if (this.getEnumeratorRetType == 1)
193 {
194 return new DictionaryEntry(this.current.Name, this.current.Value);
195 }
196 return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value);
197 }
198 }
199
200 public bool MoveNext()
201 {
202 if (this.version != this.dictionary.version)
203 {
204 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
205 }
206 while (this.index < this.dictionary.Count())
207 {
208 if (this.dictionary.entries[this.index].hashCode >= 0)
209 {
210 this.current =(IPropertyValuePair<TKey,TValue>)(PropertyValuePair<TKey,TValue>)new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
211 this.index++;
212 return true;
213 }
214 this.index++;
215 }
216 this.index = this.dictionary.Count() + 1;
217 this.current = new PropertyValuePair<TKey, TValue>();
218 return false;
219 }
220 public void Reset()
221 {
222 if (this.version != this.dictionary.version)
223 {
224 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
225 }
226 this.index = 0;
227 this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>();
228 }
229 }
230 #endregion
231 }
232
233
234 }

  ViewVC Help
Powered by ViewVC 1.1.22