Using array initializer - c#

I have a class called Matrix : IEnumerable<double>, (classic, mathematical matrix. It is basically a 2D array with some goodies).
The class is immutable, so there is no way to change its values after instance creation.
If want to create a matrix with pre-existing values I have to pass an array to the constructor like this:
double[,] arr = new double[,]
{
{1,2,3}, {4,5,6}, {7,8,9}
};
Matrix m = new Matrix(arr);
Is there a way to turn it into this: (?)
Matrix m = new Matrix
{
{1,2,3}, {4,5,6}, {7,8,9}
};
Update:
Found a hack-ish way yo make it work. I'm not sure if this solution is advisable, but it works.
class Matrix : ICloneable, IEnumerable<double>
{
// Height & Width are initialized in a constructor beforehand.
/*
* Usage:
* var mat = new Matrix(3, 4)
* {
* {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}
* };
*/
int rowIndex;
bool allowArrayInitializer;
double[,] tempData;
double[,] data;
public void Add(params double[] args)
{
if(!allowArrayInitializer)
throw new InvalidOperationException("Cannot use array initializer");
if(args.Length != Width || rowIndex >= Height)
throw new InvalidOperationException("Invalid array initializer.");
for(int i = 0; i < Width; i++)
tempData[i, rowIndex] = args[i];
if(++rowIndex == Height)
data = tempData;
}
}

Not if it’s immutable; that syntactic sugar is implemented using Add().

You will not able to do it via initializer but should be able to do it via parameterized constructor.
You can see the sample code below :
class Matrix : IEnumerable<double>
{
double[,] input;
public Matrix(double[,] inputArray)
{
input = inputArray;
}
public IEnumerator<double> GetEnumerator()
{
return (IEnumerator<double>)input.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return input.GetEnumerator();
}
}
In the main method :
static void Main(string[] args)
{
var m = new Matrix(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
}
I hope this helps you!

instead of deriving from IEnumerable I would use a property:
class Matrix
{
public double[,] Arr { get; set; }
}
Matrix m = new Matrix
{
Arr = new double [,] { {1d,2d,3d}, { 4d,5d, 6d}}
};

Related

C# function for creating jagged array

I have a problem where I want to create a jagged array. But the size of this jagged array is variable.
Does this work the same way as filling an normal array like this?
int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
terms[runs] = value;
}
The values I use are defined like this:
public static string[] nodeCassette0 = { "03", "08" };
public static string[] nodeCassette1 = { "04", "09" };
public static string[] nodeCassette2 = { "05", "10" };
public static string[] nodeCassette3 = { "06", "11" };
public static string[] nodeCassette4 = { "07", "12" };
Depending on the size required by the user a new variable nodeCassette needs to be filled according to the required size. The order in which the array is filled is always the same, it starts at 0 and ends at 4.
This should be like that but I don't know how to dynamically create it:
public string[][] nodeCassette =
{
nodeCassette0,
nodeCassette1,
nodeCassette2,
nodeCassette3,
nodeCassette4
};
Here's an example of how to create such a jagged array:
int[] sizes = new int[] { 3, 2, 1, 1, 3 };
int[][] jagged = new int[sizes.Length][];
for (int i = 0; i < sizes.Length; i++)
{
jagged[i] = new int[sizes[i]];
for (int j = 0; j < sizes[i]; j++)
{
jagged[i][j] = (i + 1) * (j + 1);
}
}
That produces:
It's no different with strings:
int[] sizes = new int[] { 3, 2, 1, 1, 3 };
string[][] jagged = new string[sizes.Length][];
for (int i = 0; i < sizes.Length; i++)
{
jagged[i] = new string[sizes[i]];
for (int j = 0; j < sizes[i]; j++)
{
jagged[i][j] = $"{i}_{j}";
}
}
We can use reflexion and Linq.
using System.Reflection;
using System.Linq;
Having this dedicated class to store the arrays by default, as well as initialize a complete list of possible elements, and offer a method to obtain a slice, i.e. a list made up of the first n-elements:
static class DefaultArrays
{
static public string[] NodeCassette0 = { "03", "08" };
static public string[] NodeCassette1 = { "04", "09" };
static public string[] NodeCassette2 = { "05", "10" };
static public string[] NodeCassette3 = { "06", "11" };
static public string[] NodeCassette4 = { "07", "12" };
static public List<string[]> All { get; private set; }
static public List<string[]> Take(int count)
{
return All.Take(count).ToList();
}
static public void Initialize()
{
All = typeof(DefaultArrays).GetFields()
.Where(field => field.FieldType == typeof(string[]))
.OrderBy(field => field.Name.Length)
.ThenBy(field => field.Name)
.Select(field => (string[])field.GetValue(null))
.ToList();
}
static DefaultArrays()
{
Initialize();
}
}
The Initialize method is created this way to possibly allow updating of the entire list in case the arrays are modified at runtime, otherwise the code can be placed directly in the constructor and all arrays marked readonly.
Here is the algorithm for All:
We get all fields of the static class by its type name.
We filter to select only those being of type array of string.
We order by the fields name string length.
Then by this name, thus we finally get for example 1, 2, 30, 40 from 40, 30, 2, 1.
We do a projection to get the reference of the value instead of the field name using it.
And we transform the Linq query into a List<string> object instance to return.
It uses a pseudo-natural numeric sort assuming all arrays are named:
"[same_same][number_without_trailing_left_0]"
Else we can use a custom comparer:
How do I sort strings alphabetically while accounting for value when a string is numeric?
sort string-numbers
Test
var slice = DefaultArrays.Take(3);
string msg = string.Join(Environment.NewLine, slice.Select(item => string.Join(", ", item)));
Console.WriteLine(msg);
Output
03, 08
04, 09
05, 10
Warning
The slice contains a list of references toward original arrays, thus any modification of a cell of an array in this list will be reflected in the matching array in the static class.
Else we need to copy the arrays with Array.Copy instead of using Linq.Take:
static public List<string[]> TakeCopy(int count)
{
var list = new List<string[]>();
foreach ( int index in Enumerable.Range(0, Math.Min(count, All.Count)))
{
int length = All[index].Length;
var array = new string[length];
Array.Copy(All[index], array, length);
list.Add(array);
}
return list;
}

