Local reference to inner array in swift - c#

I came from C# world, and used to arrays being reference types. As I understand, in swift arrays are value types, but they try to play as reference ones.
I don't actually know how to ask what I need (I think this is the case when I need to know answer to be able to ask question), but in C# I would say I need to store a reference to inner array of a jagged array into local variable.
Consider the following piece of code:
// a function to change row values in-place
func processRow(inout row : [Int], _ value : Int)
{
for col in 0..<row.count
{
row[col] = value;
}
}
// a function to change matrix values in-place
func processMatrix(inout matrix : [[Int]])
{
for rowIdx in 0..<matrix.count
{
// (1) Works with array in-place
processRow(&(matrix[rowIdx]), -1)
// (2) Creates local copy
var r = matrix[rowIdx]; // <--- What to write here to not make a copy but still have this local variable?
processRow(&r, -2);
}
}
var matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
processMatrix(&matrix)
print(matrix) // outputs [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
Swift sandbox here http://swiftlang.ng.bluemix.net/#/repl/8608824e18317e19b32113e1aa08deeb4ec5ab96ed8cdbe1dbfb4e753e87d528
Here I want to I process multidimensional array in-place, so that I don't create a copy of array or parts of array.
In option (1) I change everything to "-1", and it works, but is uses additional function for this.
In option (2) I try to use local variable to store matrix[rowIdx], but it actually creates a copy of inner array - not what I want; working with this variable changes copy of array, and not original one.
How can I achieve results like in option (1), but using local variable instead of function? That is, how can I obtain reference to inner array and put it to local variable?
I would understand answer "there's no way for this", but I want such answers to be accompanied by some Apple refs.

I don't believe that there is a way to copy an array into a local variable without making a copy. That is just the way that value types work in Swift. Says Apple:
The most basic distinguishing feature of a value type is that copying — the effect of assignment, initialization, and argument passing — creates an independent instance with its own unique copy of its data[.]
And here:
A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function...
[...]
All structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they are passed around in your code.
It's just part of the definition of value type - and every array is a value type.
The easiest way to get the result you are seeking is simply to reassign the value of r to the row that you want to change in matrix:
// (2) Creates local copy
var r = matrix[rowIdx]
processRow(&r, -2)
matrix[rowIdx] = r
The closest thing to what you are suggesting is to work with pointers: UnsafeMutablePointer, UnsafePointer, etc. But that is really fighting against the way that Swift was designed to be used. If you wanted to, however, it would look something like this:
func processRow(ptr: UnsafeMutablePointer<Int>, _ value : Int, count: Int) {
for col in 0..<count {
ptr.advancedBy(col).memory = value
}
}
func processMatrix(inout matrix : [[Int]]) {
for rowIdx in 0..<matrix.count {
let r = UnsafeMutablePointer<Int>(matrix[rowIdx])
processRow(r, -2, count: matrix[rowIdx].count)
}
}
var matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
processMatrix(&matrix)
print(matrix) // [[2, 2, 2], [2, 2, 2], [2, 2, 2]]

Swift arrays are pretty painful when it comes to work with mutable data. There are two ways to fix that:
1) Use NSMutableArray
import Foundation
func processRow(_ row : NSMutableArray, _ value : Int) {
for col in 0..<row.count {
row[col] = value;
}
}
func processMatrix(_ matrix : inout [NSMutableArray]) {
for rowIdx in 0..<matrix.count {
let r = matrix[rowIdx]
processRow(r, -2);
}
}
var matrix = [
NSMutableArray(array: [1, 2, 3]),
NSMutableArray(array: [4, 5, 6]),
NSMutableArray(array: [7, 8, 9])
]
processMatrix(&matrix)
print(matrix) // outputs , <__NSArrayM 0x6000027a1560>(-2,-2,-2)]
2) Use class wrapper
class Wrapper<T: CustomDebugStringConvertible>: CustomDebugStringConvertible {
var value: T
init(_ value: T) {
self.value = value
}
var debugDescription: String {
return value.debugDescription
}
}
func processRow(_ row : Wrapper<[Int]>, _ value : Int) {
for col in 0..<row.value.count {
row.value[col] = value;
}
}
func processMatrix(_ matrix : inout [Wrapper<[Int]>]) {
for rowIdx in 0..<matrix.count {
let r = matrix[rowIdx]
processRow(r, -2);
}
}
var matrix = [
Wrapper([1, 2, 3]),
Wrapper([4, 5, 6]),
Wrapper([7, 8, 9])
]
processMatrix(&matrix)
print(matrix) // outputs [[-2, -2, -2], [-2, -2, -2], [-2, -2, -2]]
However it doesn't look very nice.

