It just divided the byte in two parts, you could do the same with a short (then you could fit in two complete bytes) or an int (then you could fit in 2 complete shorts or 4 bytes)
But if you want a 2D -> 1D mapping it's better to use something "sane" such as x + y * width or if you don't want to use width you could use morton numbers to get a Z curve (just interleave the bits of your indices)
Well, I plan on having a 2D plain with cells ranging from 15x15 to up to 300x300, and rather than using a library collection storing both values, I would prefer to have both values point to an index within an array.
IE, R1C1 => Index 1, R1C2 => Index 2 etc etc..
I have read that these are possible but with no solutions.
Well, let's say each cell has a corresponding slot in an array containing information about the cell.
Each cell, when clicked or managed, will refer to the single slot in an array corresponding to the co-ordinates or row,column values.
If I was to use a method that relies on adding or multiplying x and y, there are chances that two cell coordinates may intersect and point to the same object. Therefore, I need the mathematical formula that produces an output using x and y to be a unique index key (not too large, otherwise the array would consume considerable amounts of memory), so that it may point to a slot in the array of cell information.
1,1 => Slot 1 in the array
1,2 => Slot 2 in the array
2,1 => Different slot than '1,2'.
I'm confused about what the value of 'width' should be. If width is the size of the cell, then I get conflicting types.
When I ran a debug loop doing (x + y * 16) I received conflicts at 17,1 17,2 etc etc.
And if I use 300 as the width, then the size of the array will be equal to 90,300 which is rediculously massive. Considering each slot in the array will store a structure of information, that could be quite a massive amount of memory usage?
The width of the 2D grid, if your coordinate goes up to 17 then either your width is >= 18 or your x coordinate it out of bounds (in that case it's only normal that it conflicts, it just wrapped to the next row)
Edit: if you don't have a width (if your grid is essentially infinite) then you can still use morton numbers.
I see, i'm sorry if I sound stupid i'm just not used to trying to store parameters like these.
So, say I have a 300x300 map, would the formula be [x + y * 300]?
Using this method, the value of the cell at 300 + 300 * 300 = 90,300. Would it be better to use bit-wise operations to try and reduce the total value down to a small index, or is this getting too complicated?
If need be, I will simply have to make a collection of structures, and make each object key equal to "x,y". Although I would prefer to avoid using any forms of collections, and focus more on indexing values on the 2D map
So, say I have a 300x300 map, would the formula be [x + y * 300]?
Yes. edit: note that the coordinate 300,300 is out of bounds in both directions, a 300x300 map only goes up to 299,299
Would it be better to use bit-wise operations to try and reduce the total value down to a small index, or is this getting too complicated?
There is no reliable way to do that unless your grid is sparse in a specific and known way (in other words: probably not)
If all your cells are used, the x + y * width formula will have an efficiency of 100% (it will number all w*h cells from 0 to w*h-1 with no gaps, so it can not be improved upon)
It seems you are essentially looking for a Hashing algorithm to take 2 values and create a single unique value where the hash would be unique across your entire set; for example the hash of the values 14, 16 would need to be different than 16, 14. I would recommend using one of the built-in collection classes that implement such algorithms rather than trying to create your own. The Hashtable and Dictionary classes should work nicely or you can use the System.Drawing.Point class to create coordinates. Personally, I'd use Dictionary<point, [some_other_type]=""> where the Points are X, Y coordinates of the cells in the set. Since Point is a struct its operated on as a value type and can easily lend itself to what you're trying to do. I believe the default Point struct's X and Y members are int and if you are worried about wasted memory you could create your own Point struct that has X and Y of byte, or short, etc. If you decide to use the Dictionary class try to pass in the number of cells to the constructor (you will hopefully know that upon creation) and it will help performance slightly by preventing it from automatically growing its internal collection as items are added.
Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.
What he is trying to do is flawed, x + y * width is far simpler than a dictionary though.
Sure it'll work, but that doesn't make it good.
edit: the dictionary approach would be better for sparser grids, ok. Otherwise, please no. You would have to check whether a cell exists before trying to use it, instead of just getting the default values (and adding all cells would negate any benefit the dictionary might have had)
array[x + y * width] = value;
Point p = new Point(x, y);
dictionary[p] = value;
The choice seems clear - assuming of course that we're talking about a non-sparse grid of known and fixed size.
I am receiving a problem with my code. Actually in my code I have taken a list view. name is Listview1. Setting values and performing sorting every thing fine. But when I am trying to display selected value like bellow.
In first click it showing correct value. When clicking the other It throwing exception "System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index".
How can i solve this problem.
Only my intention is it has to display selected value in list view.
How it is possible please show me the solution If any one knows this problem
You are half way there. You need to add one more line.
The problem is that when you select a new item, the old item becomes de-selected, and so the "SelectedIndexChanged" event is fired when there are no items selected. Then, the item you clicked becomes selected, and the "SelectedIndexChanged" event is fired again.
I just started using Directory.GetFiles and I am wondering why it returns the path including the file name. It is my expectation that it would only return a list of file names as I already know the path.
I have the follwoing code:
string files = Directory.GetFiles(@"C:\Documents and Settings\TEMP");
//Convention: Name method so that it explains what the query is doingvar fileList = from file in files
where file.Length > 1orderby file.Length
group file by file.Length;
foreach (vargroupin fileList)
//First print the group
foreach (string subgroup ingroup)
Console.WriteLine(" " + subgroup);
Without getting into substrings to extract the file name, is there a usage of Directory.GetFiles that accomplishes what I am looking for?