File: XmlBinaryWriterSession.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (260 lines) | stat: -rw-r--r-- 7,228 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.Xml
{
    using System;
    using System.Xml;
    using System.Collections;
    using System.Diagnostics;
    using System.Runtime.Serialization;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    public class XmlBinaryWriterSession
    {
        PriorityDictionary<string, int> strings;
        PriorityDictionary<IXmlDictionary, IntArray> maps;
        int nextKey;

        public XmlBinaryWriterSession()
        {
            this.nextKey = 0;
            this.maps = new PriorityDictionary<IXmlDictionary, IntArray>();
            this.strings = new PriorityDictionary<string, int>();
        }

        public virtual bool TryAdd(XmlDictionaryString value, out int key)
        {
            IntArray keys;
            if (value == null)
                throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");

            if (maps.TryGetValue(value.Dictionary, out keys))
            {
                key = (keys[value.Key] - 1);

                if (key != -1)
                {
                    // If the key is already set, then something is wrong
                    throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlKeyAlreadyExists)));
                }

                key = Add(value.Value);
                keys[value.Key] = (key + 1);
                return true;
            }

            key = Add(value.Value);
            keys = AddKeys(value.Dictionary, value.Key + 1);
            keys[value.Key] = (key + 1);
            return true;
        }

        int Add(string s)
        {
            int key = this.nextKey++;
            strings.Add(s, key);
            return key;
        }

        IntArray AddKeys(IXmlDictionary dictionary, int minCount)
        {
            IntArray keys = new IntArray(Math.Max(minCount, 16));
            maps.Add(dictionary, keys);
            return keys;
        }

        public void Reset()
        {
            nextKey = 0;
            maps.Clear();
            strings.Clear();
        }

        internal bool TryLookup(XmlDictionaryString s, out int key)
        {
            IntArray keys;
            if (maps.TryGetValue(s.Dictionary, out keys))
            {
                key = (keys[s.Key] - 1);

                if (key != -1)
                {
                    return true;
                }
            }

            if (strings.TryGetValue(s.Value, out key))
            {
                if (keys == null)
                {
                    keys = AddKeys(s.Dictionary, s.Key + 1);
                }

                keys[s.Key] = (key + 1);
                return true;
            }

            key = -1;
            return false;
        }

        class PriorityDictionary<K, V> where K : class
        {
            Dictionary<K, V> dictionary;
            Entry[] list;
            int listCount;
            int now;

            public PriorityDictionary()
            {
                list = new Entry[16];
            }

            public void Clear()
            {
                now = 0;
                listCount = 0;
                Array.Clear(list, 0, list.Length);
                if (dictionary != null)
                    dictionary.Clear();
            }

            public bool TryGetValue(K key, out V value)
            {
                for (int i = 0; i < listCount; i++)
                {
                    if (list[i].Key == key)
                    {
                        value = list[i].Value;
                        list[i].Time = Now;
                        return true;
                    }
                }

                for (int i = 0; i < listCount; i++)
                {
                    if (list[i].Key.Equals(key))
                    {
                        value = list[i].Value;
                        list[i].Time = Now;
                        return true;
                    }
                }

                if (dictionary == null)
                {
                    value = default(V);
                    return false;
                }

                if (!dictionary.TryGetValue(key, out value))
                {
                    return false;
                }

                int minIndex = 0;
                int minTime = list[0].Time;
                for (int i = 1; i < listCount; i++)
                {
                    if (list[i].Time < minTime)
                    {
                        minIndex = i;
                        minTime = list[i].Time;
                    }
                }

                list[minIndex].Key = key;
                list[minIndex].Value = value;
                list[minIndex].Time = Now;
                return true;
            }

            public void Add(K key, V value)
            {
                if (listCount < list.Length)
                {
                    list[listCount].Key = key;
                    list[listCount].Value = value;
                    listCount++;
                }
                else
                {
                    if (dictionary == null)
                    {
                        dictionary = new Dictionary<K, V>();
                        for (int i = 0; i < listCount; i++)
                        {
                            dictionary.Add(list[i].Key, list[i].Value);
                        }
                    }

                    dictionary.Add(key, value);
                }
            }

            int Now
            {
                get
                {
                    if (++now == int.MaxValue)
                    {
                        DecreaseAll();
                    }

                    return now;
                }
            }

            void DecreaseAll()
            {
                for (int i = 0; i < listCount; i++)
                {
                    list[i].Time /= 2;
                }

                now /= 2;
            }

            struct Entry
            {
                public K Key;
                public V Value;
                public int Time;
            }
        }

        class IntArray
        {
            int[] array;

            public IntArray(int size)
            {
                this.array = new int[size];
            }

            public int this[int index]
            {
                get
                {
                    if (index >= array.Length)
                        return 0;

                    return array[index];
                }
                set
                {
                    if (index >= array.Length)
                    {
                        int[] newArray = new int[Math.Max(index + 1, array.Length * 2)];
                        Array.Copy(array, newArray, array.Length);
                        array = newArray;
                    }

                    array[index] = value;
                }
            }
        }
    }
}