Related

Why can't I use array initialisation syntax separate from array declaration?

I can do this with an integer:
int a;
a = 5;
But I can't do this with an integer array:
int[] a;
a = { 1, 2, 3, 4, 5 };
Why not?
To clarify, I am not looking for the correct syntax (I can look that up); I know that this works:
int[] a = { 1, 2, 3, 4, 5 };
Which would be the equivalent of:
int a = 5;
What I am trying to understand is, why does the code fail for arrays? What is the reason behind the code failing to be recognised as valid?
The reason there is a difference is that the folks at Microsoft decided to lighten the syntax when declaring and initializing the array in the same statement, but did not add the required syntax to allow you to assign a new array to it later.
This is why this works:
int[] a = { 1, 2, 3, 4, 5 };
but this does not:
int[] a;
a = { 1, 2, 3, 4, 5 };
Could they have added the syntax to allow this? Sure, but they didn't. Most likely they felt that this use-case is so seldom used that it didn't warrant prioritizing over other features. All new features start with minus 100 points and this probably just didn't rank high enough on the priority list.
Note that { 1, 2, 3, 4, 5 } by itself has no meaning; it can only appear in two places:
As part of an array variable declaration:
int[] a = { 1, 2, 3, 4, 5 };
As part of an array creation expression:
new int[] { 1, 2, 3, 4, 5 }
The number 5, on the other hand, has a meaning everywhere it appears in C#, which is why this works:
int a;
a = 5;
So this is just special syntax the designers of C# decided to support, nothing more.
This syntax is documented in the C# specification, section 12.6 Array Initializers.
The reason your array example doesn't work is because of the difference between value and reference types. An int is a value type. It is a single location in memory whose value can be changed.
Your integer array is a reference type. It is not equivalent to a constant number of bytes in memory. Therefore, it is a pointer to the bytes where that data is stored.
In this first line, you are assigning null to a.
int[] a;
In the next line, if you want to change the value of the array, you need to assign it to a new array.
a = new[] {1, 2, 3, 4, 5};
That is why you need the new[] before the list of values within the array if you strongly type your declaration.
int[] a = {1, 2, 3, 4, 5}; // This will work.
var a = {1, 2, 3, 4, 5}; // This will not.
However, as many of the other answers have said, if you declare it in a single line, then you do not need the new[]. If you separate the declaration and initialization, then you are required to use new[].
{} syntax is available for array initialization, not to be used after declaration.
To initialize an array you should try like this:
int[] a = { 1, 2, 3, 4, 5 };
Other ways to Initializing a Single-dimensional array:
int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] a = new int[5] { 1, 2, 3, 4, 5 };
Have a look at this: different ways to initialize different kinds of arrays

How to find intersection of two arrays (Optimal solution)

