The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.Runtime \ XmlSortKeyAccumulator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlSortKeyAccumulator.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Diagnostics;
  17. using System.Globalization;
  18. using System.ComponentModel;
  19. namespace System.Xml.Xsl.Runtime
  20. {
  21.    
  22.     /// <summary>
  23.     /// Accumulates a list of sort keys and stores them in an array.
  24.     /// </summary>
  25.     [EditorBrowsable(EditorBrowsableState.Never)]
  26.     public struct XmlSortKeyAccumulator
  27.     {
  28.         private XmlSortKey[] keys;
  29.         private int pos;
  30.        
  31.         #if DEBUG
  32.         private const int DefaultSortKeyCount = 4;
  33.         #else
  34.         private const int DefaultSortKeyCount = 64;
  35.         #endif
  36.        
  37.         /// <summary>
  38.         /// Initialize the XmlSortKeyAccumulator.
  39.         /// </summary>
  40.         public void Create()
  41.         {
  42.             if (this.keys == null)
  43.                 this.keys = new XmlSortKey[DefaultSortKeyCount];
  44.            
  45.             this.pos = 0;
  46.             this.keys[0] = null;
  47.         }
  48.        
  49.         /// <summary>
  50.         /// Create a new sort key and append it to the current run of sort keys.
  51.         /// </summary>
  52.         public void AddStringSortKey(XmlCollation collation, string value)
  53.         {
  54.             AppendSortKey(collation.CreateSortKey(value));
  55.         }
  56.        
  57.         public void AddDecimalSortKey(XmlCollation collation, decimal value)
  58.         {
  59.             AppendSortKey(new XmlDecimalSortKey(value, collation));
  60.         }
  61.        
  62.         public void AddIntegerSortKey(XmlCollation collation, long value)
  63.         {
  64.             AppendSortKey(new XmlIntegerSortKey(value, collation));
  65.         }
  66.        
  67.         public void AddIntSortKey(XmlCollation collation, int value)
  68.         {
  69.             AppendSortKey(new XmlIntSortKey(value, collation));
  70.         }
  71.        
  72.         public void AddDoubleSortKey(XmlCollation collation, double value)
  73.         {
  74.             AppendSortKey(new XmlDoubleSortKey(value, collation));
  75.         }
  76.        
  77.         public void AddDateTimeSortKey(XmlCollation collation, DateTime value)
  78.         {
  79.             AppendSortKey(new XmlDateTimeSortKey(value, collation));
  80.         }
  81.        
  82.         public void AddEmptySortKey(XmlCollation collation)
  83.         {
  84.             AppendSortKey(new XmlEmptySortKey(collation));
  85.         }
  86.        
  87.         /// <summary>
  88.         /// Finish creating the current run of sort keys and begin a new run.
  89.         /// </summary>
  90.         public void FinishSortKeys()
  91.         {
  92.             this.pos++;
  93.             if (this.pos >= this.keys.Length) {
  94.                 XmlSortKey[] keysNew = new XmlSortKey[this.pos * 2];
  95.                 Array.Copy(this.keys, 0, keysNew, 0, this.keys.Length);
  96.                 this.keys = keysNew;
  97.             }
  98.             this.keys[this.pos] = null;
  99.         }
  100.        
  101.         /// <summary>
  102.         /// Append new sort key to the current run of sort keys.
  103.         /// </summary>
  104.         private void AppendSortKey(XmlSortKey key)
  105.         {
  106.             // Ensure that sort will be stable by setting index of key
  107.             key.Priority = this.pos;
  108.            
  109.             if (this.keys[this.pos] == null)
  110.                 this.keys[this.pos] = key;
  111.             else
  112.                 this.keys[this.pos].AddSortKey(key);
  113.         }
  114.        
  115.         /// <summary>
  116.         /// Get array of sort keys that was constructed by this internal class.
  117.         /// </summary>
  118.         public Array Keys {
  119.             get { return this.keys; }
  120.         }
  121.     }
  122. }

Developer Fusion