Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / Runtime / XmlSortKeyAccumulator.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XmlSortKeyAccumulator.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7 using System;
8 using System.Diagnostics;
9 using System.Globalization;
10 using System.ComponentModel;
11
12 namespace System.Xml.Xsl.Runtime {
13
14     /// <summary>
15     /// Accumulates a list of sort keys and stores them in an array.
16     /// </summary>
17     [EditorBrowsable(EditorBrowsableState.Never)]
18     public struct XmlSortKeyAccumulator {
19         private XmlSortKey[] keys;
20         private int pos;
21
22     #if DEBUG
23         private const int DefaultSortKeyCount = 4;
24     #else
25         private const int DefaultSortKeyCount = 64;
26     #endif
27
28         /// <summary>
29         /// Initialize the XmlSortKeyAccumulator.
30         /// </summary>
31         public void Create() {
32             if (this.keys == null)
33                 this.keys = new XmlSortKey[DefaultSortKeyCount];
34
35             this.pos = 0;
36             this.keys[0] = null;
37         }
38
39         /// <summary>
40         /// Create a new sort key and append it to the current run of sort keys.
41         /// </summary>
42         public void AddStringSortKey(XmlCollation collation, string value) {
43             AppendSortKey(collation.CreateSortKey(value));
44         }
45
46         public void AddDecimalSortKey(XmlCollation collation, decimal value) {
47             AppendSortKey(new XmlDecimalSortKey(value, collation));
48         }
49
50         public void AddIntegerSortKey(XmlCollation collation, long value) {
51             AppendSortKey(new XmlIntegerSortKey(value, collation));
52         }
53
54         public void AddIntSortKey(XmlCollation collation, int value) {
55             AppendSortKey(new XmlIntSortKey(value, collation));
56         }
57
58         public void AddDoubleSortKey(XmlCollation collation, double value) {
59             AppendSortKey(new XmlDoubleSortKey(value, collation));
60         }
61
62         public void AddDateTimeSortKey(XmlCollation collation, DateTime value) {
63             AppendSortKey(new XmlDateTimeSortKey(value, collation));
64         }
65
66         public void AddEmptySortKey(XmlCollation collation) {
67             AppendSortKey(new XmlEmptySortKey(collation));
68         }
69
70         /// <summary>
71         /// Finish creating the current run of sort keys and begin a new run.
72         /// </summary>
73         public void FinishSortKeys() {
74             this.pos++;
75             if (this.pos >= this.keys.Length) {
76                 XmlSortKey[] keysNew = new XmlSortKey[this.pos * 2];
77                 Array.Copy(this.keys, 0, keysNew, 0, this.keys.Length);
78                 this.keys = keysNew;
79             }
80             this.keys[this.pos] = null;
81         }
82
83         /// <summary>
84         /// Append new sort key to the current run of sort keys.
85         /// </summary>
86         private void AppendSortKey(XmlSortKey key) {
87             // Ensure that sort will be stable by setting index of key
88             key.Priority = this.pos;
89
90             if (this.keys[this.pos] == null)
91                 this.keys[this.pos] = key;
92             else
93                 this.keys[this.pos].AddSortKey(key);
94         }
95
96         /// <summary>
97         /// Get array of sort keys that was constructed by this internal class.
98         /// </summary>
99         public Array Keys {
100             get { return this.keys; }
101         }
102     }
103 }