CSharp examples for System:Array Element
Shrink Array
/*/*from w w w . jav a2s. c om*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Reflection; using System.Diagnostics; using System.Collections.Generic; using System; public class Main{ [CLSCompliant(false)] public static float[][] Shrink(float[][] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, RamUsageEstimator.NUM_BYTES_OBJECT_REF); if (newSize != array.Length) { float[][] newArray = new float[newSize][]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } [CLSCompliant(false)] public static int[][] Shrink(int[][] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, RamUsageEstimator.NUM_BYTES_OBJECT_REF); if (newSize != array.Length) { int[][] newArray = new int[newSize][]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } public static char[] Shrink(char[] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, RamUsageEstimator.NUM_BYTES_CHAR); if (newSize != array.Length) { char[] newArray = new char[newSize]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } public static bool[] Shrink(bool[] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, 1); if (newSize != array.Length) { bool[] newArray = new bool[newSize]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } public static byte[] Shrink(byte[] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, 1); if (newSize != array.Length) { var newArray = new byte[newSize]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } public static long[] Shrink(long[] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, RamUsageEstimator.NUM_BYTES_INT64); if (newSize != array.Length) { long[] newArray = new long[newSize]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } public static int[] Shrink(int[] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, RamUsageEstimator.NUM_BYTES_INT32); if (newSize != array.Length) { int[] newArray = new int[newSize]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } public static short[] Shrink(short[] array, int targetSize) { Debug.Assert(targetSize >= 0, "size must be positive (got " + targetSize + "): likely integer overflow?"); int newSize = GetShrinkSize(array.Length, targetSize, RamUsageEstimator.NUM_BYTES_INT16); if (newSize != array.Length) { short[] newArray = new short[newSize]; Array.Copy(array, 0, newArray, 0, newSize); return newArray; } else { return array; } } }