How to extract a row from an array in C#?

note: BJ Myers comment was useful and was the answer in fact. However, as it was a comment, I couldn't mark that as an answer but I've placed the corrected code (using his advice) at the end of this question.
Original question below continues:
This situation may look weird at first but here is what I intend to do:
Similar to the syntax in Python, instead of creating a multidimensional array (a 2-d array, to be exact), I want to create an array of arrays (a vector of vectors, in fact).
I'm aware that C# will not let me create pointers in safe code, but I'm still curious whether there is a safer way to accomplish this task w/o getting of the safe code limits.
So, I came up with the code below but couldn't figure out how to extract a specific row from the array (as shown between the comment lines).
Is it possible to pass the r'th row at once or do I need to create another temporary storage for r'th row and then pass that temporary vector through?
(System: Windows-10, VS-2013, C#)
using System;
public class Vector {
public double[] data;
public Vector(double[] data) {
this.data = new double[data.GetLength(0)];
this.data = data;
}
}
public class Matrix {
private int row, col;
public Matrix(double[,] data) {
this.row = data.GetLength(0);
this.col = data.GetLength(1);
Vector[] v = new Vector[this.row];
for (int r = 0; r < this.row; r++) {
// ****** this line below ******
v[r] = new Vector(data[r,???]);
// ****** how to extract the r'th row ******
}
}
static void Main(string[] args) {
double[,] data = { { 9.0, 8.0, 7.0 }, { 5.0, 6.0, 4.0 }, { 3.0, 2.0, 2.0 } };
Matrix A = new Matrix(data);
Console.ReadLine();
}
}
The corrected code is below:
using System;
public class Vector {
public double[] data;
public Vector(double[] data) {
this.data = new double[data.GetLength(0)];
this.data = data;
for (int i = 0; i < data.GetLength(0); i++) {
Console.Write("{0: 0.000 }", this.data[i]);
}
Console.WriteLine();
}
}
public class Matrix {
private int row, col;
public Matrix(double[][] data) {
this.row = data.GetLength(0);
this.col = data[0].GetLength(0);
Vector[] v = new Vector[this.row];
for (int r = 0; r < row; r++) {
v[r] = new Vector(data[r]);
}
Console.WriteLine("rows: " + this.row.ToString());
Console.WriteLine("cols: " + this.col.ToString());
}
static void Main(string[] args) {
double[][] data = { new double[] { 9.0, 8.0, 7.0 },
new double[] { 5.0, 6.0, 4.0 },
new double[] { 3.0, 2.0, 2.0 } };
Matrix A = new Matrix(data);
Console.ReadLine();
}
}
Well, you want to make an array class and acess like one? Make an indexer. what is an indexer? - it's a way to make your class accessible like an array.
Look over the link for examples, I'll help you with your specific case.
public class Vector {
public double[] data;
public double this[int i]
{
get
{
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array.
return data[i];
}
set
{
data[i] = value;
}
}
public Vector(double[] data) {
this.data = new double[data.GetLength(0)];
this.data = data;
}
}
once it's defined like so, this is perfectly valid:
double elementArray = new double[data.GetLength(1)]; // declaring an array, the size of the second dimention of the data array.
for(int i =0; i<data.GetLength(1);i++)
{
elementArray[i] = data[r,i]; // adding all the elements to the list
}
v[r] = new Vector(elementArray);
EDIT: BJ Myers' comment is right, this solution works perfectly for a jagged array too, but make sure that you declare it properly like he mentioned.
EDIT 2: Using a list is pointless here, changed the stracture to an array.

Array of array initialization by using unmanaged pointers in C#

I would like to define an array of array eg int[][] on top of an existing int[] of the right size. I want then to seamlessy use either the int[][] array of array or the int[] 'big' array while refering to the same inner data.
Is this possible to do this in C# using unmanaged code? In C++ I would defined pointers like this :
int* bigArray = new int[numberOfRows * numberOfColumns];
int** matrix = new int*[numberOfRows];
for (int i = 0; i < numberOfRows; i ++)
{
matrix[i] = (bigArray + i * numberOfColumns);
}
Yes, you can also use pointer in C#, but there are some limits on it. 1st, you need to declare the function with unsafe, and also, you need to set the build option to allow unsafe code in the project property, here is the sample code about it:
unsafe static void T1(int numberOfRows, int numberOfColumns)
{
int* bigArray = stackalloc int[numberOfRows * numberOfColumns];
int** matrix = stackalloc int*[numberOfRows];
for (int i = 0; i < numberOfRows; i++)
{
matrix[i] = (bigArray + i * numberOfColumns);
}
}
Remember, you can use the pointer only if you are so clear of that you have full control of your code. while it is not recommend you use the pointer in C#
You can do it in managed code, using a class as a wrapper and providing it with an indexer:
class Matrix
{
public readonly int[] BigArray; // make available
private int _rowSize = ...;
public int this[int x, int y]
{
get { return BigArray [x*_rowSize+y]; }
set { BigArray [x*_rowSize+y] = value; }
}
}
Edit:
changed arrray into public readonly field so that the class has a dual interface
Edit 2:
Using T[][] to provide access to T[] rows.
class Matrix2<T>
{
private T[][] _data;
private int _columns;
public Matrix2(int rows, int cols)
{
_data = new T[rows][];
_columns = cols;
for (int i = 0; i < rows; i++) _data[i] = new T[cols];
}
public T this [int x]
{
get { return _data [x/_columns][x % _columns]; }
set { _data [x/_columns][x % _columns] = value; }
}
public T[] Row(int r)
{
return _data [r];
}
}

How do I clone a range of array elements to a new array?

I have an array X of 10 elements. I would like to create a new array containing all the elements from X that begin at index 3 and ends in index 7. Sure I can easily write a loop that will do it for me but I would like to keep my code as clean as possible. Is there a method in C# that can do it for me?
Something like (pseudo code):
Array NewArray = oldArray.createNewArrayFromRange(int BeginIndex , int EndIndex)
Array.Copy doesn't fit my needs. I need the items in the new array to be clones. Array.copy is just a C-Style memcpy equivalent, it's not what I'm looking for.
You could add it as an extension method:
public static T[] SubArray<T>(this T[] data, int index, int length)
{
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
static void Main()
{
int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] sub = data.SubArray(3, 4); // contains {3,4,5,6}
}
Update re cloning (which wasn't obvious in the original question). If you really want a deep clone; something like:
public static T[] SubArrayDeepClone<T>(this T[] data, int index, int length)
{
T[] arrCopy = new T[length];
Array.Copy(data, index, arrCopy, 0, length);
using (MemoryStream ms = new MemoryStream())
{
var bf = new BinaryFormatter();
bf.Serialize(ms, arrCopy);
ms.Position = 0;
return (T[])bf.Deserialize(ms);
}
}
This does require the objects to be serializable ([Serializable] or ISerializable), though. You could easily substitute for any other serializer as appropriate - XmlSerializer, DataContractSerializer, protobuf-net, etc.
Note that deep clone is tricky without serialization; in particular, ICloneable is hard to trust in most cases.
You can use Array.Copy(...) to copy into the new array after you've created it, but I don't think there's a method which creates the new array and copies a range of elements.
If you're using .NET 3.5 you could use LINQ:
var newArray = array.Skip(3).Take(5).ToArray();
but that will be somewhat less efficient.
See this answer to a similar question for options for more specific situations.
Have you considered using ArraySegment?
http://msdn.microsoft.com/en-us/library/1hsbd92d.aspx
I see you want to do Cloning, not just copying references.
In this case you can use .Select to project array members to their clones.
For example, if your elements implemented IClonable you could do something like this:
var newArray = array.Skip(3).Take(5).Select(eachElement => eachElement.Clone()).ToArray();
Note: This solution requires .NET Framework 3.5.
The following code does it in one line:
// Source array
string[] Source = new string[] { "A", "B", "C", "D" };
// Extracting a slice into another array
string[] Slice = new List<string>(Source).GetRange(2, 2).ToArray();
In C# 8, they've introduced a new Range and Index type, which can be used like this:
int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Index i1 = 3; // number 3 from beginning
Index i2 = ^4; // number 4 from end
var slice = a[i1..i2]; // { 3, 4, 5 }
References:
https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#ranges-and-indices
https://devblogs.microsoft.com/dotnet/building-c-8-0/
string[] arr = { "Parrot" , "Snake" ,"Rabbit" , "Dog" , "cat" };
arr = arr.ToList().GetRange(0, arr.Length -1).ToArray();
Building on Marc's answer but adding the desired cloning behaviour
public static T[] CloneSubArray<T>(this T[] data, int index, int length)
where T : ICloneable
{
T[] result = new T[length];
for (int i = 0; i < length; i++)
{
var original = data[index + i];
if (original != null)
result[i] = (T)original.Clone();
return result;
}
And if implementing ICloneable is too much like hard work a reflective one using Håvard Stranden’s Copyable library to do the heavy lifting required.
using OX.Copyable;
public static T[] DeepCopySubArray<T>(
this T[] data, int index, int length)
{
T[] result = new T[length];
for (int i = 0; i < length; i++)
{
var original = data[index + i];
if (original != null)
result[i] = (T)original.Copy();
return result;
}
Note that the OX.Copyable implementation works with any of:
For the automated copy to work, though, one of the following statements must hold for instance:
Its type must have a parameterless constructor, or
It must be a Copyable, or
It must have an IInstanceProvider registered for its type.
So this should cover almost any situation you have. If you are cloning objects where the sub graph contains things like db connections or file/stream handles you obviously have issues but that it true for any generalized deep copy.
If you want to use some other deep copy approach instead this article lists several others so I would suggest not trying to write your own.
You can do this fairly easially;
object[] foo = new object[10];
object[] bar = new object[7];
Array.Copy(foo, 3, bar, 0, 7);
I think that the code you are looking for is:
Array.Copy(oldArray, 0, newArray, BeginIndex, EndIndex - BeginIndex)
As an alternative to copying the data you can make a wrapper that gives you access to a part of the original array as if it was a copy of the part of the array. The advantage is that you don't get another copy of the data in memory, and the drawback is a slight overhead when accessing the data.
public class SubArray<T> : IEnumerable<T> {
private T[] _original;
private int _start;
public SubArray(T[] original, int start, int len) {
_original = original;
_start = start;
Length = len;
}
public T this[int index] {
get {
if (index < 0 || index >= Length) throw new IndexOutOfRangeException();
return _original[_start + index];
}
}
public int Length { get; private set; }
public IEnumerator<T> GetEnumerator() {
for (int i = 0; i < Length; i++) {
yield return _original[_start + i];
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}
Usage:
int[] original = { 1, 2, 3, 4, 5 };
SubArray<int> copy = new SubArray<int>(original, 2, 2);
Console.WriteLine(copy.Length); // shows: 2
Console.WriteLine(copy[0]); // shows: 3
foreach (int i in copy) Console.WriteLine(i); // shows 3 and 4
In C# 8.0, you can now do many fancier works including reverse indices and ranges like in Python, such as:
int[] list = {1, 2, 3, 4, 5, 6};
var list2 = list[2..5].Clone() as int[]; // 3, 4, 5
var list3 = list[..5].Clone() as int[]; // 1, 2, 3, 4, 5
var list4 = list[^4..^0].Clone() as int[]; // reverse index
Array.ConstrainedCopy will work.
public static void ConstrainedCopy (
Array sourceArray,
int sourceIndex,
Array destinationArray,
int destinationIndex,
int length
)
It does not meet your cloning requirement, but it seems simpler than many answers to do:
Array NewArray = new ArraySegment(oldArray,BeginIndex , int Count).ToArray();
There's no single method that will do what you want. You will need to make a clone method available for the class in your array. Then, if LINQ is an option:
Foo[] newArray = oldArray.Skip(3).Take(5).Select(item => item.Clone()).ToArray();
class Foo
{
public Foo Clone()
{
return (Foo)MemberwiseClone();
}
}
How about useing Array.ConstrainedCopy:
int[] ArrayOne = new int[8] {1,2,3,4,5,6,7,8};
int[] ArrayTwo = new int[5];
Array.ConstrainedCopy(ArrayOne, 3, ArrayTwo, 0, 7-3);
Below is my original post. It will not work
You could use Array.CopyTo:
int[] ArrayOne = new int[8] {1,2,3,4,5,6,7,8};
int[] ArrayTwo = new int[5];
ArrayOne.CopyTo(ArrayTwo,3); //starts copy at index=3 until it reaches end of
//either array
How about this:
public T[] CloneCopy(T[] array, int startIndex, int endIndex) where T : ICloneable
{
T[] retArray = new T[endIndex - startIndex];
for (int i = startIndex; i < endIndex; i++)
{
array[i - startIndex] = array[i].Clone();
}
return retArray;
}
You then need to implement the ICloneable interface on all of the classes you need to use this on but that should do it.
I'm not sure how deep it really is, but:
MyArray.ToList<TSource>().GetRange(beginningIndex, endIndex).ToArray()
It's a bit of overhead, but it might cut out an unnecessary method.
As far as cloning goes, I don't think serialization calls your constructors. This may break class invariants if you're doing interesting things in the ctor's.
It seems the safer bet is virtual clone methods calling copy constructors.
protected MyDerivedClass(MyDerivedClass myClass)
{
...
}
public override MyBaseClass Clone()
{
return new MyDerivedClass(this);
}
Cloning elements in an array is not something that can be done in a universal way. Do you want deep cloning or a simple copy of all members?
Let's go for the "best effort" approach: cloning objects using the ICloneable interface or binary serialization:
public static class ArrayExtensions
{
public static T[] SubArray<T>(this T[] array, int index, int length)
{
T[] result = new T[length];
for (int i=index;i<length+index && i<array.Length;i++)
{
if (array[i] is ICloneable)
result[i-index] = (T) ((ICloneable)array[i]).Clone();
else
result[i-index] = (T) CloneObject(array[i]);
}
return result;
}
private static object CloneObject(object obj)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, obj);
stream.Seek(0,SeekOrigin.Begin);
return formatter.Deserialize(stream);
}
}
}
This is not a perfect solution, because there simply is none that will work for any type of object.
You can take class made by Microsoft:
internal class Set<TElement>
{
private int[] _buckets;
private Slot[] _slots;
private int _count;
private int _freeList;
private readonly IEqualityComparer<TElement> _comparer;
public Set()
: this(null)
{
}
public Set(IEqualityComparer<TElement> comparer)
{
if (comparer == null)
comparer = EqualityComparer<TElement>.Default;
_comparer = comparer;
_buckets = new int[7];
_slots = new Slot[7];
_freeList = -1;
}
public bool Add(TElement value)
{
return !Find(value, true);
}
public bool Contains(TElement value)
{
return Find(value, false);
}
public bool Remove(TElement value)
{
var hashCode = InternalGetHashCode(value);
var index1 = hashCode % _buckets.Length;
var index2 = -1;
for (var index3 = _buckets[index1] - 1; index3 >= 0; index3 = _slots[index3].Next)
{
if (_slots[index3].HashCode == hashCode && _comparer.Equals(_slots[index3].Value, value))
{
if (index2 < 0)
_buckets[index1] = _slots[index3].Next + 1;
else
_slots[index2].Next = _slots[index3].Next;
_slots[index3].HashCode = -1;
_slots[index3].Value = default(TElement);
_slots[index3].Next = _freeList;
_freeList = index3;
return true;
}
index2 = index3;
}
return false;
}
private bool Find(TElement value, bool add)
{
var hashCode = InternalGetHashCode(value);
for (var index = _buckets[hashCode % _buckets.Length] - 1; index >= 0; index = _slots[index].Next)
{
if (_slots[index].HashCode == hashCode && _comparer.Equals(_slots[index].Value, value))
return true;
}
if (add)
{
int index1;
if (_freeList >= 0)
{
index1 = _freeList;
_freeList = _slots[index1].Next;
}
else
{
if (_count == _slots.Length)
Resize();
index1 = _count;
++_count;
}
int index2 = hashCode % _buckets.Length;
_slots[index1].HashCode = hashCode;
_slots[index1].Value = value;
_slots[index1].Next = _buckets[index2] - 1;
_buckets[index2] = index1 + 1;
}
return false;
}
private void Resize()
{
var length = checked(_count * 2 + 1);
var numArray = new int[length];
var slotArray = new Slot[length];
Array.Copy(_slots, 0, slotArray, 0, _count);
for (var index1 = 0; index1 < _count; ++index1)
{
int index2 = slotArray[index1].HashCode % length;
slotArray[index1].Next = numArray[index2] - 1;
numArray[index2] = index1 + 1;
}
_buckets = numArray;
_slots = slotArray;
}
internal int InternalGetHashCode(TElement value)
{
if (value != null)
return _comparer.GetHashCode(value) & int.MaxValue;
return 0;
}
internal struct Slot
{
internal int HashCode;
internal TElement Value;
internal int Next;
}
}
and then
public static T[] GetSub<T>(this T[] first, T[] second)
{
var items = IntersectIteratorWithIndex(first, second);
if (!items.Any()) return new T[] { };
var index = items.First().Item2;
var length = first.Count() - index;
var subArray = new T[length];
Array.Copy(first, index, subArray, 0, length);
return subArray;
}
private static IEnumerable<Tuple<T, Int32>> IntersectIteratorWithIndex<T>(IEnumerable<T> first, IEnumerable<T> second)
{
var firstList = first.ToList();
var set = new Set<T>();
foreach (var i in second)
set.Add(i);
foreach (var i in firstList)
{
if (set.Remove(i))
yield return new Tuple<T, Int32>(i, firstList.IndexOf(i));
}
}
This is the optimal way, I found, to do this:
private void GetSubArrayThroughArraySegment() {
int[] array = { 10, 20, 30 };
ArraySegment<int> segment = new ArraySegment<int>(array, 1, 2);
Console.WriteLine("-- Array --");
int[] original = segment.Array;
foreach (int value in original)
{
Console.WriteLine(value);
}
Console.WriteLine("-- Offset --");
Console.WriteLine(segment.Offset);
Console.WriteLine("-- Count --");
Console.WriteLine(segment.Count);
Console.WriteLine("-- Range --");
for (int i = segment.Offset; i <= segment.Count; i++)
{
Console.WriteLine(segment.Array[i]);
}
}
Hope It Helps!
use extention method :
public static T[] Slice<T>(this T[] source, int start, int end)
{
// Handles negative ends.
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
// Return new array.
T[] res = new T[len];
for (int i = 0; i < len; i++)
{
res[i] = source[i + start];
}
return res;
}
and you can use it
var NewArray = OldArray.Slice(3,7);
Code from the System.Private.CoreLib.dll:
public static T[] GetSubArray<T>(T[] array, Range range)
{
if (array == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}
(int Offset, int Length) offsetAndLength = range.GetOffsetAndLength(array.Length);
int item = offsetAndLength.Offset;
int item2 = offsetAndLength.Length;
if (default(T) != null || typeof(T[]) == array.GetType())
{
if (item2 == 0)
{
return Array.Empty<T>();
}
T[] array2 = new T[item2];
Buffer.Memmove(ref Unsafe.As<byte, T>(ref array2.GetRawSzArrayData()), ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), item), (uint)item2);
return array2;
}
T[] array3 = (T[])Array.CreateInstance(array.GetType().GetElementType(), item2);
Array.Copy(array, item, array3, 0, item2);
return array3;
}
array1 = [5,6,7,8];
int[] array2 = new int[2];
Array.ConstrainedCopy(array1, 1, array2, 0, 2);
array2 = [6,7];
Array.ConstrainedCopy takes five (5) parameters:
source array (sourceArray)
starting index of source array (sourceIndex)
destination array (destinationArray)
starting index of destination array (destinationIndex)
number of elements to copy (length)
public static T[] SubArray<T>(T[] data, int index, int length)
{
List<T> retVal = new List<T>();
if (data == null || data.Length == 0)
return retVal.ToArray();
bool startRead = false;
int count = 0;
for (int i = 0; i < data.Length; i++)
{
if (i == index && !startRead)
startRead = true;
if (startRead)
{
retVal.Add(data[i]);
count++;
if (count == length)
break;
}
}
return retVal.ToArray();
}

How to remove duplicates from int[][]

I have an array of arrays - information about selection in Excel using VSTO, where each element means start and end selection position.
For example,
int[][] selection = {
new int[] { 1 }, // column A
new int[] { 6 }, // column F
new int[] { 6 }, // column F
new int[] { 8, 9 } // columns H:I
new int[] { 8, 9 } // columns H:I
new int[] { 12, 15 } // columns L:O
};
Could you please help me to find a way, maybe using LINQ or Extension methods, to remove duplicated elements? I mean: F and F, H:I and H:I, etc.
If you want to use a pure LINQ/extension method solution, then you'll need to define your own implementation of IEqualityComparer for arrays/sequences. (Unless I'm missing something obvious, there's no pre-existing array or sequence comparer in the BCL). This isn't terribly hard however - here's an example of one that should do the job pretty well:
public class SequenceEqualityComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
{
return Enumerable.SequenceEqual(x, y);
}
// Probably not the best hash function for an ordered list, but it should do the job in most cases.
public int GetHashCode(IEnumerable<T> obj)
{
int hash = 0;
int i = 0;
foreach (var element in obj)
hash = unchecked((hash * 37 + hash) + (element.GetHashCode() << (i++ % 16)));
return hash;
}
}
The advantage of this is that you can then simply call the following to remove any duplicate arrays.
var result = selection.Distinct(new SequenceEqualityComparer<int>()).ToArray();
Hope that helps.
First you need a way to compare the integer arrays. To use it with the classes in the framework, you do that by making an EquailtyComparer. If the arrays are always sorted, that is rather easy to implement:
public class IntArrayComparer : IEqualityComparer<int[]> {
public bool Equals(int[] x, int[] y) {
if (x.Length != y.Length) return false;
for (int i = 0; i < x.Length; i++) {
if (x[i] != y[i]) return false;
}
return true;
}
public int GetHashCode(int[] obj) {
int code = 0;
foreach (int value in obj) code ^= value;
return code;
}
}
Now you can use an integer array as key in a HashSet to get the unique arrays:
int[][] selection = {
new int[] { 1 }, // column A
new int[] { 6 }, // column F
new int[] { 6 }, // column F
new int[] { 8, 9 }, // columns H:I
new int[] { 8, 9 }, // columns H:I
new int[] { 12, 15 } // columns L:O
};
HashSet<int[]> arrays = new HashSet<int[]>(new IntArrayComparer());
foreach (int[] array in selection) {
arrays.Add(array);
}
The HashSet just throws away duplicate values, so it now contains four integer arrays.

Categories

Resources