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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 90 by william, Sat Mar 9 17:42:34 2013 UTC revision 122 by william, Sun Mar 10 19:22:06 2013 UTC
# Line 4  using System.Linq; Line 4  using System.Linq;
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  {  {
13        [Serializable]
14      public class PropertyCollection<T> : IPropertyCollection<T>      internal class PropertyCollection<T> : IPropertyCollection<T>
15      {      {
16          private List<T> items = new List<T>();          private List<T> items = new List<T>();
17          public PropertyCollection() : this(new List<T>()) { }          public PropertyCollection() : this(new List<T>()) { }
18          public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }          public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }
19          public int PropertyCount { get { return items.Count; } }          public int PropertyCount { get { return items.Count; } }
20          public bool IsReadOnly { get { return (items as ICollection<T>).IsReadOnly; } }  
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); }          public void AddProperty(T item) { items.Add(item); }
35          public void ClearProperties() { items.Clear(); }          public void ClearProperties() { items.Clear(); }
36          public bool ContainsProperty(T item) { return items.Contains(item); }          public bool ContainsProperty(T item) { return items.Contains(item); }
# Line 22  namespace libxmltv.Core Line 38  namespace libxmltv.Core
38          public bool RemoveProperty(T item) { return items.Remove(item); }          public bool RemoveProperty(T item) { return items.Remove(item); }
39          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
40          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
     }  
41    
42      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>          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;          private Dictionary<TKey, TValue> properties;
103          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
104          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); } }
105          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
106          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }          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.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); } }
108          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); }
109          protected PropertyDictionary(SerializationInfo info, StreamingContext context)          protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
         {  
         }  
110          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
111          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
112          public TValue this[TKey key]          public TValue this[TKey key]
# Line 69  namespace libxmltv.Core Line 140  namespace libxmltv.Core
140          public bool RemoveProperty(TKey key) { return properties.Remove(key); }          public bool RemoveProperty(TKey key) { return properties.Remove(key); }
141          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); }
142          public int PropertyCount { get { return properties.Count; } }          public int PropertyCount { get { return properties.Count; } }
143          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }          public bool IsReadOnly
144          public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); }          {
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(); }          public void ClearProperties() { properties.Clear(); }
157          public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); }          public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
158          public void CopyPropertiesTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { var list = properties.ToList(); list.CopyTo(array, arrayIndex); }          public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex)
159          public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); }          {
160          public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); }              var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
161          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }              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 class PropertyDictionary : PropertyDictionary<string, object>          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  }  }

Legend:
Removed from v.90  
changed lines
  Added in v.122

  ViewVC Help
Powered by ViewVC 1.1.22