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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (hide annotations) (download)
Sun Mar 10 08:44:53 2013 UTC (7 years, 6 months ago) by william
File size: 10202 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22