|
Seriously? Do you mean:
index = x + y * width
That's pretty much basic knowledge.. so I suspect you mean something else, but then what do you mean?
|
|
|
|
|
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'.
Thank you 
|
|
|
|
|
Yes that's why you multiply y by the width
|
|
|
|
|
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
|
|
|
|
|
Epoque wrote: 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
Epoque wrote: 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.
|
|
|
|
|
No offense but that's a really bad idea if all he wants to do is map 2D coordinates to 1D coordinates..
|
|
|
|
|
But seriously, for what this poster wants it will probably be entirely workable - and far far far less complex than what he's trying to do.
Basically this poster is guilty of premature optimization IMO.
|
|
|
|
|
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)
Compare:
array[x + y * width] = value;
Point p = new Point(x, y);
if (dictionary.ContainsKey(p))
dictionary[p] = value;
else
dictionary.Add(p, value);
The choice seems clear - assuming of course that we're talking about a non-sparse grid of known and fixed size.
|
|
|
|
|
I don't see why you're using an unsafe context to do that.
And you basically can't store sixteen bits in an eight-bit byte.
|
|
|
|
|
yet another non-believer.
|
|
|
|
|
Lets have a simple try at this.
If you have a series of X/Y co-ordinates which is assocaited with some data, the simplist method is this:
Dictionary<Point,YourData> dictionary = new Dictionary<Point,YourData>();
where Point is in the System.Drawing namespace (you could define your own if its not fit for your purpose - its just the dictionary key)
and YourData is any class or data structure of your choosing containing data assocaited with x/y coords.
There sure are many more efficient ways of storing data, but why not get your functionality working and worry about optimization when it becomes a bottleneck.
|
|
|
|
|
To quote the late, great Mitch Hedberg when describing 2-in-1 shampoo...
2-in-1 is a BS term, because 1 is not big enough to hold 2. That's why 2 was created. If it was 2 in 1, it would be overflowing... the bottle would be all sticky...
To store 2 byte values, 2 bytes are required. To store 2 nibbles (half-bytes), sure you could use 1 byte, but the question begs why?
|
|
|
|
|
Hi,
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.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
this.label1.Text = listView1.SelectedItems[0].ToString();
}
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
Thanks in advance.........
sampath-padamatinti
|
|
|
|
|
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.
Try this in your code ...
private void listView1_SelectedIndexChanged(object sender, EventArgs e) {
if (listView1.SelectedItems.Count > 0)
label1.Text = listView1.SelectedItems[0].ToString();
}
|
|
|
|
|
Iam new to webparts. I have a list control in a webzone as my webpart.
Now the web part title comes as untitled.
I can set the webpart title using
WebPartZone1.WebParts(0).Title = "MAIN MENU".
But this will fail if i move my webpart to other webpartzone. How can is set the
title even if i move to other webpart zone
|
|
|
|
|
Good morning.
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:
internal static void DemoGrouping()
{
string[] files = Directory.GetFiles(@"C:\Documents and Settings\TEMP");
var fileList = from file in files
where file.Length > 1
orderby file.Length
group file by file.Length;
foreach (var group in fileList)
{
Console.WriteLine(group.Key);
foreach (string subgroup in group)
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?
Thank you, WHEELS
|
|
|
|
|
Don't think there is.
Alernatively though, use DirectoryInfo.GetFiles() . This gives you an array of FileInfo , and from that you can just get the file name (.Name property).
Regards,
Rob Philpott.
|
|
|
|
|
This (or something close to it) should get you just the file name.
string fileName = System.IO.Path.GetFileName(path);
.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.
|
|
|
|
|
Thanks to everyone for the excellent replies.
WHEELS
|
|
|
|
|
Hello;
I am working on a little piece of code that copies files from one location to another. I want to display a progress of the operation in the console. I don't want to use P/Invoke win32.
I use File.Copy() method but it seems it is only possible to get the size before and/or after not during the copy.
For the time being, I use two seprate thread, one for copy and another one that display "=" while the copy thread is alive. This shows a progress but it is not very accurate !
I tried with StopWatch, FileSystemWatcher... Any help on this would be greatly apreciated !

|
|
|
|
|
Why not do the copy yourself in appropriate size chunks (say 4KB), that way you can work out exactly how far through you are?
Regards,
Rob Philpott.
|
|
|
|