Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/SortedList.cs @ 11211

Last change on this file since 11211 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 5.2 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37#if SILVERLIGHT
38using System.Collections;
39using System.Collections.Generic;
40
41namespace Google.ProtocolBuffers
42{
43    /// <summary>
44    /// Dictionary implementation which always yields keys in sorted order.
45    /// This is not particularly efficient: it wraps a normal dictionary
46    /// for most operations, but sorts by key when either iterating or
47    /// fetching the Keys/Values properties.
48    /// This is only used for Silverlight, which doesn't have the normal
49    /// sorted collections.
50    /// </summary>
51    internal sealed class SortedList<TKey, TValue> : IDictionary<TKey, TValue>
52    {
53        private readonly IDictionary<TKey, TValue> wrapped = new Dictionary<TKey, TValue>();
54
55        public SortedList()
56        {
57        }
58
59        public SortedList(IDictionary<TKey, TValue> dictionary)
60        {
61            foreach (KeyValuePair<TKey, TValue> entry in dictionary)
62            {
63                Add(entry.Key, entry.Value);
64            }
65        }
66
67        public void Add(TKey key, TValue value)
68        {
69            wrapped.Add(key, value);
70        }
71
72        public bool ContainsKey(TKey key)
73        {
74            return wrapped.ContainsKey(key);
75        }
76
77        public ICollection<TKey> Keys
78        {
79            get
80            {
81                List<TKey> keys = new List<TKey>(wrapped.Count);
82                foreach (var pair in this)
83                {
84                    keys.Add(pair.Key);
85                }
86                return keys;
87            }
88        }
89
90        public bool Remove(TKey key)
91        {
92            return wrapped.Remove(key);
93        }
94
95        public bool TryGetValue(TKey key, out TValue value)
96        {
97            return wrapped.TryGetValue(key, out value);
98        }
99
100        public ICollection<TValue> Values
101        {
102            get
103            {
104                List<TValue> values = new List<TValue>(wrapped.Count);
105                foreach (var pair in this)
106                {
107                    values.Add(pair.Value);
108                }
109                return values;
110            }
111        }
112
113        public TValue this[TKey key]
114        {
115            get { return wrapped[key]; }
116            set { wrapped[key] = value; }
117        }
118
119        public void Add(KeyValuePair<TKey, TValue> item)
120        {
121            wrapped.Add(item);
122        }
123
124        public void Clear()
125        {
126            wrapped.Clear();
127        }
128
129        public bool Contains(KeyValuePair<TKey, TValue> item)
130        {
131            return wrapped.Contains(item);
132        }
133
134        public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
135        {
136            wrapped.CopyTo(array, arrayIndex);
137        }
138
139        public int Count
140        {
141            get { return wrapped.Count; }
142        }
143
144        public bool IsReadOnly
145        {
146            get { return wrapped.IsReadOnly; }
147        }
148
149        public bool Remove(KeyValuePair<TKey, TValue> item)
150        {
151            return wrapped.Remove(item);
152        }
153
154        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
155        {
156            IComparer<TKey> comparer = Comparer<TKey>.Default;
157            var list = new List<KeyValuePair<TKey, TValue>>(wrapped);
158            list.Sort((x, y) => comparer.Compare(x.Key, y.Key));
159            return list.GetEnumerator();
160        }
161
162        IEnumerator IEnumerable.GetEnumerator()
163        {
164            return GetEnumerator();
165        }
166    }
167}
168
169#endif
Note: See TracBrowser for help on using the repository browser.