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
|
//------------------------------------------------------------------------------
// <copyright file="XmlNamedNodeMap.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">lionelu</owner>
//------------------------------------------------------------------------------
namespace System.Xml {
using System.Collections;
partial class XmlNamedNodeMap {
// Optimized to minimize space in the zero or one element cases.
internal struct SmallXmlNodeList {
// If field is null, that represents an empty list.
// If field is non-null, but not an ArrayList, then the 'list' contains a single
// object.
// Otherwise, field is an ArrayList. Once the field upgrades to an ArrayList, it
// never degrades back, even if all elements are removed.
private object field;
public int Count {
get {
if (field == null)
return 0;
ArrayList list = field as ArrayList;
if (list != null)
return list.Count;
return 1;
}
}
public object this[int index] {
get {
if (field == null)
throw new ArgumentOutOfRangeException("index");
ArrayList list = field as ArrayList;
if (list != null)
return list[index];
if (index != 0)
throw new ArgumentOutOfRangeException("index");
return field;
}
}
public void Add(object value) {
if (field == null) {
if (value == null) {
// If a single null value needs to be stored, then
// upgrade to an ArrayList
ArrayList temp = new ArrayList();
temp.Add(null);
field = temp;
}
else
field = value;
return;
}
ArrayList list = field as ArrayList;
if (list != null) {
list.Add(value);
}
else {
list = new ArrayList();
list.Add(field);
list.Add(value);
field = list;
}
}
public void RemoveAt(int index) {
if (field == null)
throw new ArgumentOutOfRangeException("index");
ArrayList list = field as ArrayList;
if (list != null) {
list.RemoveAt(index);
return;
}
if (index != 0)
throw new ArgumentOutOfRangeException("index");
field = null;
}
public void Insert(int index, object value) {
if (field == null) {
if (index != 0)
throw new ArgumentOutOfRangeException("index");
Add(value);
return;
}
ArrayList list = field as ArrayList;
if (list != null) {
list.Insert(index, value);
return;
}
if (index == 0) {
list = new ArrayList();
list.Add(value);
list.Add(field);
field = list;
}
else if (index == 1) {
list = new ArrayList();
list.Add(field);
list.Add(value);
field = list;
}
else {
throw new ArgumentOutOfRangeException("index");
}
}
class SingleObjectEnumerator : IEnumerator {
object loneValue;
int position = -1;
public SingleObjectEnumerator(object value) {
loneValue = value;
}
public object Current {
get {
if (position != 0) {
throw new InvalidOperationException();
}
return this.loneValue;
}
}
public bool MoveNext() {
if (position < 0) {
position = 0;
return true;
}
position = 1;
return false;
}
public void Reset() {
position = -1;
}
}
public IEnumerator GetEnumerator() {
if (field == null) {
return XmlDocument.EmptyEnumerator;
}
ArrayList list = field as ArrayList;
if (list != null) {
return list.GetEnumerator();
}
return new SingleObjectEnumerator(field);
}
}
}
}
|