Hello,
I've looked online for an implementation of a multidimensional packed
bit array (like BitArray but with more than one dimension), and could
not find any, so I'm trying to create my own. However with limited
success sofar. I'm having troubles with calculating the required size of
the storage and also with the bit shifts... my brain is turning in
circles trying to figure this out ;-)
Anyone want to help with this?
Thomas Bruckner
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// new array 4 dimensions each with 2, 2, 4 and 4 elements
MultiBitArray mba = new MultiBitArray(new int[] { 2, 2, 4,
4 });
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
for (int k = 0; k < 15; k++)
{
for (int l = 0; l < 15; l++)
{
int[] coord = new int[] { i, j, k, l};
// make sure it is initially false
System.Diagnostics.Debug.Assert(mba.GetValue(coord ) == false);
// set to true, and check
mba.SetValue(coord, true);
System.Diagnostics.Debug.Assert(mba.GetValue(coord ) == true);
Console.WriteLine(
mba.GetLocation(coord).ToString());
}
}
}
}
Console.ReadKey();
}
}
public class MultiBitArray
{
/// <summary>
/// Number of dimensions.
/// </summary>
private int dimCount;
/// <summary>
/// bite size of each dimension (may be different)
/// </summary>
private int[] bitSize;
/// <summary>
/// storage for bits
/// </summary>
internal byte[] storage;
public MultiBitArray(int[] bitSize)
{
this.dimCount = bitSize.Length;
this.bitSize = bitSize;
AllocStorage();
}
/// <summary>
/// Allocates storage space for bits.
/// </summary>
private void AllocStorage()
{
long storageSize = 1;
for (int i = 0; i < this.dimCount; i++)
{
storageSize *= (this.bitSize[i] * dimCount);
}
// i can't even figure out how to determine the
// amount of memory... so I set it very large for testing
//storageSize *= 4;
this.storage = new byte[storageSize];
}
/// <summary>
/// Gets the location of a bit according to dimension indexes
/// </summary>
internal int GetLocation(int[] coord)
{
int location = 0;
int bitcount = 0;
for (int i = 0; i < dimCount; i++)
{
location = (location << bitcount) | coord[i];
bitcount = this.bitSize[i];
}
return location;
}
/// <summary>
/// Read a bit.
/// </summary>
public bool GetValue(int[] coord)
{
int location = GetLocation(coord);
return (this.storage[location / 8] & (byte)(1 << (location
% 8))) != 0;
}
/// <summary>
/// Set a bit.
/// </summary>
public void SetValue(int[] coord, bool value)
{
int location = GetLocation(coord);
this.storage[location / 8] |= (byte)(1 << (location % 8));
}
}
} 3 3516
Thomas Bruckner wrote:
I've looked online for an implementation of a multidimensional packed
bit array (like BitArray but with more than one dimension), and could
not find any, so I'm trying to create my own.
What feature do you need that an array of BitArray does not provide ?
Arne
<ch*******@gmail.comwrote in message
news:a4**********************************@v32g2000 hsa.googlegroups.com...
<snip>
>he wanted to know the size, so if I use [2,2]=4bits, [3,3,3]=27bits [3,3,4]=36 bits then
Correct, that is how you determine the storage in bits (as Colin said)
>thus
[2,2] 2*3+2 = 8 [3,3,3] 3*9+3*2+3=36 [3,3,4] 3*9+3*2+4=37
is mathemagical to me..
That is because x*9+y*3+z is the way to calculate the index into the
storage.
If you have [3,3,3] =size 27
this means that
x={0,1,2}
y={0,1,2}
z={0,1,2}
if you plug each possible combination of x,y,z into the equation you
will get an index that varies from 0 thru 26 (one less than the size of
the storage)
[2,2] 1*2 + 1 = 3 = 4-1
[3,3,3] 2*9+2*3+2 = 26 = 27-1
[3,3,4] 2*12+2*4+3=35 = 36 - 1
or more generally
[a,b,c] == b*c*x + c*y + z
and plugging in the max values we get
b*c*(a-1) + c*(b-1) + (c-1) =
b*c*a - b*c + b*c - c + c - 1 = a*b*c - 1
Hopefully I cleared things up a bit (probably just muddied them worse)
Bill
But u are probably correct...
>
//CY- Dölj citerad text -
- Visa citerad text -
So I was trying for the size, and you for the position of the bit in
the array...
think Arne has the right solution an array of arrays...
Think I can follow (draw) your thoughts on 2,2 and 3,3,3 but lost at
(drawing) 4,4,4 ;)
No, dont answer that,,, just silly talk
//CY This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Charles Banas |
last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the...
|
by: Mark Smith |
last post by:
I'm trying to copy data from a 1D array to a 2D array.
The obvious thing doesn't work:
int twoDee = new int;
int oneDee = new int { 1, 2 };
Array.Copy(oneDee, 2, twoDee, 2, 2);
This causes a...
|
by: chris |
last post by:
Hi there,
I created a Multidimensional array of labels
Label lblMultiArray = new Label { {Label3, LblThuTotal},
{Label4,LblFriTotal} };
Now I would like to compare the values in the array,...
|
by: parag.kanade |
last post by:
I have a packet structure which has a field of Integer arrays, that is
packed struct
{
int a;
char b;
int count;
}foo;
I need to initialize all the entries of count to a particular value,
|
by: |
last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily
complicated than the other languages I know. I feel this is because there
aren't many good official resources out there to...
|
by: Chuy08 |
last post by:
If I have a multidimensional array like the following:
Array
$records =Array
0 = 30 year, 6.0;
1 = 30 year, 6.0;
2 = Pay Option, 1.0;
3 = Pay Option, 1.0;
How could I flatten this to...
|
by: Sikandar |
last post by:
Hi,
I am beginner in C. Pls let me know what is packed array in C. Where
is it used?
Thanks,
Sikandar
|
by: LittleCake |
last post by:
Hi All,
I have a multidimensional array where each sub-array contains just two
entries, which indicates a relationship between those two entries. for
example the first sub-array:
=Array
(
=30...
|
by: Slain |
last post by:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |