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 |
98 |
|
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 |
|
|
} |
42 |
william |
95 |
public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary |
43 |
william |
92 |
{ |
44 |
william |
94 |
public PropertyDictionary() : base() { } |
45 |
william |
92 |
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 |
william |
90 |
public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue> |
53 |
|
|
{ |
54 |
william |
92 |
[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 |
william |
90 |
private Dictionary<TKey, TValue> properties; |
92 |
|
|
public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); } |
93 |
william |
92 |
public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } } |
94 |
william |
90 |
public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); } |
95 |
|
|
public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); } |
96 |
william |
92 |
public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } } |
97 |
william |
90 |
public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); } |
98 |
william |
92 |
protected PropertyDictionary(SerializationInfo info, StreamingContext context) { } |
99 |
william |
90 |
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 |
william |
98 |
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 |
william |
92 |
public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); } |
145 |
william |
90 |
public void ClearProperties() { properties.Clear(); } |
146 |
william |
92 |
public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); } |
147 |
|
|
public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex) |
148 |
|
|
{ |
149 |
|
|
var list = properties.ToList().Cast<PropertyValuePair<TKey, TValue>>().ToList(); |
150 |
|
|
list.CopyTo(array, arrayIndex); |
151 |
|
|
} |
152 |
|
|
public bool RemoveProperty(PropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); } |
153 |
|
|
IEnumerator<PropertyValuePair<TKey, TValue>> IEnumerable<PropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); } |
154 |
william |
90 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); } |
155 |
william |
92 |
|
156 |
|
|
#region enumerator support |
157 |
|
|
public class PropertyDictionaryEnumerator: IEnumerator<PropertyValuePair<TKey,TValue>>, IDisposable |
158 |
|
|
{ |
159 |
|
|
private PropertyDictionary<TKey, TValue> dictionary; |
160 |
|
|
private int index; |
161 |
|
|
private PropertyValuePair<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 |
william |
93 |
this.version = dictionary.version; |
170 |
|
|
this.index = 0; |
171 |
william |
92 |
this.getEnumeratorRetType = getEnumeratorRetType; |
172 |
william |
93 |
this.current = new PropertyValuePair<TKey, TValue>(); |
173 |
william |
92 |
} |
174 |
|
|
|
175 |
|
|
public PropertyValuePair<TKey, TValue> Current |
176 |
|
|
{ |
177 |
william |
93 |
get { return this.current; } |
178 |
william |
92 |
} |
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 = 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 = new KeyValuePair<TKey, TValue>(); |
228 |
|
|
} |
229 |
|
|
} |
230 |
|
|
#endregion |
231 |
william |
90 |
} |
232 |
|
|
|
233 |
william |
92 |
|
234 |
william |
90 |
} |