/[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 92 - (hide annotations) (download)
Sun Mar 10 08:21:30 2013 UTC (7 years, 7 months ago) by william
File size: 9999 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     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 william 90 public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
40     {
41 william 92 [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 william 90 private Dictionary<TKey, TValue> properties;
79     public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
80 william 92 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
81 william 90 public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
82     public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
83 william 92 public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } }
84 william 90 public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
85 william 92 protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
86 william 90 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 william 92 public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
121 william 90 public void ClearProperties() { properties.Clear(); }
122 william 92 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 william 90 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
131 william 92
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.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 william 90 }
209    
210 william 92
211 william 90 }

  ViewVC Help
Powered by ViewVC 1.1.22