I am writting algorithm for intersection of two arrays A and B , I want an optimized solution in terms of space complexity and time complexity.
I have written algorithm and it works fine but i want to know if there is any more optimal solution then this exist or if someone could provide me.
What i do is:
(1) Find the Smallest size array among two.
(2) The new array wil be of size allocated size equal to smaller size array
(3) From smaller size array i go and compare with each element in bigger array if it exists one ,i get it in third array"C" and break it right there (because we need to find intersection, even if it repeats 100 times after
we don't care for us only one existence is enough to put in third array). At the same time we also have to check if the element in smaller array which to be compared with all elements in bigger array already exist in third array, Example A=[0,1,1], B[0,1,2,3].
Now we start with A's first element, it is present in array B we save it in C[0], then go to second , now C is [0,1], and in next step we again have 1 to compare, which we have already compared.So for this situation we have to do check if element to be compare already exist in array C then we eliminate check for it.
(4) We store the found element in C (third array) and print it.
My full working code for it is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] aar1 = { 0, 1, 1, 7, 2, 6, 3, 9, 11, 2, 2,3,3,3,3,3,1 };
int[] aar2 = { 0, 1, 2, 3, 4, 5, 6, 11, 11, 1, 1, 1, 1 };
int[] arr3 = findIntersection(aar1, aar2);
Console.WriteLine("the array is : " + arr3);
Console.ReadKey();
}
private static int[] findIntersection(int[] aar1, int[] aar2)
{
int[] arr3 = { 0 };
if (aar1.Count() < aar2.Count())
{
int counter = 0;
arr3 = new int[aar1.Count()];
foreach (int var1 in aar1)
{
if (!checkifInThirdArray(var1, arr3))
{
foreach (int var2 in aar2)
{
if (var1 == var2)
{
arr3[counter++] = var1;
break;
}
}
}
}
}
else
{
int counter = 0;
arr3 = new int[aar1.Count()];
foreach (int var2 in aar2)
{
if (!checkifInThirdArray(var2, arr3))
{
foreach (int var1 in aar1)
{
if (var2 == var1)
{
arr3[counter++] = var2;
break;
}
}
}
}
}
return arr3;
}
private static bool checkifInThirdArray(int var1, int[] arr3)
{
bool flag = false;
if (arr3 != null)
{
foreach (int arr in arr3)
{
if (arr == var1) { flag = true; break; }
}
}
return flag;
}
}
}
One space complexity issue i found is (the others i would really appreciate if you let me know with solution if you find any) :
(1) When i allocate the size to third array, i allocate the Min of the two arrays to be compared, In case if the intersection element are too less then we
have unnecessarily allocated the extra memory. How to solve this issue ?
Please note that i don't have to use any inbuilt function like intersection() or any other.
It sounds like your solution is an O(n2) one in that, for every single element in one array, you may need to process every single element in the other (in the case where the intersection is the null set). You should be aware that C# actually has facilities for finding the intersection of arrays but, should you wish to implement your own, read on.
You would probably be better of sorting both arrays (in-place if allowed otherwise to a separate collection) then doing a merge-check of the two to construct another. The sort could be O(n log n) and the merge check would be O(n).
If you're wondering what I mean by merge check, it's simply processing both (sorted) arrays side by side.
If the first element in both matches, you have an intersect point and you should store that value and advance both lists until the next value is different.
If they're different, there's no intersect point and you can advance the array with the lowest value until it changes.
By way of example, here's some code in Python (the ideal pseudo-code language) that implements such a solution. Array a contains all the multiples of three between 0 and 18 inclusive (in arbitrary order and including duplicates), while array b has all the even numbers in that range (again, with some duplicates and ordered "randomly").
a = [0,3,15,3,9,6,12,15,18,6]
b = [10,0,2,12,4,6,18,8,16,10,12,6,14,16]
# Copy and sort.
a2 = a; a2.sort()
b2 = b; b2.sort()
# Initial pointers and results for merge check.
ap = 0
bp = 0
c = []
# Continue until either array is exhausted.
while ap < len(a2) and bp < len(b2):
# Check for intersect or which list has lowest value.
if a2[ap] == b2[bp]:
# Intersect, save, advance both lists to next number.
val = a2[ap]
c.append(val)
while ap < len(a2) and a2[ap] == val:
ap += 1
while bp < len(b2) and b2[bp] == val:
bp += 1
elif a2[ap] < b2[bp]:
# A has smallest, advance it to next number.
val = a2[ap]
while ap < len(a2) and a2[ap] == val:
ap += 1
else:
# B has smallest, advance it to next number.
val = b2[bp]
while bp < len(b2) and b2[bp] == val:
bp += 1
print(c)
If you run that, you'll see the intersect list that's formed between the two arrays:
[0, 6, 12, 18]
Maybe I am not understanding you right but why don't you use the following;
int[] aar1 = { 0, 1, 1, 7, 2, 6, 3, 9, 11, 2, 2,3,3,3,3,3,1 };
int[] aar2 = { 0, 1, 2, 3, 4, 5, 6, 11, 11, 1, 1, 1, 1 };
aarResult = aar1.Intersect(aar2).ToArray();
This will result in an array with only the space needed and intersects the arrays. You can also initialize the aarResult as follows to get the minimum array size:
int[] aarResult = new int[Math.Min(aar1.Count(), aar2.Count())];
You can use LINQ Intersect method. It uses hashing and works for linear O(N+M) which is faster than your algorithm:
int[] aar1 = { 0, 1, 1, 7, 2, 6, 3, 9, 11, 2, 2, 3, 3, 3, 3, 3, 1 };
int[] aar2 = { 0, 1, 2, 3, 4, 5, 6, 11, 11, 1, 1, 1, 1 };
int[] result = aar1.Intersect(aar2).ToArray();
It will also solve your unnecessarily allocated items problem, because it will create an array of the exact answer size.

Why are arrays only allowed in certain places?

