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?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
GetFiles() and the like return the full path, which is useful as you can ask those methods to work hierarchically (SearhOption.AllDirectories), so just a filename would not always be sufficient.
You can extract the filename from a full path using the Path class, you can't do the reverse.
BTW: Using DirectoryInfo to get all of them at once is a costly operation if you only are interested in a few of the results returned.