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 |
{ |
{ |
26 |
public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); } |
public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); } |
27 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } |
28 |
} |
} |
29 |
|
public class PropertyDictionary : PropertyDictionary<string, object> |
30 |
|
{ |
31 |
|
public PropertyDictionary() : base() { } |
32 |
|
public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { } |
33 |
|
public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { } |
34 |
|
public PropertyDictionary(int capacity) :base(capacity) { } |
35 |
|
public PropertyDictionary(IPropertyDictionary dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer) { } |
36 |
|
public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { } |
37 |
|
protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } |
38 |
|
} |
39 |
public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue> |
public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue> |
40 |
{ |
{ |
41 |
|
[StructLayout(LayoutKind.Sequential)] |
42 |
|
private struct Entry |
43 |
|
{ |
44 |
|
public int hashCode; |
45 |
|
public int next; |
46 |
|
public TKey name; |
47 |
|
public TValue value; |
48 |
|
} |
49 |
|
private Entry[] entries |
50 |
|
{ |
51 |
|
get{ |
52 |
|
List<Entry> list = new List<Entry>(); |
53 |
|
int index = 0; |
54 |
|
foreach (var t in properties) |
55 |
|
{ |
56 |
|
Entry entry = new Entry(); |
57 |
|
entry.hashCode = this.properties.Comparer.GetHashCode(t.Key) & 0x7fffffff; |
58 |
|
entry.next = index + 1; |
59 |
|
entry.name = t.Key; |
60 |
|
entry.value = t.Value; |
61 |
|
list.Add(entry); |
62 |
|
index++; |
63 |
|
} |
64 |
|
return list.ToArray(); |
65 |
|
} |
66 |
|
} |
67 |
|
|
68 |
|
private int version |
69 |
|
{ |
70 |
|
get |
71 |
|
{ |
72 |
|
Type t = typeof(Dictionary<TKey, TValue>); |
73 |
|
var field = t.GetField("version", BindingFlags.NonPublic | BindingFlags.Instance); |
74 |
|
var v = Convert.ToInt32(field.GetValue(properties)); |
75 |
|
return v; |
76 |
|
} |
77 |
|
} |
78 |
private Dictionary<TKey, TValue> properties; |
private Dictionary<TKey, TValue> properties; |
79 |
public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); } |
public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); } |
80 |
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); } } |
81 |
public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); } |
public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); } |
82 |
public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); } |
public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); } |
83 |
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); } } |
84 |
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); } |
85 |
protected PropertyDictionary(SerializationInfo info, StreamingContext context) |
protected PropertyDictionary(SerializationInfo info, StreamingContext context) { } |
|
{ |
|
|
} |
|
86 |
public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } } |
public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } } |
87 |
public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } } |
public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } } |
88 |
public TValue this[TKey key] |
public TValue this[TKey key] |
117 |
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); } |
118 |
public int PropertyCount { get { return properties.Count; } } |
public int PropertyCount { get { return properties.Count; } } |
119 |
public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } } |
public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } } |
120 |
public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); } |
public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); } |
121 |
public void ClearProperties() { properties.Clear(); } |
public void ClearProperties() { properties.Clear(); } |
122 |
public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); } |
public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); } |
123 |
public void CopyPropertiesTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { var list = properties.ToList(); list.CopyTo(array, arrayIndex); } |
public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex) |
124 |
public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); } |
{ |
125 |
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); } |
var list = properties.ToList().Cast<PropertyValuePair<TKey, TValue>>().ToList(); |
126 |
|
list.CopyTo(array, arrayIndex); |
127 |
|
} |
128 |
|
public bool RemoveProperty(PropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); } |
129 |
|
IEnumerator<PropertyValuePair<TKey, TValue>> IEnumerable<PropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); } |
130 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); } |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); } |
|
|
|
|
} |
|
131 |
|
|
132 |
public class PropertyDictionary : PropertyDictionary<string, object> |
#region enumerator support |
133 |
{ |
public class PropertyDictionaryEnumerator: IEnumerator<PropertyValuePair<TKey,TValue>>, IDisposable |
134 |
|
{ |
135 |
|
private PropertyDictionary<TKey, TValue> dictionary; |
136 |
|
private int index; |
137 |
|
private PropertyValuePair<TKey, TValue> current; |
138 |
|
private int getEnumeratorRetType; |
139 |
|
internal const int DictEntry = 1; |
140 |
|
internal const int KeyValuePair = 2; |
141 |
|
private int version; |
142 |
|
public PropertyDictionaryEnumerator(PropertyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType) |
143 |
|
{ |
144 |
|
this.dictionary = dictionary; |
145 |
|
this.getEnumeratorRetType = getEnumeratorRetType; |
146 |
|
} |
147 |
|
|
148 |
|
public PropertyValuePair<TKey, TValue> Current |
149 |
|
{ |
150 |
|
get { throw new NotImplementedException(); } |
151 |
|
} |
152 |
|
|
153 |
|
public void Dispose() |
154 |
|
{ |
155 |
|
throw new NotImplementedException(); |
156 |
|
} |
157 |
|
|
158 |
|
object IEnumerator.Current |
159 |
|
{ |
160 |
|
get |
161 |
|
{ |
162 |
|
if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1))) |
163 |
|
{ |
164 |
|
throw new InvalidOperationException("Operation can't happen"); |
165 |
|
} |
166 |
|
if (this.getEnumeratorRetType == 1) |
167 |
|
{ |
168 |
|
return new DictionaryEntry(this.current.Name, this.current.Value); |
169 |
|
} |
170 |
|
return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value); |
171 |
|
} |
172 |
|
} |
173 |
|
|
174 |
|
public bool MoveNext() |
175 |
|
{ |
176 |
|
if (this.version != this.dictionary.version) |
177 |
|
{ |
178 |
|
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); |
179 |
|
} |
180 |
|
while (this.index < this.dictionary.Count()) |
181 |
|
{ |
182 |
|
if (this.dictionary.entries[this.index].hashCode >= 0) |
183 |
|
{ |
184 |
|
this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value); |
185 |
|
this.index++; |
186 |
|
return true; |
187 |
|
} |
188 |
|
this.index++; |
189 |
|
} |
190 |
|
this.index = this.dictionary.Count() + 1; |
191 |
|
this.current = new PropertyValuePair<TKey, TValue>(); |
192 |
|
return false; |
193 |
|
|
194 |
|
|
195 |
|
} |
196 |
|
|
197 |
|
public void Reset() |
198 |
|
{ |
199 |
|
if (this.version != this.dictionary.version) |
200 |
|
{ |
201 |
|
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); |
202 |
|
} |
203 |
|
this.index = 0; |
204 |
|
this.current = new KeyValuePair<TKey, TValue>(); |
205 |
|
} |
206 |
|
} |
207 |
|
#endregion |
208 |
} |
} |
209 |
|
|
210 |
|
|
211 |
} |
} |