# HG changeset patch # Parent 55e0dadf7ad564741da146b77cf1e5dc30af6846 [mq]: CSHARP-257 diff -r 55e0dadf7ad5 git/Bson/ObjectModel/BsonArray.cs --- a/git/Bson/ObjectModel/BsonArray.cs Tue Jul 12 14:15:24 2011 +0400 +++ b/git/Bson/ObjectModel/BsonArray.cs Tue Jul 12 14:16:47 2011 +0400 @@ -56,6 +56,28 @@ /// /// A list of values to add to the array. public BsonArray( + bool[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -78,6 +100,28 @@ /// /// A list of values to add to the array. public BsonArray( + DateTime[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -89,6 +133,28 @@ /// /// A list of values to add to the array. public BsonArray( + double[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -100,6 +166,28 @@ /// /// A list of values to add to the array. public BsonArray( + int[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -111,6 +199,28 @@ /// /// A list of values to add to the array. public BsonArray( + long[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -122,6 +232,28 @@ /// /// A list of values to add to the array. public BsonArray( + object[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -133,6 +265,28 @@ /// /// A list of values to add to the array. public BsonArray( + ObjectId[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( IEnumerable values ) : this(0) { @@ -142,6 +296,28 @@ /// /// Initializes a new instance of the BsonArray class. /// + /// A list of values to add to the array. + public BsonArray( + string[] values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// + /// A list of values to add to the array. + public BsonArray( + IList values + ) + : this(0) { + AddRange(values); + } + + /// + /// Initializes a new instance of the BsonArray class. + /// /// The initial capacity of the array. public BsonArray( int capacity @@ -225,6 +401,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + bool[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -255,6 +461,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + DateTime[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -270,6 +506,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + double[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -285,6 +551,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + int[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -300,6 +596,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + long[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -315,6 +641,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + object[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -330,6 +686,36 @@ /// A list of values to add to the array. /// A BsonArray or null. public static BsonArray Create( + ObjectId[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( IEnumerable values ) { if (values != null) { @@ -342,6 +728,36 @@ /// /// Creates a new BsonArray. /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + string[] values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// + /// A list of values to add to the array. + /// A BsonArray or null. + public static BsonArray Create( + IList values + ) { + if (values != null) { + return new BsonArray(values); + } else { + return null; + } + } + + /// + /// Creates a new BsonArray. + /// /// A value to be mapped to a BsonArray. /// A BsonArray or null. public new static BsonArray Create( @@ -396,10 +812,68 @@ public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as bool[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonBoolean.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonBoolean.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonBoolean.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + bool[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonBoolean.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonBoolean.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonBoolean.Create(values[i])); } return this; } @@ -426,10 +900,68 @@ public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as DateTime[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDateTime.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDateTime.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonDateTime.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + DateTime[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonDateTime.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDateTime.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDateTime.Create(values[i])); } return this; } @@ -442,9 +974,33 @@ public BsonArray AddRange( IEnumerable values ) { - if (values != null) { - foreach (var value in values) { - this.values.Add(BsonDouble.Create(value)); + var valuesArray = values as double[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDouble.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDouble.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonDouble.Create(value)); + } } } return this; @@ -453,15 +1009,108 @@ /// /// Adds multiple elements to the array. /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + double[] values + ) { + if (values != null) { + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDouble.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonDouble.Create(values[i])); + } + return this; + } + + + /// + /// Adds multiple elements to the array. + /// /// A list of values to add to the array. /// The array (so method calls can be chained). public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as int[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt32.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt32.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonInt32.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + int[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonInt32.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt32.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt32.Create(values[i])); } return this; } @@ -474,10 +1123,68 @@ public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as long[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt64.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt64.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonInt64.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + long[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonInt64.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt64.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonInt64.Create(values[i])); } return this; } @@ -490,10 +1197,68 @@ public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as object[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonValue.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonValue.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonValue.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + object[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonValue.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonValue.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonValue.Create(values[i])); } return this; } @@ -506,10 +1271,68 @@ public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as ObjectId[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonObjectId.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonObjectId.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonObjectId.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + ObjectId[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonObjectId.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonObjectId.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonObjectId.Create(values[i])); } return this; } @@ -522,10 +1345,68 @@ public BsonArray AddRange( IEnumerable values ) { + var valuesArray = values as string[]; + if (valuesArray != null) + { + int count = valuesArray.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonString.Create(valuesArray[i])); + } + else + { + var valuesList = values as IList; + if (valuesList != null) + { + int count = valuesList.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonString.Create(valuesList[i])); + } + else + { + var valuesCollection = values as ICollection; + Grow(valuesCollection != null ? valuesCollection.Count : 0); + if (values != null) + { + foreach (var value in values) + this.values.Add(BsonString.Create(value)); + } + } + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + string[] values + ) { if (values != null) { - foreach (var value in values) { - this.values.Add(BsonString.Create(value)); - } + int count = values.Length; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonString.Create(values[i])); + } + return this; + } + + /// + /// Adds multiple elements to the array. + /// + /// An array of values to add to the array. + /// The array (so method calls can be chained). + public BsonArray AddRange( + IList values + ) { + if (values != null) { + int count = values.Count; + Grow(count); + for (int i = 0; i < count; i++) + this.values.Add(BsonString.Create(values[i])); } return this; } @@ -535,10 +1416,10 @@ /// /// A shallow clone of the array. public override BsonValue Clone() { - var clone = new BsonArray(values.Capacity); - foreach (var value in values) { - clone.Add(value.Clone()); - } + int count = values.Count; + var clone = new BsonArray(count); + for (int i = 0; i < count; i++) + clone.values.Add(values[i].Clone()); return clone; } @@ -625,10 +1506,10 @@ /// /// A deep clone of the array. public override BsonValue DeepClone() { - var clone = new BsonArray(values.Capacity); - foreach (var value in values) { - clone.Add(value.DeepClone()); - } + int count = values.Count; + var clone = new BsonArray(count); + for (int i = 0; i < count; i++) + clone.values.Add(values[i].DeepClone()); return clone; } @@ -807,5 +1688,14 @@ return GetEnumerator(); } #endregion + + #region private members + void Grow(int count) + { + int capacity = this.values.Count + count; + if (this.values.Capacity < capacity) + this.values.Capacity = capacity; + } + #endregion } }