|
No offense, but your code is odd.
Now of course you can store multiple values in one variable, but they'll be smaller.
Example:
static byte CombineBytes(byte a, byte b)
{
return (byte)((a & 15) | (b << 4));
}
A and B can only be zero through 15 though, obviously.
To get them back:
byte a = (byte)(x & 15);
byte b = (byte)(x >> 4);
warning: all code here is untested. (but really it is just about the idea, not the actual code)
|
|
|
|
|
Thank you for this.
Let's say I want to store a value greater than 15, would it be plausable to use a "short" value instead, to increase the available range of data?
And, would each of the values produced be unique, or would they intersect at some point with conflicting values?
Thank you for your help.
|
|
|
|
|
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)
|
|
|
|
|
Just out of interest; why would you want to do this?
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|