F
Frank Hileman
That's not hard. Use an array of unsigned ints; upper 3 can be type
identifier; remaining bits index into array of a specific type. For example,
wrap it in a struct to make it easier:
enum ArrayType
{
Int = 0x1,
Double = 0x2,...
}
struct ArrayEntry
{
const unsigned int typeMask = 0x7 << 29;
const unsigned int indexMask = ~ typeMask;
usigned int data;
public ArrayEntry(ArrayType type, int index)
{
data= 0; // for compiler warning
Type = type;
Index = index;
}
ArrayType Type
{
get { return (ArrayType)(data>> 29); }
set { value = Index | ((int)data) << 29); }
}
int Index
{
get { return data & indexMask; }
set { data = Type | (value & indexMask); }
}
}
Then if you have ArrayEntry[] a, you do a.Index, a.Type, then use that
info to index into the type-specific arrays, which I would call pools, if
you choose to use them that way.
Regards,
Frank Hileman
Prodige Software Corporation
check out VG.net: www.prodigesoftware.com/screen5.png
An animated vector graphics system integrated in VS.net
beta requests: vgdotnetbeta at prodigesoftware.com
identifier; remaining bits index into array of a specific type. For example,
wrap it in a struct to make it easier:
enum ArrayType
{
Int = 0x1,
Double = 0x2,...
}
struct ArrayEntry
{
const unsigned int typeMask = 0x7 << 29;
const unsigned int indexMask = ~ typeMask;
usigned int data;
public ArrayEntry(ArrayType type, int index)
{
data= 0; // for compiler warning
Type = type;
Index = index;
}
ArrayType Type
{
get { return (ArrayType)(data>> 29); }
set { value = Index | ((int)data) << 29); }
}
int Index
{
get { return data & indexMask; }
set { data = Type | (value & indexMask); }
}
}
Then if you have ArrayEntry[] a, you do a.Index, a.Type, then use that
info to index into the type-specific arrays, which I would call pools, if
you choose to use them that way.
Regards,
Frank Hileman
Prodige Software Corporation
check out VG.net: www.prodigesoftware.com/screen5.png
An animated vector graphics system integrated in VS.net
beta requests: vgdotnetbeta at prodigesoftware.com
100 said:The real problem with this idea is - how we can possibly know which type is
the object at given index in order to know what overload to use to retrieve
the data. We can provide method that returns the type of an object by index
value. But then we need to check this type and call the appropriate method
which will introduce more overhead when reading data. It may turns that
boxing/unboxing is the best solution and the problem with the prformance is
not there.
That's why I think we can't come up with good idea unless Mountain gives us
an example how he is planning to use this storage. He may not need to have
the objects ordered and then this solution will work and we won't have the
problem of wasting the memory.
B\rgds
100
So100 said:Hi Paul,
You are hundred percent right. That was my fault. I wrote this minutes
before I went home yesterday and its my fault that I posted this without any
revision.
Thanks for bringing this out. We can't use indexer for that. What we can do
is to use separate operations SetData and GetData with as many overloads as
different types we want to accept. GetData cannot return value because ot
will introduce the same problem that we have already with the indexer.we
can have prototype like
void GetData(int index, out <type> value)
{
}
this should fix the problem.
B\rgds
100
Paul Robson said:100 wrote:
class Storage
{
int[] intArr= new int[10];
double[] dblArr = new int[10];
object[] refArr = new object[10];
public int this[int i]
{
get{return intArr;}
set{intArr = value;}
}
public double this[int i]
I thought that C# couldn't do polymorphism on return types only ? Am I
wrong ?