/[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 94 - (show annotations) (download)
Sun Mar 10 08:45:58 2013 UTC (7 years, 7 months ago) by william
File size: 10079 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 public bool IsReadOnly { get { return (items as ICollection<T>).IsReadOnly; } }
21 public void AddProperty(T item) { items.Add(item); }
22 public void ClearProperties() { items.Clear(); }
23 public bool ContainsProperty(T item) { return items.Contains(item); }
24 public void CopyPropertiesTo(T[] array, int arrayIndex) { items.CopyTo(array, arrayIndex); }
25 public bool RemoveProperty(T item) { return items.Remove(item); }
26 public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
27 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>
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;
79 public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
80 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); }
82 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.Name, d.Value); } }
84 public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
85 protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
86 public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
87 public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
88 public TValue this[TKey key]
89 {
90 get
91 {
92 if (this.ContainsProperty(key))
93 {
94 return properties[key];
95 }
96 else
97 {
98 throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
99 }
100 }
101 set
102 {
103 if (this.ContainsProperty(key))
104 {
105 properties[key] = value;
106 }
107 else
108 {
109 throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
110 }
111 }
112 }
113
114 public void AddProperty(TKey key, TValue value) { if (properties.ContainsKey(key)) { this[key] = value; } else { properties.Add(key, value); } }
115 public bool ContainsProperty(TKey key) { return properties.ContainsKey(key); }
116 public bool RemoveProperty(TKey key) { return properties.Remove(key); }
117 public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
118 public int PropertyCount { get { return properties.Count; } }
119 public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }
120 public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
121 public void ClearProperties() { properties.Clear(); }
122 public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
123 public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex)
124 {
125 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(); }
131
132 #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.version = dictionary.version;
146 this.index = 0;
147 this.getEnumeratorRetType = getEnumeratorRetType;
148 this.current = new PropertyValuePair<TKey, TValue>();
149 }
150
151 public PropertyValuePair<TKey, TValue> Current
152 {
153 get { return this.current; }
154 }
155
156 public void Dispose()
157 {
158 }
159
160 object IEnumerator.Current
161 {
162 get
163 {
164 if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1)))
165 {
166 throw new InvalidOperationException("Operation can't happen");
167 }
168 if (this.getEnumeratorRetType == 1)
169 {
170 return new DictionaryEntry(this.current.Name, this.current.Value);
171 }
172 return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value);
173 }
174 }
175
176 public bool MoveNext()
177 {
178 if (this.version != this.dictionary.version)
179 {
180 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
181 }
182 while (this.index < this.dictionary.Count())
183 {
184 if (this.dictionary.entries[this.index].hashCode >= 0)
185 {
186 this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
187 this.index++;
188 return true;
189 }
190 this.index++;
191 }
192 this.index = this.dictionary.Count() + 1;
193 this.current = new PropertyValuePair<TKey, TValue>();
194 return false;
195 }
196 public void Reset()
197 {
198 if (this.version != this.dictionary.version)
199 {
200 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
201 }
202 this.index = 0;
203 this.current = new KeyValuePair<TKey, TValue>();
204 }
205 }
206 #endregion
207 }
208
209
210 }

  ViewVC Help
Powered by ViewVC 1.1.22