I have 2 structs in C language:
struct CSquare
{
char Side;
int Row;
int Col;
};
struct CSide
{
char m_Side;
char m_Blocks[3][3];
CSquare *m_Moves;
};
and C++ code:
int Count = 0;
int Flag = 0;
if (m_Down->m_Blocks[0][1] == *m_Down)
{
Count++;
Flag |= 2;
// type of m_Down is CSide
}
I'm trying to convert they to C#:
public class Square
{
public char Side { get; set; }
public int Row { get; set; }
public int Col { get; set; }
}
public class CubeSide
{
private char Side { get; set; }
private char[,] _block = new char[3, 3];
private Square[] moves;
public char[,] Block
{
get { return _block; }
set { _block = value; }
}
internal Square[] Moves
{
get { return moves; }
set { moves = value; }
}
}
But I don't know how to convert line:
if (m_Down->m_Blocks[0][1] == *m_Down)
to C#?
How can I convert this line to C#?
this line make no sense, i guess it's always evaluate to false.
What you can do is to set a breakpoint on that line and perform a quick watch, evaluate *m_Down to make sure there's no overload operator.
Then, evaluate the condition. Depending on your type of project, put some printf("inside the if")/printf("inside the else"), messagebox or write it in a file. If the condition is evaluate to true, print the value of m_Down->m_BLocks[0][1] and *m_Down...
Make sure you first understand the logic behind this line. Once you understand it, it will be easy to write it in c#
PS: in C#, use Byte instead of Char
Related
Let's say I have a text file and a line like this:
This is an example line within a file.
What I need to do is to modify this line based on a prefixed column position, and an input expectedString.
For example:
When i want to modify "example" text from the line above:
I would start from position 11 of that line as an input, and take 7 characters.
It would be something like:
TestMethod1()
{
int posStart = 11;
int posEnd = 17;
ModifyLine(line number, posStart, posEnd, expectedString)
}
I may have many similar methods with the only different is the posStart, and posEnd. I want to change it to a shorter version like this:
TestMethod1()
{
ModifyLine(line number, examplePosStart, examplePosEnd, stringExpected)
}
+examplePosStart, examplePosEnd would be declared somewhere not in the same file.
class TextPosition
{
public constant int example1PosStart = 11;
public constant int example1PosEnd = 17;
public constant int example2PosStart = 18;
public constant int example2PosStart = 25;
}
I am wondering are there any other more optimal ways to declare all the posStarts, posEnds in one place like above?
You can use an array
public class TextPosition
{
public int StartPos { get; set; }
public int EndPos { get; set; }
public static readonly TextPosition[] Positions = new[] {
new TextPosition { StartPos = 11, EndPos = 17 },
new TextPosition { StartPos = 18, EndPos = 25 }
}
}
I'm trying to convert some C++ into C# for a personal learning project.
Below you will see my C++ and then my attempt at converting it into C# using Unity classes like Vector3.
My question:
How do I handle pointers to a struct inside a struct of the same type?
As far as I can tell it's not possible (HexTri m_nbAB, m_nbBC, m_nbCA;)
Do I need to use a class instead ?
.h file
struct HexTri;
struct HexTile
{
HexTile( Imath::V3f p );
Imath::V3f m_vertPos;
Imath::V3f m_nrm;
enum {
Terrain_WATER,
Terrain_DESERT,
Terrain_GRASSLAND,
Terrain_FOREST,
Terrain_MOUNTAIN
};
int m_terrain;
std::vector<HexTri*> m_hextri;
};
struct HexTri
{
HexTri( size_t a, size_t b, size_t c );
size_t m_hexA, m_hexB, m_hexC;
HexTri *m_nbAB, *m_nbBC, *m_nbCA;
union {
size_t m_newvert;
float m_angle;
} m_tmp;
};
.cpp file
HexTile::HexTile( Imath::V3f p ) :
m_vertPos( p )
{
m_terrain = HexTile::Terrain_DESERT;
m_nrm = p.normalize();
}
HexTri::HexTri( size_t a, size_t b, size_t c) :
m_hexA( a ), m_hexB( b ), m_hexC( c )
{
m_nbAB = NULL;
m_nbBC = NULL;
m_nbCA = NULL;
}
Here is my C# conversion so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct HexTile
{
private Vector3 _position;
public Vector3 Position
{
get
{
return _position;
}
set
{
_position = value;
}
}
private Vector3 _normal;
public Vector3 Normal
{
get
{
return _normal;
}
set
{
_normal = value;
}
}
enum terrain{
Terrain_WATER,
Terrain_DESERT,
Terrain_GRASSLAND,
Terrain_FOREST,
Terrain_MOUNTAIN
};
List<HexTri> hextri;
public HexTile( Vector3 position, Vector3 normal)
{
// Defaults
_position = new Vector3(0,0,0);
_normal = new Vector3(0,0,0);
hextri = new List<HexTri>();
// Initilize with value
Position = position;
Normal = normal;
}
}
public struct HexTri
{
private int _hexA;
public int HexA
{
get
{
return _hexA;
}
set
{
_hexA = value;
}
}
private int _hexB;
public int HexB
{
get
{
return _hexB;
}
set
{
_hexB = value;
}
}
private int _hexC;
public int HexC
{
get
{
return _hexC;
}
set
{
_hexC = value;
}
}
// Q1 No pointers, cant do this
HexTri m_nbAB, m_nbBC, m_nbCA; //??
public HexTri( int a, int b, int c)
{
// Defaults
_hexA = -1;//??
_hexB = -1;//??
_hexC = -1;//??
// Initilize with value
HexA = a;
HexB = b;
HexC = c;
}
}
My take on this:
You are in the right way declaring 3 objects of HexTri type.
EDIT to address the point explained by pinkfloydx33 in a comment:
If you declared HexTri as a class instead of a struct, this C# this code makes a and b "point" to the same object:
HexTri a = new HexTri();
HexTri b = a;
A good explanation of the differences between classes and structs in C# (they are not the same differences as in class vs struct in C++) in this answer.
As described in the title I'm unsure what a particular feature is called when a struct is treated as an array. For example, Unity3D has the Color struct which has 4 public floats r,g,b,a and public float this[int index] { get; set; }. Is there a particular term for what this feature is called? I'm coming from Java and just starting to learn about C#, I tried to look up what this was but was only finding stuff regarding how to create an array of structs.
It is called an "indexer property"
Example:
public struct Color
{
public int R { get; }
public int G { get; }
public int B { get; }
public int this[int index]
{
get
{
switch(index)
{
case 0: return R;
case 1: return G;
case 2: return B;
}
throw new IndexOutOfRangeException();
}
}
}
(Note: not the real Color you use, just an example off my head)
A property like that is called indexer:
public struct Color
private float[] components;
public float this[int index] { //<-- indexer.
get {
if (components == null) {
components = new float[4];
}
return components[index];
}
set {
if (components == null) {
components = new float[4];
}
components[index] = value;
}
}
}
Apparently the developers of Unity3D sometimes wanted to access the Color as if it was an collection of values for ARGB (aka channels). They used the indexer operator in order to mimic that.
You can read more on indexer operators on MSDN here and here.
I'm looking for an implementation of a three dimensional circular buffer in C# .NET that allows me to extract slices of the buffer.
I tried the multidimensional array but it doesn't allow me to get a specific slice of the buffer.
I also tried to implement a version using jagged array, but i have find some problems in understanding how to initialzie the matrix, and also in slice extraction.
Thanks for the help.
[EDIT 1]
The buffer will be used to store data from reltime sensors, it will be fixed in width height and the depth (time which be the circular part) will be defined by the user.
The slices i need will be a fixed size matrix (width and heigh with the data of a given time).
[EDIT 2]
The only implementation that i could implement that works. With the jagged array i'm still stuck at the declaration (i find it chaotic)
public class CircularBufferMatrix<T>
{
private readonly T[,,] _buffer;
public CircularBufferMatrix(int width, int height, int depth)
{
Width = width;
Height = height;
Depth = depth;
_buffer = new T[width, height, depth];
}
public T this[int x, int y, int z]
{
set { _buffer[x, y, z%Depth] = value; }
get { return _buffer[x, y, z%Depth]; }
}
#region Getters
public int Width { get; }
public int Height { get; }
public int Depth { get; }
#endregion
}
I would split these in two classes. You want something like a CircularBuffer class, which handles the reading and writing. The other could be an implementation of the 2D array you want to store. The reason of splitting these, is because you want to Read/write frames separately.
For example:
Circular buffer implementation:
public class CircularBuffer<T>
{
private T[] _buffer;
private int IncRollover(int value)
{
value++;
if (value >= _buffer.Length)
value = 0;
return value;
}
public CircularBuffer(int count)
{
_buffer = new T[count];
}
public bool Write(T element)
{
// if the writeindex (after increasing) equals the readindex, the buffer is full
var newWriteIndex = IncRollover(WriteIndex);
if (newWriteIndex == ReadIndex)
return false;
_buffer[WriteIndex] = element;
WriteIndex = newWriteIndex;
return true;
}
public bool TryRead(out T element)
{
if (ReadIndex == WriteIndex)
{
element = default(T);
return false;
}
element = _buffer[ReadIndex];
ReadIndex = IncRollover(ReadIndex);
return true;
}
public IEnumerable<T> ReadAll()
{
T element;
while (TryRead(out element))
yield return element;
}
public int ReadIndex { get; private set; }
public int WriteIndex { get; private set; }
}
This will take care of reading and writing induvidual 'frames'/slices. You can expand this class if you like to read on Index.
note: The write will return false if the buffer is full
This could be an implementation of the 2d buffers stored within the Circular buffer:
public class MyWhateverBuffer<T>
{
private CircularBuffer<T[,]> _buffer;
public int Width { get; private set; }
public int Height { get; private set; }
public int Depth { get; private set; }
public MyWhateverBuffer(int width, int height, int depth)
{
Width = width;
Height = height;
Depth = depth;
_buffer = new CircularBuffer<T[,]>(depth);
}
public T[,] New()
{
return new T[Width, Height];
}
public bool Add(T[,] buffer)
{
return _buffer.Write(buffer);
}
public bool TryRead(out T[,] buffer)
{
return _buffer.TryRead(out buffer);
}
public IEnumerable<T[,]> ReadAll()
{
return _buffer.ReadAll();
}
}
This class can be used like:
MyWhateverBuffer<double> myBuffer = new MyWhateverBuffer<double>(100, 100, 100);
var oneSlice = myBuffer.New();
oneSlice[10, 10] = 3.5;
oneSlice[50, 10] = 23.5;
oneSlice[10, 20] = 43.5;
myBuffer.Add(oneSlice);
var anotherSlice = myBuffer.New();
anotherSlice[10, 10] = 13.5;
anotherSlice[50, 10] = 23.5;
anotherSlice[10, 20] = 43.5;
var result = myBuffer.Add(anotherSlice);
if(!result)
{
// the buffer was full..
}
// Read the results from the buffer.
foreach(var slice in myBuffer.ReadAll())
{
Trace.WriteLine(slice[10, 10]);
}
You should always check if the buffer could be added. You don't want to lose info.
Side note:
The most profit gains with a Circular buffer is declaring the elements ones. Like big arrays will be reused every time.
If I understand your requirements correctly, you want a circular buffer consisting of items that are two dimensional arrays of data and the data from one item is never repeated in other items. C# doesn't have a cicular buffer type but you can always create one yourself:-
public class CircularBuffer <T> // make it generic
{
public class Iterator
{
public void MoveForward ();
public void MoveBackward ();
public T Item { get { } };
CicrcularBuffer <T> m_container;
LinkedListNode <T> m_current_item;
}
public void Add (T item);
public Iterator GetIterator ();
LinkedList<T> m_data;
}
Then create a class to contain your data:-
public class SensorData
{
// contains the two dimension data
}
And use it like:-
var buffer = new CircularBuffer <SensorData> ();
var iterator = buffer.GetIterator ();
var item = iterator.Item;
iterator.MoveForward ();
Obviously, there's a lot to be filled in but it should get you started.
I have a 2d array of a class. The size of array is very large (around 3000*3000) and accessing the array with ordinary row and column method is taking very much time. For this purpose, I want to use pointers to access the array.
Following is my array code:
Class definition:
Class BoxData
{
Size _bound;
bool _isFilled=false;
Color _color=Colors.White;
public Size Bounds
{
get
{
return _bound;
}
set
{
_bound=value;
}
}
public bool IsFilled
{
get
{
return _isFilled;
}
set
{
_isFilled=value;
}
}
public Color FillColor
{
get
{
return _color;
}
set
{
_color=value;
}
}
}
Class used as array in application:
BoxData[,] boxData=new BoxData[3000,3000];
I want to access boxData with pointers.
Thanks
Try a jagged array instead of a multi dimensional one, they are faster in Microsoft's CLR implementation
BoxData[][] boxData=new BoxData[3000][];
for (int i=0; i<3000; i++)
boxData[i] = new BoxData[3000];
Maybe you could use a struct instead of a class for BoxData ?
Struct is a value type: as you declare your array, everything will be populated already.
You will not longer use a loop to create new BoxData() instances.
var x = new BoxData[3000,3000]; // Populated array of BoxData
Because of struct vs class restrictions, you will have to remove initializers this way...
struct BoxData
{
Size _bound;
bool _isFilled; // = false;
Color _color; // = Color.White;
public Size Bounds
{
get
{
return _bound;
}
set
{
_bound = value;
}
}
public bool IsFilled
{
get
{
return _isFilled;
}
set
{
_isFilled = value;
}
}
public Color FillColor
{
get
{
return _color;
}
set
{
_color = value;
}
}
}
...and initialize your default values using a loop will be much more faster.
for (int j = 0; j < 3000; j++)
for (int i = 0; i < 3000; i++)
x[i, j].FillColor = Color.White;