This question already has answers here:
Changing the value of an element in a list of structs
(7 answers)
Closed 10 months ago.
In the following program, I am unable to modify individual list items:
public class Program
{
static void Main(string[] args)
{
List<Point2d> list = new List<Point2d>();
list.Add(new Point2d(0, 0));
list.Add(new Point2d(0, 1));
foreach (Point2d item in list)
{
item.Print();
}
Point2d p = list[0];
p.Set(-1, -1);
foreach (Point2d item in list)
{
item.Print();
}
Console.ReadKey();
}
}
Output:
(0,0) (0,1) (0,0) (0,1)
My expected output was:
(0,0) (0,1) (-1,-1) (0,1)
What am I doing incorrectly?
Relevant source code:
public struct Point2d : IEquatable<Point2d>
{
public double X { get; set; }
public double Y { get; set; }
#region constructor
public Point2d(double x, double y)
{
X = x;
Y = y;
}
#endregion
public void Print()
{
Console.Write("(");
Console.Write(X);
Console.Write(",");
Console.Write(Y);
Console.Write(") ");
}
public void Set(double x, double y)
{
X = x;
Y = y;
}
public double GetDistance(Point2d otherPoint)
{
return Math.Sqrt(GetSquaredDistance(otherPoint));
}
public double GetSquaredDistance(Point2d otherPoint)
{
return ((otherPoint.X - X) * (otherPoint.X - X))
+ ((otherPoint.Y - Y) * (otherPoint.Y - Y));
}
public Point2d GetTranslated(Point2d center)
{
return new Point2d(X + center.X, Y + center.Y);
}
#region override string ToString()
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("(" + X + " , " + Y + ")");
return sb.ToString();
}
#endregion
#region equality comparison implementations
public override bool Equals(object other)
{
if (!(other is Point2d)) return false;
return Equals((Point2d)other);
}
public bool Equals(Point2d other)
{
return X == other.X && Y == other.Y;
}
public override int GetHashCode()
{
return (int)Math.Round(Y * 31.0 + X, 0); // 31 = some prime number
}
public static bool operator ==(Point2d a1, Point2d a2)
{
return a1.Equals(a2);
}
public static bool operator !=(Point2d a1, Point2d a2)
{
return !a1.Equals(a2);
}
#endregion
}
Point2d is a struct so when you did Point2d p = list[0]; you made a totally separate copy of the object. Your set only changed the copy not the original, you either need to make Point2d a class or add a list[0] = p; after the set.
Bugs like this is why it is recommended to make structs immutable and have no Set methods.
Related
I have a simple class point
public class point
{
private double X;
private double Y;
public double x
{
get { return X; }
set { X = value; }
}
public double y
{
get { return Y; }
set { Y = value; }
}
public point() { }
public point(double _x , double _y)
{
x = _x;
y = _y;
}
}
and I'm trying to use this loop to get unique values
for (int i = 0; i < dim.Count; i++)
{
if (dimO[i].x == dim[i].x && dimO[i].y == dim[i].y)
{
continue;
}
else
{
dimO.Add(dim[i]);
}
}
but I got an " out of index" exception .. what's wrong here?
You should override Equals and GetHashCode if you want to compare a point by its x and y.
public override bool Equals(object obj)
{
var point2 = obj as Point;
return point2 != null && point2.x == x && point2.y == y;
}
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
return hash;
}
}
I take the hash code function here.
Now you could have a list of unique points by using
var dim0 = (new HashSet(dim)).ToList();
// linq
var dim0 = dim.Distinct().ToList();
Or if you want to use for loop
var dim0 = new List<Point>();
foreach(var p in dim){
if(!dim0.Contains(p)){
dim0.Add(p);
}
}
Your solution is not working because dim0 does not initially have any point.
I want to implement my own Priority Queue in C# using the naive approach. I need to use PQ for edges in a graph to implement Prims algorithm. Here is what I've done so far:
public class Edge
{
private int v; // one vertex
private int w; // the other vertex
private int weight; // edge weight
public Edge(int v0, int w0, int weight0)
{
v = v0;
w = w0;
weight = weight0;
}
public Edge()
{
v = 0;
w = 0;
weight = 0;
}
public int get_weight()
{
return weight;
}
public int either()
{
return v;
}
public int the_other(int vertex)
{
if (vertex == v) return w;
else return v;
}
public int compareTo(Edge that)
{
if (this.get_weight() < that.get_weight()) return -1;
if (this.get_weight() < that.get_weight()) return 1;
else return 0;
}
public string to_str()
{
string s = v.ToString() + " " + weight.ToString() + " " + w.ToString() + '\n';
return s;
}
}
public class MyPriorityQueue_Edge
{
private List<Edge> q = new List<Edge>();
int size;
public MyPriorityQueue_Edge()
{
size = 0;
}
public void insert(Edge e)
{
q.Add(e);
size++;
}
Edge Min()
{
int min_weight = 1000;
Edge min_edge = new Edge();
foreach(Edge e in q)
if(e.get_weight()< min_weight)
min_edge = e;
return min_edge;
}
public Edge delmin()
{
Edge e = q.Min();
q.Remove(e);
size--;
return e;
}
public bool is_empty()
{
if (size == 0) return true;
else return false;
}
}
When I compile it the compiler points to this line:
Edge e = q.Min();
And says that I did not work on the exception "System.Argument.Exception". How can I fix this?
From MSDN Enumerable.Min<TSource> Method (IEnumerable<TSource>)
If type TSource implements IComparable<T>, this method uses that implementation to compare values. Otherwise, if type TSource implements IComparable, that implementation is used to compare values.
You need to implement IComparable interface in your Edge class.
public class Edge : IComparable
Then add a CompareTo() method. Something like
public int CompareTo(object obj)
{
if (obj is Edge)
{
Edge other = (Edge)obj;
return this.compareTo(other); // you already implemented this in your Edge class
}
else
{
throw new InvalidOperationException();
}
}
http://codepaste.net/i87t39
The error I get is "One of the parameters of a binary operator must be the containing type"
public class Vector3D<T>
{
public T x;
public T y;
public T z;
public Vector3D()
{
}
public Vector3D(T a, T b, T c)
{
x = a; y = b; z = c;
}
/*public Vector3D(double a, double b, double c)
{
x = a; y = b; z = c;
}*/
public override string ToString()
{
//return base.ToString();
return String.Format("({0} {1} {2})", x , y , z);
}
public Vector3D<double> operator+( Vector3D<double> right)
{
Vector3D<double> vd = new Vector3D<double>() { x = 0, y = 0, z = 0};
vd.x = left.x + right.x;
vd.y = left.y + right.y;
vd.z = left.z + right.z;
return vd;
}
}
If I copy the code in your link:
public class Vector3D<T>
{
public T x;
public T y;
public T z;
public Vector3D()
{
}
public Vector3D(T a, T b, T c)
{
x = a; y = b; z = c;
}
public override string ToString()
{
//return base.ToString();
return String.Format("({0} {1} {2})", x , y , z);
}
public Vector3D<double> operator+( Vector3D<double> right)
{
Vector3D<double> vd = new Vector3D<double>() { x = 0, y = 0, z = 0};
vd.x = left.x + right.x;
vd.y = left.y + right.y;
vd.z = left.z + right.z;
return vd;
}
}
The error which you have is in operator+, because the containing type is Exp<T>, not Exp<double>. You should change it. Also there is no definition of left in this method !
Your method should like something like that:
public static Vector3D<T> operator +(Vector3D<T> right)
{
Vector3D<T> vd = new Vector3D<T>();
vd.x = right.x;
vd.y = right.y;
vd.z = right.z;
return vd;
}
Without a lot of extra work, you're not going to be able to implement a fully general-purpose generic Vector3D<T> class complete with mathematical operators. There are no constraints that you can provide for the generic type that will at once allow T to be a built-in numeric type and at the same time provide math operators for T.
There are a variety of ways to deal with this, such as special-casing the type in the generic class (yuck!), requiring T to implement an interface (i.e. wrap a normal numeric type in a type that implements the interface), or making the type abstract and requiring specialized subclasses to implement the operators as named methods (one of which could even depend on an interface, while others based directly on numeric types would just implement them directly).
For example:
abstract class Vector3D<T>
{
public readonly T x;
public readonly T y;
public readonly T z;
public Vector3D() { }
public Vector3D(T x, T y, T z)
{
this.x = x;
this.y = y;
this.z = z;
}
public abstract Vector3D<T> Add(Vector3D<T> right);
}
class Vector3DDouble : Vector3D<double>
{
public Vector3DDouble() { }
public Vector3DDouble(double x, double y, double z)
: base(x, y, z)
{ }
public override Vector3D<double> Add(Vector3D<double> right)
{
return new Vector3DDouble(x + right.x, y + right.y, z + right.z);
}
}
Assuming for the moment you've somehow addressed that issue, let's look at your operator + overload. First, your code won't even compile. You have overloaded the unary + operator, because you only have one parameter right for the overload, but in the method body you assume a second parameter left which is undeclared.
A more sensible implementation might look like this:
public static Vector3D<T> operator+(Vector3D<T> left, Vector3D<T> right)
{
return left.Add(right);
}
Note: I'm assuming here you've solved the arithmetic issue by requiring implementers to provide the arithmetic operations via named method, e.g. Add() per my example above. Obviously the exact implementation here would depend on how you dealt with the general issue of doing math with T values.
I have a huge array that contains reference type elements, and I want to create a lot of other arrays that essentially just point to specific parts of that one big array.
In other words, I want to create "indexers" or "pointers with lengths".
In C++ it's easy to do so using pointers and for each pointer assign a length, for example create a struct which contains a pointer with a length.
How can I achieve this in C#/.NET?
The whole point is to avoid copying anything, I just want pointers to specific parts in an array that already exists in memory.
Any ideas?
Jon's suggestion of using ArraySegment<T> is likely what you want. If however you are wanting to represent a pointer to the interior of an array, the way you can in C++, here's some code for that. No warranty is expressed or implied, use at your own risk.
This code does not track the "length" of the interior pointer in any way, but it is quite easy to add that feature if you want.
internal struct ArrayPtr<T>
{
public static ArrayPtr<T> Null { get { return default(ArrayPtr<T>); } }
private readonly T[] source;
private readonly int index;
private ArrayPtr(ArrayPtr<T> old, int delta)
{
this.source = old.source;
this.index = old.index + delta;
Debug.Assert(index >= 0);
Debug.Assert(index == 0 || this.source != null && index < this.source.Length);
}
public ArrayPtr(T[] source)
{
this.source = source;
index = 0;
}
public bool IsNull()
{
return this.source == null;
}
public static bool operator <(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index < b.index;
}
public static bool operator >(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index > b.index;
}
public static bool operator <=(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index <= b.index;
}
public static bool operator >=(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index >= b.index;
}
public static int operator -(ArrayPtr<T> a, ArrayPtr<T> b)
{
Debug.Assert(Object.ReferenceEquals(a.source, b.source));
return a.index - b.index;
}
public static ArrayPtr<T> operator +(ArrayPtr<T> a, int count)
{
return new ArrayPtr<T>(a, +count);
}
public static ArrayPtr<T> operator -(ArrayPtr<T> a, int count)
{
return new ArrayPtr<T>(a, -count);
}
public static ArrayPtr<T> operator ++(ArrayPtr<T> a)
{
return a + 1;
}
public static ArrayPtr<T> operator --(ArrayPtr<T> a)
{
return a - 1;
}
public static implicit operator ArrayPtr<T>(T[] x)
{
return new ArrayPtr<T>(x);
}
public static bool operator ==(ArrayPtr<T> x, ArrayPtr<T> y)
{
return x.source == y.source && x.index == y.index;
}
public static bool operator !=(ArrayPtr<T> x, ArrayPtr<T> y)
{
return !(x == y);
}
public override bool Equals(object x)
{
if (x == null) return this.source == null;
var ptr = x as ArrayPtr<T>?;
if (!ptr.HasValue) return false;
return this == ptr.Value;
}
public override int GetHashCode()
{
unchecked
{
int hash = this.source == null ? 0 : this.source.GetHashCode();
return hash + this.index;
}
}
public T this[int index]
{
get { return source[index + this.index]; }
set { source[index + this.index] = value; }
}
}
Now we can do stuff like:
double[] arr = new double[10];
var p0 = (ArrayPtr<double>)arr;
var p5 = p0 + 5;
p5[0] = 123.4; // sets arr[5] to 123.4
var p7 = p0 + 7;
int diff = p7 - p5; // 2
It sounds like you're looking for something like ArraySegment<T>. Contrary to my earlier thoughts, it does have an indexer and implement IEnumerable<T> etc - it's just done with explicit interfaces.
Sample code:
using System;
using System.Collections.Generic;
static class Test
{
static void Main()
{
string[] original = { "The", "quick", "brown", "fox", "jumped", "over",
"the", "lazy", "dog" };
IList<string> segment = new ArraySegment<string>(original, 3, 4);
Console.WriteLine(segment[2]); // over
foreach (var word in segment)
{
Console.WriteLine(word); // fox jumped over the
}
}
}
EDIT: As noted in comments, ArraySegment<T> is only really "fully functional" in .NET 4.5. The .NET 4 version doesn't implement any interfaces.
You could use LINQ:
yourArray.Skip(startIndex).Take(numberToTake)
The query is lazily evaluated.
I have an abstract class, Vector, which I would like to overload the operators +,-,*, etc.
I want any derived classes to be able to use these, and get an object back with the same type as the calling object.
I tried with generics, (as follows, in brief), but I couldn't find a legal way to do it:
public static T operator +<T>( T V1, T V2) where T : Vector
{
//some calculation
return new T(args);
}
I then tried to do it just using the base class:
public static Vector operator+(Vector V1, Vector V2)
{
if (V1.Dimension != V2.Dimension)
throw new VectorTypeException("Vector Dimensions Must Be Equal");
double[] ArgList = new double[V1.Dimension];
for (int i = 0; i < V1.Dimension; i++) { ArgList[i] = V1[i] + V2[i]; }
return (Vector)Activator.CreateInstance(V1.GetType(), new object[] { ArgList});
}
If this method is passed in two child objects, it should perform the operation on them, and return a new object of the same heritage.
The problem I ran into with this is that I cannot enforce that all such child classes must have a constructor with the appropriate signature, and I can't call the base constructor to make the object.
What are ways to either (a) Make either of these work, or (b) do this elegantly in another way?
You could declare instance-level abstract methods which your subclass can override:
public abstract class Vector
{
protected abstract Vector Add(Vector otherVector);
public static Vector operator +(Vector v1, Vector v2)
{
return v1.Add(v2);
}
}
public class SubVector : Vector
{
protected override Vector Add(Vector otherVector)
{
//do some SubVector addition
}
}
Might run into some issues especially with multiple subclasses (Will SubVector have to know how to add with SomeOtherSubVectorClass? What if you add ThirdVectorType class?) and perhaps handling null cases. Also, making sure that SubVector.Add behaves the same as SomeOtherSubVectorClass.Add when it comes to commutative operations.
EDIT: based on your other comments, you could so something like:
public class Vector2D : Vector
{
public double X { get; set; }
public double Y { get; set; }
protected override Vector Add(Vector otherVector)
{
Vector2D otherVector2D = otherVector as Vector2D;
if (otherVector2D != null)
return new Vector2D() { X = this.X + otherVector2D.X, Y = this.Y + otherVector2D.Y };
Vector3D otherVector3D = otherVector as Vector3D;
if (otherVector3D != null)
return new Vector3D() { X = this.X + otherVector3D.X, Y = this.Y + otherVector3D.Y, Z = otherVector3D.Z };
//handle other cases
}
}
public class Vector3D : Vector
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
protected override Vector Add(Vector otherVector)
{
Vector2D otherVector2D = otherVector as Vector2D;
if (otherVector2D != null)
return new Vector3D() { X = this.X + otherVector2D.X, Y = this.Y + otherVector2D.Y, Z = this.Z };
Vector3D otherVector3D = otherVector as Vector3D;
if (otherVector3D != null)
return new Vector3D() { X = this.X + otherVector3D.X, Y = this.Y + otherVector3D.Y, Z = this.Z + otherVector3D.Z };
//handle other cases
}
}
EDITx2:
Given your latest comment, perhaps your should just maintain an internal array/matrix and just do generic matrix math. Your subclasses can expose X/Y/Z property wrappers against the array indicies:
public class Vector
{
protected double[] Values;
public int Length { get { return Values.Length; } }
public static Vector operator +(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
{
throw new VectorTypeException("Vector Dimensions Must Be Equal");
}
else
{
//perform generic matrix addition/operation
double[] newValues = new double[v1.Length];
for (int i = 0; i < v1.Length; i++)
{
newValues[i] = v1.Values[i] + v2.Values[i];
}
//or use some factory/service to give you a Vector2D, Vector3D, or VectorND
return new Vector() { Values = newValues };
}
}
}
public class Vector2D : Vector
{
public double X
{
get { return Values[0]; }
set { Values[0] = value; }
}
public double Y
{
get { return Values[1]; }
set { Values[1] = value; }
}
}
public class Vector3D : Vector
{
public double X
{
get { return Values[0]; }
set { Values[0] = value; }
}
public double Y
{
get { return Values[1]; }
set { Values[1] = value; }
}
public double Z
{
get { return Values[2]; }
set { Values[2] = value; }
}
}
EDITx3: Based on your latest comment, I guess you could implement operator overloads on each subclass, do the shared logic in a static method (say in the base Vector class), and somewhere do a switch/case check to provide a specific subclass:
private static Vector Add(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
{
throw new VectorTypeException("Vector Dimensions Must Be Equal");
}
else
{
//perform generic matrix addition/operation
double[] newValues = new double[v1.Length];
for (int i = 0; i < v1.Length; i++)
{
newValues[i] = v1.Values[i] + v2.Values[i];
}
//or use some factory/service to give you a Vector2D, Vector3D, or VectorND
switch (newValues.Length)
{
case 1 :
return new Vector1D() { Values = newValues };
case 2 :
return new Vector2D() { Values = newValues };
case 3 :
return new Vector3D() { Values = newValues };
case 4 :
return new Vector4D() { Values = newValues };
//... and so on
default :
throw new DimensionOutOfRangeException("Do not support vectors greater than 10 dimensions");
//or you could just return the generic Vector which doesn't expose X,Y,Z values?
}
}
}
Then your subclasses would have:
public class Vector2D
{
public static Vector2D operator +(Vector2D v1, Vector2D v2)
{
return (Vector2D)Add(v1, v2);
}
}
public class Vector3D
{
public static Vector3D operator +(Vector3D v1, Vector3D v2)
{
return (Vector3D)Add(v1, v2);
}
}
Some duplication, but I don't see a way around it off the top of my head to allow the compiler to do this:
Vector3 v1 = new Vector3(2, 2, 2);
Vector3 v2 = new Vector3(1, 1, 1);
var v3 = v1 + v2; //Vector3(3, 3, 3);
Console.WriteLine(v3.X + ", " + v3.Y + ", " + v3.Z);
or for other dimensions:
Vector2 v1 = new Vector2(2, 2);
Vector2 v2 = new Vector2(1, 1);
var v3 = v1 + v2; //Vector2(3, 3, 3);
Console.WriteLine(v3.X + ", " + v3.Y); // no "Z" property to output!
What about having an abstract method called Add() that operator+ just acts as a wrapper for? ie, "return v1.Add(v2)". This would also enable you to define interfaces which non-Vector classes can constrain their code to, enabling to perform math-like operations (since generic code can't see/touch operators like +, -, etc for any type).
The only constructor you can code with in a generic method is the default (ie, parameter-less) constructor, which you have to specify in the generic constraints for the method/type.
Five years later I had the exact same problem, only I was calling them Ntuples, not vectors. Here is what I did:
using System;
using System.Collections.Generic;
public class Ntuple{
/*parent class
has an array of coordinates
coordinate-wise addition method
greater or less than in dictionary order
*/
public List<double> Coords = new List<double>();
public int Dimension;
public Ntuple(List<double> Input){
Coords=Input;
Dimension=Input.Count;
}//instance constructor
public Ntuple(){
}//empty constructor, because something with the + overload?
public static Ntuple operator +(Ntuple t1, Ntuple t2)
{
//if dimensions don't match, throw error
List<double> temp = new List<double>();
for (int i=0; i<t1.Dimension; i++){
temp.Add(t1.Coords[i]+t2.Coords[i]);
}
Ntuple sum = new Ntuple(temp);
return sum;
}//operator overload +
public static bool operator >(Ntuple one, Ntuple other){
//dictionary order
for (int i=0; i<one.Dimension; i++){
if (one.Coords[i]>other.Coords[i]) {return true;}
}
return false;
}
public static bool operator <(Ntuple one, Ntuple other){
//dictionary order
for (int i=0; i<one.Dimension; i++){
if (one.Coords[i]<other.Coords[i]) {return true;}
}
return false;
}
}//ntuple parent class
public class OrderedPair: Ntuple{
/*
has additional method PolarCoords, &c
*/
public OrderedPair(List<double> Coords) : base(Coords){}
//instance constructor
public OrderedPair(Ntuple toCopy){
this.Coords=toCopy.Coords;
this.Dimension=toCopy.Dimension;
}
}//orderedpair
public class TestProgram{
public static void Main(){
List<double> oneCoords=new List<double>(){1,2};
List<double> otherCoords= new List<double>(){2,3};
OrderedPair one = new OrderedPair(oneCoords);
OrderedPair another = new OrderedPair(otherCoords);
OrderedPair sum1 = new OrderedPair(one + another);
Console.WriteLine(one.Coords[0].ToString()+one.Coords[1].ToString());
Console.WriteLine(sum1.Coords[0].ToString()+sum1.Coords[1].ToString());
bool test = one > another;
Console.WriteLine(test);
bool test2 = one < another;
Console.WriteLine(test2);
}
}
}//namespace ntuples