/[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 94 by william, Sun Mar 10 08:45:58 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            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      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
40      {      {
41            [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          private Dictionary<TKey, TValue> properties;          private Dictionary<TKey, TValue> properties;
79          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }          public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
80          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); } }
81          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }          public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
82          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }          public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
83          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); } }
84          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); }
85          protected PropertyDictionary(SerializationInfo info, StreamingContext context)          protected PropertyDictionary(SerializationInfo info, StreamingContext context) { }
         {  
         }  
86          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }          public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
87          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }          public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
88          public TValue this[TKey key]          public TValue this[TKey key]
# Line 70  namespace libxmltv.Core Line 117  namespace libxmltv.Core
117          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); }
118          public int PropertyCount { get { return properties.Count; } }          public int PropertyCount { get { return properties.Count; } }
119          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }
120          public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); }          public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
121          public void ClearProperties() { properties.Clear(); }          public void ClearProperties() { properties.Clear(); }
122          public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); }          public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
123          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)
124          public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); }          {
125          public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); }              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          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
         
     }  
131    
132      public class PropertyDictionary : PropertyDictionary<string, object>          #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.version = dictionary.version;
146                    this.index = 0;
147                    this.getEnumeratorRetType = getEnumeratorRetType;
148                    this.current = new PropertyValuePair<TKey, TValue>();
149                }
150    
151                public PropertyValuePair<TKey, TValue> Current
152                {
153                    get { return this.current; }
154                }
155    
156                public void Dispose()
157                {
158                }
159    
160                object IEnumerator.Current
161                {
162                    get
163                    {
164                        if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1)))
165                        {
166                            throw new InvalidOperationException("Operation can't happen");
167                        }
168                        if (this.getEnumeratorRetType == 1)
169                        {
170                            return new DictionaryEntry(this.current.Name, this.current.Value);
171                        }
172                        return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value);
173                    }
174                }
175    
176                public bool MoveNext()
177                {
178                    if (this.version != this.dictionary.version)
179                    {
180                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
181                    }
182                    while (this.index < this.dictionary.Count())
183                    {
184                        if (this.dictionary.entries[this.index].hashCode >= 0)
185                        {
186                            this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
187                            this.index++;
188                            return true;
189                        }
190                        this.index++;
191                    }
192                    this.index = this.dictionary.Count() + 1;
193                    this.current = new PropertyValuePair<TKey, TValue>();
194                    return false;
195                }
196                public void Reset()
197                {
198                    if (this.version != this.dictionary.version)
199                    {
200                        ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
201                    }
202                    this.index = 0;
203                    this.current = new KeyValuePair<TKey, TValue>();
204                }
205            }
206            #endregion  
207      }      }
208    
209      
210  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.22