I was trying to compare a 2D array in C# using IComparer but not able to compile the code from my point of view in sort method ab is assumed to be a jagad array instead of a normal array .Does any one know how to solve that
int[,] ab = new int[3, 4]
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 2, 3, 4, 5 }
};
Array.Sort<int[]>(ab, new ComparerTwoDArray());
foreach (var i in ab)
{
Console.WriteLine(i);
}
class ComparerTwoDArray : IComparer<int[]>
{
int ix;
public int Compare(int[] x, int[] y)
{
return x[0].CompareTo(y[0]);
}
}
You are not using the proper method: https://msdn.microsoft.com/en-us/library/system.array.sort%28v=vs.110%29.aspx
Array.Sort Method: Sorts the elements in a one-dimensional array.
You could use List<List<int>> and then use its Sort LINQ extension, like:
list.Sort((x,y) => x[0].CompareTo(y[0]));
Unfortunately a multi-dimensional array is a quite static beast. You can easily access single elements within it by providing the n-dimensional coordinates (eg. array[2,8,4] in a 3d array), but you can't access any whole areas (like rows, columns, rectangles, etc.).
If you really have the need to do such a kind of re-sorting you shouldn't save your data within a multi-dimensional array. Instead put it into a jagged array or a list of lists or even a IReadOnlyDictionary<int, IReadOnlyDictionary<int, IReadOnlyDictionary<int, int>>> for a 4 dimensions sparse matrix. In that case you could quite easily write a comparer for each dimension as you like.
In your case you just need a something like List<IReadOnlyList<int>> and a IComparer<IReadOnlyList<T>> which could possibly be implemented like this:
public class ListComparer<T> : IComparer<IReadOnlyList<T>>
{
public static readonly IComparer<IReadOnlyList<T>> Default = new ListComparer<T>();
private readonly bool _checkCount;
private readonly int _numberOfElementsToCompare;
private readonly IComparer<T> _elementComparer;
public ListComparer()
: this(true, 1, Comparer<T>.Default)
{
}
public ListComparer(
bool checkCount,
int numberOfElementsToCompare,
IComparer<T> elementComparer)
{
_checkCount = checkCount;
_numberOfElementsToCompare = numberOfElementsToCompare;
_elementComparer = elementComparer
?? throw new ArgumentNullException(nameof(elementComparer));
}
public int Compare(IReadOnlyList<T> x, IReadOnlyList<T> y)
{
if (ReferenceEquals(x, y))
return 0;
if (ReferenceEquals(x, null))
return -1;
if (ReferenceEquals(y, null))
return 1;
if (_checkCount)
{
var diff = x.Count.CompareTo(y.Count);
if (diff != 0)
return diff;
}
return x.Take(_numberOfElementsToCompare)
.Zip(y, (i, j) => _elementComparer.Compare(i, j))
.SkipWhile(value => value == 0)
.FirstOrDefault();
}
}
And would be used:
var matrix = new List<IReadOnlyList<int>>
{
{ new List<int> { 1, 2, 3, 4 } },
{ new List<int> { 5, 6, 7, 8 } },
{ new List<int> { 2, 3, 4, 5 } }
};
matrix.Sort(ListComparer<int>.Default);
foreach (var item in matrix)
{
Console.WriteLine(String.Join(", ", item));
}
Related
I have two classes
class CSparseMatrix:
{
public int NumberOfDimensions { get; set;}
public int DefaultNumber { get; set; }
List<int> dimensionsRanges = new List<int>(); // that's specify dimension of ranges, f.e. {100, 100, 100, 120..}
List<CSparseCell> cells = new List<CSparseCell>(); // contains only values different from default for this matrix
}
class CSparseCell {
public int Value { get; set; }
public List<int> coordinates = new List<int>();
}
And the problem is: how to loop through this CSparseMatrix and output all ranges with values in format like: [0, 0, 0, 0] - *value*, [0, 0, 0, 1] - *value*, [0, 0, 0, 2] - *value*, ...[dimensionsRanges[0]-1, dimensionsRanges[1]-1, dimensionsRanges[2]-1, dimensionsRanges[3]-1] - *value* so through all ranges and output all values (we can have any number of this dimensions).
That means that in program we had to to output all values of matrix, which can have any number of dimensions and ranges could be different. But we don't know what this number of dimensions will be so can't use n-nested loops, and actually I have no idea of algorithm or method how to iterate through all values from List<int> dimensionsRanges
The way, we get the specific value from this "matrix" is
public int GetValueFromCell(List<int> coordinate)
{
foreach(CSparseCell cell in cells)
{
if(cell.coordinates.All(coordinate.Contains)) {
return cell.Value;
}
}
return DefaultNumber;
}
Since you say your matrix is large, avoid returning CSparseCell by pushing the Value lookup to the answer computation.
You create a method to return all the coordinates in the sparse matrix, using a helper method to increment coordinates. NOTE: I changed the coordinate increment method to use a for loop instead of do which might be easier to understand (?).
void IncCoord(ref List<int> aCoord) { // ref not needed, just for documentation
for (var curDim = NumberOfDimensions - 1; curDim >= 0; --curDim) {
if (aCoord[curDim] == dimensionsRanges[curDim] - 1) // handle carry
aCoord[curDim] = 0;
else {
++aCoord[curDim];
break;
}
}
}
public IEnumerable<List<int>> AllCoords() {
var curCellCoord = Enumerable.Repeat(0, NumberOfDimensions).ToList();
var numCells = dimensionsRanges.Product();
for (int j1 = 0; j1 < numCells; ++j1) {
yield return curCellCoord.ToList();
IncCoord(ref curCellCoord);
}
}
Now you can use this method to get all Values and you can format the output however you like. Assuming t is a CSparseMatrix:
var ans = t.AllCoords().Select(c => $"[{c.Join(",")}] - {t.GetValueFromCell(c)}");
A couple of helper extension methods are used:
public static class IEnumerableExt {
public static int Product(this IEnumerable<int> src) => src.Aggregate(1, (a,n) => a * n);
}
public static class StringExt {
public static string Join<T>(this IEnumerable<T> items, string sep) => String.Join(sep, items);
}
I'd make a couple suggestions. First tying the coordinate and the value together complicate things. It'd be much better to separate those two.
Secondly, having to search the entire array to find a single cell makes it very inefficient. You can create a simple sparse matrix out of a dictionary. This not at all the best way but with the unknown key requirements you have given it's at least better than the array. (is it really a matrix if the key is 1..n ?)
Here's an example. First we make the coordinates their own type.
readonly struct SparseCoord : IEquatable<SparseCoord>
{
public static SparseCoord ToCoordinate(params int[] coords)
{
return new SparseCoord(coords);
}
public SparseCoord(int[] c)
{
this.Coordinate = new int[c?.Length ?? 0];
if (null != c)
c.CopyTo(this.Coordinate, 0);
}
public int[] Coordinate { get; }
public bool Equals([AllowNull] SparseCoord other)
{
return Enumerable.SequenceEqual(this.Coordinate, other.Coordinate);
}
public override bool Equals(object obj)
{
if (obj is SparseCoord c)
return this.Equals(c);
return false;
}
public override int GetHashCode()
{
var hash = new HashCode();
foreach (var i in this.Coordinate)
hash.Add(i);
return hash.ToHashCode();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append('(');
foreach (var i in this.Coordinate)
{
sb.Append(i);
sb.Append(',');
}
sb.Length = sb.Length - 1;
sb.Append(')');
return sb.ToString();
}
}
Then we create a sparse matrix using a dictionary as the storage. Note this is incomplete obviously as there's no way to clear it partially or completely but again it's just an example.
class SparseMatrix : IEnumerable<KeyValuePair<SparseCoord, int>>
{
Dictionary<SparseCoord, int> cells = new Dictionary<SparseCoord, int>();
public int this[SparseCoord coord]
{
get
{
if (this.cells.TryGetValue(coord, out var ret))
return ret;
return 0;
}
set
{
this.cells[coord] = value;
}
}
public IEnumerator<KeyValuePair<SparseCoord, int>> GetEnumerator()
{
return this.cells.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
Then you can add values and iterate over the contents:
static void Main(string[] _)
{
SparseMatrix matrix = new SparseMatrix();
matrix[SparseCoord.ToCoordinate(1, 2, 3)] = 1;
matrix[SparseCoord.ToCoordinate(5, 6, 7)] = 2;
foreach (var value in matrix)
Console.WriteLine(value);
}
Finally I would reiterate that if your matrix really is going to get "big" as you said in a comment then you should invest some time in looking at some well known implementations of sparse matrix.
I would like to send to a method a list of numbers, and to get in return all the possible combinations of numbers I gen generate from this list by putting the digits next to each other.
For example, for the numbers {1, 2, 3} I would to give in return:
{1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321}
This code for example (which I haven't finished) only "Knows" to deal with lists that contain 3 numbers in them.
private static void M1()
{
var intList = new List<int>() { 1, 2, 3 };
var resultList = AllPossibleCombinations(intList);
}
private static object AllPossibleCombinations(List<int> List)
{
var result = new List<int>();
result.Add(List[0]);
result.Add(List[1]);
result.Add(List[2]);
result.Add(List[0] * 10 + List[1]);
result.Add(List[1] * 10 + List[2]);
result.Add(List[0] * 10 + List[2]);
result.Add(List[1] * 10 + List[0]);
result.Add(List[2] * 10 + List[1]);
result.Add(List[2] * 10 + List[0]);
return result;
}
How can I write something more generic? How can I get a list with different number of elements and give in return all the possible combinations?
This isn't necessarily the most efficient, but here's how you could do this using non-recursive methods returning IEnumerable<T>. Something like this will probably require the least possible memory, since it doesn't require building all the permutations in memory. Instead, it just allows you to iterate over the permutations one by one.
private static void Main()
{
var input1 = new[] {"1", "2", "3"};
foreach (var output in EnumeratePermutations(input1))
Debug.WriteLine(String.Join(",", output));
}
private static IEnumerable<T[]> EnumeratePermutations<T>(T[] input)
{
return from partCount in Enumerable.Range(1, input.Length)
let inputs = Enumerable.Repeat(input, partCount).ToArray()
from indices in EnumerateCrossjoinIndices(inputs)
where indices.Distinct().Count() == indices.Length
select indices.Select(n => input[n]).ToArray();
}
private static IEnumerable<int[]> EnumerateCrossjoinIndices(params Array[] arrays)
{
var arrayCount = arrays.Length;
if (arrayCount == 0)
yield break;
if (arrays.Any(a => a.Length == 0))
yield break;
var indexer = new int[arrayCount];
yield return (int[]) indexer.Clone();
for (var dimension = arrayCount - 1; dimension >= 0; --dimension)
{
++indexer[dimension];
if (indexer[dimension] == arrays[dimension].Length)
indexer[dimension] = 0;
else
{
yield return (int[]) indexer.Clone();
dimension = arrayCount;
}
}
}
EnumerateCrossjoinIndices takes n arrays of potentially different lengths and yields an enumeration of the indices that would be used in a crossjoin operation. For example, given {"1","2"} and {"A","B","C"}, it would yield 6 arrays {0,0}, {0,1}, {0,2}, {1,0}, {1,1}, {1,2}. Note that the arrays yielded by this method contain indices into the input arrays, not the elements at those indices.
EnumeratePermutations takes an array and yields the permutations of the items of that array. It does this by enumerating over the indices that would be used to crossjoin the array against itself x times (where x = 1 to n, where n is the number of items in the array). It then filters out any set of crossjoin indices where the set isn't a distinct set.
Try this sample code:
private static List<int> AllPossibleCombinations(IList<int> alphabet)
{
List<int[]> combinations = new List<int[]>();
MakeCombination(combinations, alphabet.Count, new int[0]); // Start recursion
combinations.RemoveAt(0); // Remove empty combination
return combinations.ConvertAll(c => c.Aggregate(0, (sum, index) => sum * 10 + alphabet[index]));
}
private static void MakeCombination(List<int[]> output, int length, int[] usedIndices)
{
output.Add(usedIndices);
for (int i = 0; i < length; i++)
if (Array.IndexOf(usedIndices, i) == -1) // If the index wasn't used earlier
{
// Add element to the array of indices by creating a new one:
int[] indices = new int[usedIndices.Length + 1];
usedIndices.CopyTo(indices, 0);
indices[usedIndices.Length] = i;
if (length + 1 != indices.Length)
MakeCombination(output, length, indices); // Recursion
}
}
It uses recursion to generate desired combinations.
Usage:
var t = AllPossibleCombinations(new[] { 1, 2, 3 });
Solution 1
The more generic and type independent way is to create tree-based algorithm which returns collection of combinations of input objects.
Code:
public static class IEnumerableExtensions
{
private class Node<T>
{
public Node()
{
Children = new List<Node<T>>();
}
public T Value
{
get;
set;
}
public bool IsRoot
{
get;
set;
}
public List<Node<T>> Children
{
get;
private set;
}
public Node<T> Parent
{
get;
set;
}
public List<Node<T>> Path
{
get
{
List<Node<T>> Result = new List<Node<T>>();
Result.Add(this);
if (this.Parent.IsRoot == false)
{
Result.AddRange(this.Parent.Path);
}
return Result;
}
}
}
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> enumerable)
{
List<Node<T>> AllNodes = new List<Node<T>>();
// Create tree.
Node<T> Root = new Node<T>() { IsRoot = true };
Queue<Node<T>> Queue = new Queue<Node<T>>();
Queue.Enqueue(Root);
int CurrentLevel = 0;
int LevelsToCreate = enumerable.Count();
while (Queue.Count > 0)
{
var CurrentLevelNodes = Queue.ToList();
Queue.Clear();
foreach (var LoopNode in CurrentLevelNodes)
{
if (LoopNode.Children.Count == 0)
{
foreach (var LoopValue in enumerable)
{
var NewNode = new Node<T>() { Value = LoopValue, Parent = LoopNode };
AllNodes.Add(NewNode);
LoopNode.Children.Add(NewNode);
Queue.Enqueue(NewNode);
}
}
}
CurrentLevel++;
if (CurrentLevel >= LevelsToCreate)
{
break;
}
}
// Return list of all paths (which are combinations).
List<List<T>> Result = new List<List<T>>();
foreach (var LoopNode in AllNodes)
{
if (!LoopNode.IsRoot)
{
List<T> Combination = LoopNode.Path.Select(Item => Item.Value).ToList();
Result.Add(Combination);
}
}
return Result;
}
}
Example with numbers:
class Program
{
static void Main(string[] args)
{
List<int> Input = new List<int>() { 1, 2, 3 };
var Combinations = Input.Combinations();
}
}
Example with strings:
static void Main(string[] args)
{
var Input = new List<string>() { "a", "b" };
var Combinations = Input.Combinations();
foreach (var LoopCombination in Combinations)
{
string Combination = string.Join(String.Empty, LoopCombination);
Console.WriteLine(Combination);
}
Console.ReadKey();
}
Solution 2
The second idea is to not to use tree-based algorithm and create combinations step-by-step - it may be faster.
Code:
public class Combinator<T>
{
private readonly Dictionary<int, T> _Pattern;
private readonly int _Min = 0;
private readonly int _Max;
private List<int> _CurrentCombination;
public Combinator(IEnumerable<T> pattern)
{
_Pattern = new Dictionary<int, T>();
for (int i = 0; i < pattern.Count(); i++)
{
_Pattern.Add(i, pattern.ElementAt(i));
}
_CurrentCombination = new List<int>();
_Max = pattern.Count() - 1;
}
public bool HasFinised
{
get;
private set;
}
public IEnumerable<T> Next()
{
// Initialize or increase.
if (_CurrentCombination.Count == 0)
{
_CurrentCombination.Add(_Min);
}
else
{
MyIncrease(_CurrentCombination.Count - 1);
}
if (_CurrentCombination.Count - 1 == _Max && _CurrentCombination.All(Key => Key == _Max))
{
HasFinised = true;
}
return _CurrentCombination.Select(Key => _Pattern[Key]).ToList();;
}
private void MyIncrease(int index)
{
if (index >= 0)
{
_CurrentCombination[index] = _CurrentCombination[index] + 1;
if (_CurrentCombination[index] > _Max)
{
_CurrentCombination[index] = _Min;
if (index - 1 < 0)
{
_CurrentCombination.Insert(0, -1);
index++;
}
MyIncrease(index - 1);
}
}
}
}
Example:
class Program
{
static void Main(string[] args)
{
var Pattern = new List<string>() { "a", "b", "c" };
Combinator<string> Combinator = new Combinator<string>(Pattern);
while (Combinator.HasFinised == false)
{
var Combined = Combinator.Next();
var Joined = string.Join("-", Combined);
Console.WriteLine(Joined);
}
Console.ReadKey();
}
}
If you want to combine items with others only:
static void Main(string[] args)
{
Combinator<int> Combinator = new Combinator<int>(new List<int>() { 1, 2, 3 });
while (Combinator.HasFinised == false)
{
var NextCombination = Combinator.Next();
var DistinctCheck = NextCombination.ToList().Distinct();
if (DistinctCheck.Count() == NextCombination.Count())
{
Console.WriteLine(string.Join(String.Empty, NextCombination.Select(Item => Item.ToString())));
}
}
Console.ReadKey();
}
I'm porting a game written in Pascal (compiled in 16 bit) to C# (so it will run on machines newer than XP). From what I've gathered, in Pascal, it's possible to type define in the type section of a unit/program through syntax like this:
type
BaseArrayPtr = ^BaseArray;
BaseArray = array [1 .. 5, 1 .. 5] of Integer;
SubArray = array [0 .. 3] of BaseArray;
I also gathered that, unfortunately, it is impossible to type define in C#. However, I'm trying for a workaround. So far, this is what I have:
BoundedArray.cs:
using System;
using System.Collections;
namespace test
{
abstract class BoundedArray<T>
{
public BoundedArray()
{
m_data = null;
}
public T this[params int[] index]
{
get
{
if (index.Length != m_data.Rank)
throw new IndexOutOfRangeException();
return (T) m_data.GetValue(index);
}
set
{
if (index.Length != m_data.Rank)
throw new IndexOutOfRangeException();
m_data.SetValue(value, index);
}
}
protected void SetAttributes(int[] lowerBounds, int[] lengths)
{
if (lengths.Length != lowerBounds.Length)
throw new ArgumentException();
m_lowerBounds = lowerBounds;
m_lengths = lengths;
m_data = Array.CreateInstance(typeof(T), m_lengths, m_lowerBounds);
m_data.Initialize(); // Should (but doesn't) initialize every element in m_data
}
Array m_data;
int[] m_lengths;
int[] m_lowerBounds;
}
}
test.cs:
using System;
namespace test
{
class Program
{
public static int[] ints(params int[] values)
{
return values;
}
class BaseArray : BoundedArray<int>
{
public BaseArray()
{
SetAttributes(ints(2, 2), ints(1, 2));
}
}
class SubArray : BoundedArray<BaseArray>
{
public SubArray()
{
SetAttributes(ints(4), ints(2));
}
}
static void Main(string[] args)
{
SubArray subArray = new SubArray();
Console.Read();
}
}
}
I've checked baseArray, and the default values of m_data are zeroes, since they are ints. However, in subArray, the default values of m_data are null - the BaseArray instances inside the array in subArray haven't been initialized for some reason. How do I get the default constructor to run?
EDIT: The real question at the moment is why doesn't m_data.Initialize(); in the SetAttributes method initialize all elements in m_data? The documentation on MSDN seems to indicate that it should...
EDIT:
So I believe that problem is that System.Array.Initialize only works on value-types. Since classes are references types in C#, System.Array.Initialize doesn't do anything. So I have to find a way to initialize a reference-type array of variable dimensions, lengths, and lower bounds.
Well I have done some changes that when you want to create an instance of a SubArray you should pass BaseArray as source of data to be initialize.
As i understood you want to set the values from BaseArray to SubArray.
Here is my work:
BoundedArray.cs
abstract class BoundedArray<T>
{
public BoundedArray()
{
m_data = null;
}
public int[] Lengths;
public int[] LowerBounds;
public void CreateInstance()
{
if (Lengths.Length != LowerBounds.Length)
throw new Exception("Incorrect number of lengths or lower bounds.");
m_data = Array.CreateInstance(typeof(T), Lengths, LowerBounds);
}
public void CreateInstance(Array source)
{
if (Lengths.Length != LowerBounds.Length)
throw new Exception("Incorrect number of lengths or lower bounds.");
m_data = Array.CreateInstance(typeof(T), Lengths, LowerBounds);
/************************************************************************/
/* Now you should find the value of BaseArray and set it to m_data */
/************************************************************************/
}
public T this[params int[] index]
{
get
{
if (index.Length != m_data.Rank)
throw new IndexOutOfRangeException();
return (T)m_data.GetValue(index);
}
set
{
if (index.Length != m_data.Rank)
throw new IndexOutOfRangeException();
m_data.SetValue(value, index);
}
}
public Array GetData()
{
return m_data;
}
Array m_data;
}
Test.cs
class Program
{
public static int[] ints(params int[] values)
{
return values;
}
class BaseArray : BoundedArray<int>
{
public BaseArray()
{
Lengths = ints(1, 2);
LowerBounds = ints(2, 2);
CreateInstance();
}
}
class SubArray : BoundedArray<BaseArray>
{
public SubArray(BaseArray arr)
{
Lengths = ints(2);
LowerBounds = ints(4);
CreateInstance(arr.GetData());
}
}
static void Main(string[] args)
{
BaseArray baseArray = new BaseArray();
SubArray subArray = new SubArray(baseArray);
Console.Read();
}
}
You have a singe-dimensional array SubArray which holds BaseArray objects which are two-dimensional arrays of intergers. In place of Pascal type, you can define a custom C# class which would override the indexer operator to give you exactly the same behavior.
EDITED
So, in Pascal you have this:
type
BaseArrayPtr = ^BaseArray;
BaseArray = array [1 .. 5, 1 .. 5] of Integer;
SubArray = array [0 .. 3] of BaseArray;
Maybe I misunderstood the question, but is the below not exactly the same, in C#?
public class BaseArray
{
int[,] m_array = new int[5, 5];
static void CheckBounds(int x, int y)
{
if (x < 1 || x > 5 || y < 1 || y > 5)
throw new IndexOutOfRangeException();
}
public int this[int x, int y]
{
get
{
CheckBounds(x, y);
return m_array[x-1, y-1];
}
set
{
CheckBounds(x, y);
m_array[x-1, y-1] = value;
}
}
}
public class SubArray
{
BaseArray[] m_array = new BaseArray[4];
public BaseArray this[int x]
{
get { return m_array[x]; }
set { m_array[x] = value; }
}
}
I've already answered my own question once, but I came up with a much better implementation of my answer.
Here's what this solution consists of:
SetAttributes must be run once, in the default constructor of a class based off of BoundedArray
During SetAttributes, I gather a jagged, two-dimensional array of all of the indices in the current BoundedArray subclass
I create instances of the template type by calling Activator.CreateInstance and assigning one per index
Other things to note:
Set attributes now takes a variable length array of int[]s instead of two int[]s. Previously, it was taking the lowerbounds and the lengths, but I realized it makes more sense to just take int[]s which are lower and upper bounds, and then use a LINQ query to check that there aren't any which aren't pairs
I created a static class called IntArray, which is used extensively by SetAttributes and in test.cs
I tried to throw as many useful errors as possible, since I'll probably end up using this code a lot
I have a feeling that Combinations(int[][] list1, int[] list2) is probably where the most improvement on my solution could be found. I'm open to suggestions on how to improve all of my code
So, without further ado, my complete solution:
BoundedArray.cs
using System;
using System.Linq;
using System.Collections.Generic;
namespace test
{
static class IntArray
{
public static int[] FromValues(params int[] values)
{
return values;
}
public static int[] Sequence(int from, int length)
{
if (from < 0 || length < 1)
throw new ArgumentException();
return Enumerable.Range(from, length).ToArray();
}
public static int[][] Combinations(int[] list1, int[] list2)
{
return Combinations(list1.Select(i => new int[] { i }).ToArray(), list2);
}
public static int[][] Combinations(int[][] list1, int[] list2)
{
List<List<int>> result = new List<List<int>>();
for (int i = 0; i < list1.Length; i++)
{
for (int j = 0; j < list2.Length; j++)
result.Add(((int[]) list1.GetValue(i)).Concat(new int[] { list2[j] }).ToList());
}
return result.Select(i => i.ToArray()).ToArray();
}
}
abstract class BoundedArray<T>
{
public BoundedArray()
{
m_data = null;
}
public Array Value
{
get { return m_data; }
}
public T this[params int[] index]
{
get
{
if (index.Length != m_data.Rank)
throw new IndexOutOfRangeException();
return (T) m_data.GetValue(index);
}
set
{
if (index.Length != m_data.Rank)
throw new IndexOutOfRangeException();
m_data.SetValue(value, index);
}
}
protected void SetAttributes(params int[][] values)
{
// Make sure all of the values are pairs
if (values.Where(i => i.Length != 2).ToArray().Length > 0)
throw new ArgumentException("Input arrays must be of length 2.");
int[] lowerBounds = values.Select(i => i[0]).ToArray();
int[] lengths = values.Select(i => i[1] - i[0] + 1).ToArray();
m_data = Array.CreateInstance(typeof(T), lengths, lowerBounds);
int[][] indices = (lowerBounds.Length != 1) ?
IntArray.Combinations(IntArray.Sequence(lowerBounds[0], lengths[0]), IntArray.Sequence(lowerBounds[1], lengths[1]))
: IntArray.Sequence(lowerBounds[0], lengths[0]).Select(i => new int[] { i }).ToArray();
for (int i = 2; i < lowerBounds.Length; i++)
indices = IntArray.Combinations(indices, IntArray.Sequence(lowerBounds[i], lengths[i]));
for (int i = 0; i < indices.Length; i++)
m_data.SetValue(Activator.CreateInstance(typeof(T)), indices[i]);
}
Array m_data;
}
}
test.cs
using System;
namespace test
{
class Program
{
// *** Examples of what you can do with BoundedArray ***
// Multi-dimensional, bounded base array
class BaseArray : BoundedArray<int>
{
public BaseArray()
{
SetAttributes(IntArray.FromValues(2, 3), IntArray.FromValues(2, 4));
}
}
// One-dimensional, bounded subclass array
class SubArray : BoundedArray<BaseArray>
{
public SubArray()
{
SetAttributes(IntArray.FromValues(4, 6));
}
}
static void Main(string[] args)
{
// Initializations used for testing purposes
BaseArray baseArray = new BaseArray();
SubArray subArray = new SubArray();
// Example of assignment
baseArray[3, 4] = 3;
subArray[4][2, 3] = 4;
subArray[4][2] = 3; // Weakness: compiles, but causes IndexOutOfRangeException
Console.Read();
}
}
}
Thougts?
I'm trying to figure out how to get a single dimension from a multidimensional array (for the sake of argument, let's say it's 2D), I have a multidimensional array:
double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };
If it was a jagged array, I would simply call d[0] and that would give me an array of {1, 2, 3, 4, 5}, is there a way I can achieve the same with a 2D array?
No. You could of course write a wrapper class that represents a slice, and has an indexer internally - but nothing inbuilt. The other approach would be to write a method that makes a copy of a slice and hands back a vector - it depends whether you want a copy or not.
using System;
static class ArraySliceExt
{
public static ArraySlice2D<T> Slice<T>(this T[,] arr, int firstDimension)
{
return new ArraySlice2D<T>(arr, firstDimension);
}
}
class ArraySlice2D<T>
{
private readonly T[,] arr;
private readonly int firstDimension;
private readonly int length;
public int Length { get { return length; } }
public ArraySlice2D(T[,] arr, int firstDimension)
{
this.arr = arr;
this.firstDimension = firstDimension;
this.length = arr.GetUpperBound(1) + 1;
}
public T this[int index]
{
get { return arr[firstDimension, index]; }
set { arr[firstDimension, index] = value; }
}
}
public static class Program
{
static void Main()
{
double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };
var slice = d.Slice(0);
for (int i = 0; i < slice.Length; i++)
{
Console.WriteLine(slice[i]);
}
}
}
Improved version of that answer:
public static IEnumerable<T> SliceRow<T>(this T[,] array, int row)
{
for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
{
yield return array[row, i];
}
}
public static IEnumerable<T> SliceColumn<T>(this T[,] array, int column)
{
for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
{
yield return array[i, column];
}
}
Rectangular arrays are not built for this purpose. If you need that type of functionality, you should switch to a jagged array. It is pretty simple to write a function that will convert a rectangular array into a jagged one.
You could also simply rebuild that array by calling GetLength(int dimension) on the appropriate dimension, and then indexing it properly to retrieve each value. It would be cheaper than converting the entire array, but the cheapest option is to change it to use jagged arrays.
This should replicate the a[r] functionality of a jagged array:
T[] Slice<T>(T[,] a, int r) => Enumerable.Range(0, a.GetUpperBound(1) + 1).Select(i => a[r, i]).ToArray();
A SDK is returning me an array with multiple dimensions such as:
int[,,] theArray = new int[2,8,12];
I need to visit each element in the array and return the value and the position of the value. I need to do this without knowing the number of dimensions and elements of the array being passed in.
Use for loops:
for (int i=theArray.GetLowerBound(0);i<=theArray.GetUpperBound(0);++i)
{
for (int j=theArray.GetLowerBound(1);j<=theArray.GetUpperBound(1);++j)
{
for (int k=theArray.GetLowerBound(2);k<=theArray.GetUpperBound(2);++k)
{
// do work, using index theArray[i,j,k]
}
}
}
If you don't know the number of dimensions in advance, you can use Array.Rank to determine that.
Would something like this work for you? It recurses the ranks so you can use a foreach() and get an array containing the current item's indices.
class Program
{
static void Main(string[] args)
{
int[, ,] theArray = new int[2, 8, 12];
theArray[0, 0, 1] = 99;
theArray[0, 1, 0] = 199;
theArray[1, 0, 0] = 299;
Walker w = new Walker(theArray);
foreach (int i in w)
{
Console.WriteLine("Item[{0},{1},{2}] = {3}", w.Pos[0], w.Pos[1], w.Pos[2], i);
}
Console.ReadKey();
}
public class Walker : IEnumerable<int>
{
public Array Data { get; private set; }
public int[] Pos { get; private set; }
public Walker(Array array)
{
this.Data = array;
this.Pos = new int[array.Rank];
}
public IEnumerator<int> GetEnumerator()
{
return this.RecurseRank(0);
}
private IEnumerator<int> RecurseRank(int rank)
{
for (int i = this.Data.GetLowerBound(rank); i <= this.Data.GetUpperBound(rank); ++i)
{
this.Pos.SetValue(i, rank);
if (rank < this.Pos.Length - 1)
{
IEnumerator<int> e = this.RecurseRank(rank + 1);
while (e.MoveNext())
{
yield return e.Current;
}
}
else
{
yield return (int)this.Data.GetValue(this.Pos);
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.RecurseRank(0);
}
}
}
I'm not sure I understand your question about "return the position[n,n,n]", but if you're trying to return more than one value from a method, there are a couple of ways to do it.
• Useoutor reference parameters (e.g.,Int) that get set to the returned values before returning from the method.
• Pass in an array, e.g., an array of three ints, the elements of which get set by the method before it returns.
• Return an array of values, e.g., an array of three ints.