The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.Qil \ SubstitutionList

  1. //------------------------------------------------------------------------------
  2. // <copyright file="SubstitutionList.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.Collections;
  17. using System.Diagnostics;
  18. namespace System.Xml.Xsl.Qil
  19. {
  20.    
  21.     /// <summary>
  22.     /// Data structure for use in CloneAndReplace
  23.     /// </summary>
  24.     /// <remarks>Isolates the many QilNode classes from changes in
  25.     /// the underlying data structure.</remarks>
  26.     internal sealed class SubstitutionList
  27.     {
  28.         private ArrayList s;
  29.        
  30.         public SubstitutionList()
  31.         {
  32.             this.s = new ArrayList(4);
  33.         }
  34.        
  35.         /// <summary>
  36.         /// Add a substituion pair
  37.         /// </summary>
  38.         /// <param name="find">a node to be replaced</param>
  39.         /// <param name="replace">its replacement</param>
  40.         public void AddSubstitutionPair(QilNode find, QilNode replace)
  41.         {
  42.             s.Add(find);
  43.             s.Add(replace);
  44.         }
  45.        
  46.         /// <summary>
  47.         /// Remove the last a substituion pair
  48.         /// </summary>
  49.         public void RemoveLastSubstitutionPair()
  50.         {
  51.             s.RemoveRange(s.Count - 2, 2);
  52.         }
  53.        
  54.         /// <summary>
  55.         /// Remove the last N substitution pairs
  56.         /// </summary>
  57.         public void RemoveLastNSubstitutionPairs(int n)
  58.         {
  59.             Debug.Assert(n >= 0, "n must be nonnegative");
  60.             if (n > 0) {
  61.                 n *= 2;
  62.                 s.RemoveRange(s.Count - n, n);
  63.             }
  64.         }
  65.        
  66.         /// <summary>
  67.         /// Find the replacement for a node
  68.         /// </summary>
  69.         /// <param name="n">the node to replace</param>
  70.         /// <returns>null if no replacement is found</returns>
  71.         public QilNode FindReplacement(QilNode n)
  72.         {
  73.             Debug.Assert(s.Count % 2 == 0);
  74.             for (int i = s.Count - 2; i >= 0; i -= 2)
  75.                 if (s[i] == n)
  76.                     return (QilNode)s[i + 1];
  77.             return null;
  78.         }
  79.     }
  80. }

Developer Fusion