/[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 93 by william, Sun Mar 10 08:44:53 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  {  {
# Line 23  namespace libxmltv.Core Line 26  namespace libxmltv.Core
26          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
27          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
28      }      }
29        public class PropertyDictionary : PropertyDictionary<string, object>
30        {
31            public PropertyDictionary() : base()
32            {
33                ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);
34            }
35            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      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
43      {      {
44            [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          private Dictionary<TKey, TValue> properties;          private Dictionary<TKey, TValue> properties;
82          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
83          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); } }
84          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
85          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
86          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); } }
87          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); }
88          protected PropertyDictionary(SerializationInfo info, StreamingContext context)          protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
         {  
         }  
89          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
90          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
91          public TValue this[TKey key]          public TValue this[TKey key]
# Line 70  namespace libxmltv.Core Line 120  namespace libxmltv.Core
120          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); }
121          public int PropertyCount { get { return properties.Count; } }          public int PropertyCount { get { return properties.Count; } }
122          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }
123          public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); }          public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
124          public void ClearProperties() { properties.Clear(); }          public void ClearProperties() { properties.Clear(); }
125          public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); }          public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
126          public void CopyPropertiesTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { var list = properties.ToList(); list.CopyTo(array, arrayIndex); }          public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex)
127          public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); }          {
128          public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); }              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          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
         
     }  
134    
135      public class PropertyDictionary : PropertyDictionary<string, object>          #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                    this.version = dictionary.version;
149                    this.index = 0;
150                    this.getEnumeratorRetType = getEnumeratorRetType;
151                    this.current = new PropertyValuePair<TKey, TValue>();
152                }
153    
154                public PropertyValuePair<TKey, TValue> Current
155                {
156                    get { return this.current; }
157                }
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      }      }
211    
212      
213  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.22