As I understand it, C# has a syntax for writing arrays as such: { 1, 2, 3 }. Why is this invalid:
x = { 1, 2, 3 }.GetLength(0);
while this is valid?
int[] numbers = { 1, 2, 3 };
x = numbers.GetLength(0);
Isn't the datatype of the expression { 1, 2, 3 } the same as numbers?
Arrays are allowed anywhere - but you can only use that particular syntax (which is called an array initializer for creating them as part of a variable declaration - or as part of a larger expression called an array creation expression.
You can still create them though:
x = new int[] { 1, 2, 3 }.GetLength(0);
So within that, new int[] { 1, 2, 3 } is the array creation expression, and the { 1, 2, 3 } part is the array initializer.
Array creation expressions are described in section 7.6.10.4 of the C# 5 spec, and array initializers are described in section 12.6.
The syntax you refer to is an object collection initializer. It is useful when initializing an instance of different types. It does not, in itself, create an instance of a given type.
For instance, you can use it to declare arrays:
int[] nums = new int[] { 1, 2, 3 };
Lists:
List<int> nums = new List<int> { 1, 2, 3 };
Dictionary:
Dictionary<string, int> pairs = { { "One", 1 }, { "Two", 2 }, { "Three", 3 } };
You can still inline things to achieve your initial intention with a little more code:
new[] { 1, 2, 3 }.GetLength(0);
x = new[] { 1, 2, 3 }.GetLength(0); will get you what you want since {1, 2, 3} isn't on its own an array, but rather an array initializer. And GetLength() works with the former but not the latter.

Binary Search on the first element in a multiple dimensional array

My goal is to perform a binary search for only the first element in a 2D array. I have been searching all day to find if it is possible using BinarySearch() in .NET but I can't find a thing.
To make this clearer. Imagine I had a 1D array, unsorted. If I sort the array, I lose the original index. I would like to create a second element of my array to hold the original index (this I can do) then sort by first element, then binary search over the first elements.
If anyone could push me in the right direction I'd be very grateful.
Thanks
Well, if I understand you correctly, you need something like this:
// initialize the array and the indexes array
var a2D = new int[2][];
a2D[0] = new[] { 3, 14, 15, 92, 65, 35 }; // <-- your array (fake data here)
a2D[1] = Enumerable.Range(0, a2D[0].Length).ToArray(); // create the indexes row
// sort the first row and the second one containing the indexes
Array.Sort(a2D[0], a2D[1]);
// now a2D array contains:
// row 0: 3, 14, 15, 35, 65, 92
// row 1: 0, 1, 2, 5, 4, 3
// and you can perform binary search on the first row:
int columnIndexOf35 = Array.BinarySearch(a2D[0], 35);
// columnIndexOf35 = 3
//
// a2D[0][columnIndexOf35] = 35 <- value
// a2D[1][columnIndexOf35] = 5 <- original index
As per MSDN, Array.BinarySearch method operates only with one-dimensional arrays, so it is impossible to use it directly in your case. Some of the options you have are:
Extract first column into a separate array and call Array.BinarySearch on it.
Define custom class Pair that implements interface IComparable and construct your array with the instances of this class.
Implement binary search on two dimensional array by yourself.
It looks like you want to have object that holds data and "original index" and than sort/search array of objects by data.
(This answer shows Andrei's option 2)
class IndexedData:IComparable
{
public MyType Data;
public int OriginalIndex;
public int CompareTo(object obj) {
// add correct checks for null,.. here
// and return correct comparison result.
// I.e. if MyType is IComparable - just delegate.
return Data.CompareTo(obj);
}
Check IComparable on MSDN for implementation/usage details.
Depending on what you're planning to do with the arrays afterwards, another solution might be to use LINQ.
var unsortedStartingArray = new[] {3, 6, 2, 1, 20, 20};
var q = unsortedStartingArray
.Select((item, index) => new {item, index})
.ToLookup(x => x.item, x => x.index);
var notFound = q[30]; // An empty array. Nothing found
var indexOf1 = q[1].First(); // returns 3
var multipleIndexsOf20 = q[20]; // Returns an array with 4, 5
The index into the lookup would then be the value you're searching for. Performance wise I would guesstimate this to be faster aswell about 5 times slower from my crude testing.

In c# does Array.ToArray() perform a DEEP copy?

This should be a pretty basic question, but I've been having a little trouble finding a definite answer.
When you have an array of values and you use the .ToArray() method does it create a deep or shallow copy of the array?
No.
You can easily verify this by writing a small program to test.
Not strictly speaking, ICollection.ToArray() creates a new T[] and assigns each element in the original collection to the new array using Array.CopyTo().
Note:
If T is a value type, values are assigned and not references. This will behave as one would expect of a "Deep" copy.
int[] foo = { 1, 2, 3, 4 };
int[] bar = foo.ToArray();
for (int i = 0; i < foo.Length; i++) foo[i] += 10;
Console.WriteLine(string.Join(',', foo)); // 11, 12, 13, 14
Console.WriteLine(string.Join(',', bar)); // 1, 2, 3, 4

Categories

Resources