/[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 128 - (show annotations) (download)
Wed Mar 13 13:45:05 2013 UTC (7 years, 8 months ago) by william
File size: 11890 byte(s)
+ current wip

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 [Serializable]
14 internal 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 this.GetEnumerator(); }
41
42 public override string ToString()
43 {
44 return string.Format("Property Count: {0}", PropertyCount);
45 }
46 }
47 [Serializable]
48 internal class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary
49 {
50 public PropertyDictionary() : base() { }
51 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 [Serializable]
59 internal class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
60 {
61 [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 public override string ToString()
69 {
70 return new PropertyValuePair<TKey, TValue>(name, value).ToString();
71 }
72 }
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(this.properties));
99 return v;
100 }
101 }
102 private Dictionary<TKey, TValue> properties;
103 public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
104 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
105 public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
106 public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
107 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
108 public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
109 protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
110 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 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 public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
156 public void ClearProperties() { properties.Clear(); }
157 public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
158 public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex)
159 {
160 var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
161 list.CopyTo(array, arrayIndex);
162 }
163 public bool RemoveProperty(IPropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }
164 public IEnumerator<IPropertyValuePair<TKey, TValue>> GetEnumerator()
165 {
166 //return new PropertyDictionaryEnumerator(this, 2);
167 List<IPropertyValuePair<TKey, TValue>> list = new List<IPropertyValuePair<TKey, TValue>>();
168 foreach (var k in properties) { list.Add(new PropertyValuePair<TKey, TValue>(k.Key, k.Value)); }
169 return list.GetEnumerator();
170 }
171 IEnumerator IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
172
173 public override string ToString()
174 {
175 return string.Format("Property Count: {0}", PropertyCount);
176 }
177 #region enumerator support
178 //public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable
179 //{
180 // private PropertyDictionary<TKey, TValue> dictionary;
181 // private int index;
182 // private IPropertyValuePair<TKey, TValue> current;
183 // private int getEnumeratorRetType;
184 // internal const int DictEntry = 1;
185 // internal const int KeyValuePair = 2;
186 // private int version;
187 // public PropertyDictionaryEnumerator(PropertyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType)
188 // {
189 // this.dictionary = dictionary;
190 // //this.version = dictionary.version;
191 // this.index = 0;
192 // this.getEnumeratorRetType = getEnumeratorRetType;
193 // this.current = new PropertyValuePair<TKey, TValue>();
194 // }
195 // public IPropertyValuePair<TKey, TValue> Current
196 // {
197 // get { return this.current; }
198 // }
199 // public void Dispose()
200 // {
201 // }
202 // object IEnumerator.Current
203 // {
204 // get
205 // {
206 // if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1)))
207 // {
208 // throw new InvalidOperationException("Operation can't happen");
209 // }
210 // if (this.getEnumeratorRetType == 1)
211 // {
212 // return new DictionaryEntry(this.current.Name, this.current.Value);
213 // }
214 // return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value);
215 // }
216 // }
217 // public bool MoveNext()
218 // {
219 // //if (this.version != this.dictionary.version)
220 // //{
221 // // ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
222 // //}
223 // while (this.index < this.dictionary.Count())
224 // {
225 // if (this.dictionary.entries[this.index].hashCode >= 0)
226 // {
227 // this.current =(IPropertyValuePair<TKey,TValue>)(PropertyValuePair<TKey,TValue>)new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
228 // this.index++;
229 // return true;
230 // }
231 // this.index++;
232 // }
233 // this.index = this.dictionary.Count() + 1;
234 // this.current = new PropertyValuePair<TKey, TValue>();
235 // return false;
236 // }
237 // public void Reset()
238 // {
239 // //if (this.version != this.dictionary.version)
240 // //{
241 // // ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
242 // //}
243 // this.index = 0;
244 // this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>();
245 // }
246 //}
247 #endregion
248 }
249
250
251 }

  ViewVC Help
Powered by ViewVC 1.1.22