View mode: basic / threaded / horizontal-split · Log in · Help
June 19, 2005
Array indexing & typedef
Does:
typedef int MyType;
char[MyType]	val;

result in val being an associative array, rather than an arrays 
whose indicies are typechecked to be instances of MyType?

If so, is there a valid way of specifying that one wants an array 
of items which is indexed by a specialized type of integer short 
of defining a new class?

If not, what's the approved way to define an associative array 
with an index of ints?

I'm certain that this must have come up before, but I haven't 
been able to find it.
June 19, 2005
Re: Array indexing & typedef
"Charles Hixson" <charleshixsn@earthlink.net> wrote in message 
news:d950du$2u4c$1@digitaldaemon.com...
> Does:
> typedef int MyType;
> char[MyType] val;
>
> result in val being an associative array, rather than an arrays whose 
> indicies are typechecked to be instances of MyType?

Yes.  Any time you put _any_ kind of type inside the brackets, it's an AA. 
This works too:

char[int] val;

val is an AA of chars indexed by ints.  I think an AA with an int key is 
usually called a "sparse array."
June 20, 2005
Re: Array indexing & typedef
Jarrett Billingsley wrote:
> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message 
> news:d950du$2u4c$1@digitaldaemon.com...
> 
>>Does:
>>typedef int MyType;
>>char[MyType] val;
>>
>>result in val being an associative array, rather than an arrays whose 
>>indicies are typechecked to be instances of MyType?
> 
> 
> Yes.  Any time you put _any_ kind of type inside the brackets, it's an AA. 
> This works too:
> 
> char[int] val;
> 
> val is an AA of chars indexed by ints.  I think an AA with an int key is 
> usually called a "sparse array." 
> 
>
Thanks for the info.

Unfortunately, what I wanted wasn't a sparse array, but an array 
that required it's indexes to be a particular type... O, well. 
At other times I want sparse arrays...so that's all to the good.

Any suggestions as to how to require indexes to be of a 
particular type?  (All I can think of is building a class, and 
implementing optAssign, etc.)
June 20, 2005
Re: Array indexing & typedef
On Mon, 20 Jun 2005 13:34:14 -0700, Charles Hixson  
<charleshixsn@earthlink.net> wrote:
> Jarrett Billingsley wrote:
>> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message  
>> news:d950du$2u4c$1@digitaldaemon.com...
>>
>>> Does:
>>> typedef int MyType;
>>> char[MyType] val;
>>>
>>> result in val being an associative array, rather than an arrays whose  
>>> indicies are typechecked to be instances of MyType?
>>   Yes.  Any time you put _any_ kind of type inside the brackets, it's  
>> an AA. This works too:
>>  char[int] val;
>>  val is an AA of chars indexed by ints.  I think an AA with an int key  
>> is usually called a "sparse array."
> Thanks for the info.
>
> Unfortunately, what I wanted wasn't a sparse array, but an array that  
> required it's indexes to be a particular type... O, well. At other times  
> I want sparse arrays...so that's all to the good.
>
> Any suggestions as to how to require indexes to be of a particular  
> type?  (All I can think of is building a class, and implementing  
> optAssign, etc.)

I dont understand what you want. It seems like you're saying you want a  
normal array i.e. sequentialy indexed items and you want to limit which  
type can be used to index it to? why?

I'd guess you have something like:

class Bar{}
enum Foo {
 A,B,C,D,E,F,G
}
Bar[G] data;

and you're trying to get the compiler to limit the indices to the 'data'  
array to the enumerated type, or similar.

Am I on the right track?

Regan
June 20, 2005
Re: Array indexing & typedef
On Mon, 20 Jun 2005 13:34:14 -0700, Charles Hixson wrote:

> Jarrett Billingsley wrote:
>> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message 
>> news:d950du$2u4c$1@digitaldaemon.com...
>> 
>>>Does:
>>>typedef int MyType;
>>>char[MyType] val;
>>>
>>>result in val being an associative array, rather than an arrays whose 
>>>indicies are typechecked to be instances of MyType?
>> 
>> 
>> Yes.  Any time you put _any_ kind of type inside the brackets, it's an AA. 
>> This works too:
>> 
>> char[int] val;
>> 
>> val is an AA of chars indexed by ints.  I think an AA with an int key is 
>> usually called a "sparse array." 
>> 
>>
> Thanks for the info.
> 
> Unfortunately, what I wanted wasn't a sparse array, but an array 
> that required it's indexes to be a particular type... O, well. 
> At other times I want sparse arrays...so that's all to the good.
> 
> Any suggestions as to how to require indexes to be of a 
> particular type?  (All I can think of is building a class, and 
> implementing optAssign, etc.)

Maybe a struct might be useful ...

struct arrtype
{
   int x;
}

void main()
{
   char[][ arrtype ] c;
   
   arrtype a,b;
   a.x = 1;
   b.x = 2;
   c[a] = "one";
   c[b] = "two";
}


-- 
Derek Parnell
Melbourne, Australia
21/06/2005 8:04:42 AM
June 22, 2005
Re: Array indexing & typedef
Regan Heath wrote:
> On Mon, 20 Jun 2005 13:34:14 -0700, Charles Hixson  
> <charleshixsn@earthlink.net> wrote:
>> Jarrett Billingsley wrote:
>>> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message  
>>> news:d950du$2u4c$1@digitaldaemon.com...
>>>
>>>> Does:
>>>> typedef int MyType;
>>>> char[MyType] val;
>>>>
>>>> result in val being an associative array, rather than an arrays 
>>>> whose  indicies are typechecked to be instances of MyType?
>>>   Yes.  Any time you put _any_ kind of type inside the brackets, 
>>> it's  an AA. This works too:
>>>  char[int] val;
>>>  val is an AA of chars indexed by ints.  I think an AA with an int 
>>> key  is usually called a "sparse array."
>> Thanks for the info.
>>
>> Unfortunately, what I wanted wasn't a sparse array, but an array that  
>> required it's indexes to be a particular type... O, well. At other 
>> times  I want sparse arrays...so that's all to the good.
>>
>> Any suggestions as to how to require indexes to be of a particular  
>> type?  (All I can think of is building a class, and implementing  
>> optAssign, etc.)
> 
> I dont understand what you want. It seems like you're saying you want a  
> normal array i.e. sequentialy indexed items and you want to limit which  
> type can be used to index it to? why?
> 
> I'd guess you have something like:
> 
> class Bar{}
> enum Foo {
>  A,B,C,D,E,F,G
> }
> Bar[G] data;
> 
> and you're trying to get the compiler to limit the indices to the 
> 'data'  array to the enumerated type, or similar.
> 
> Am I on the right track?
> 
> Regan
Sort of right.  I'm translating a program from Ada (learning 2 
languages at once).  In Ada if one defines a type and then 
defines an array type, say:
   Type GoalRange is Integer range 0...MaxGoal;
   Type Goals is array (GoalRange) of Goal;

then it's a compilation error if you index the array with a 
variable of any type other than GoalRange.  This isn't necessary 
in order to translate the program...but it does add a layer of 
error checking at compile time that I think useful...*!*IF*!* I 
can add it without too much work.  (OTOH, Ada doesn't have built 
in hash tables...and I've been scratching my head around how to 
replace tree searches that aren't well documented [now where is 
this "out" variable used...and WHY??].)

OTOH, replacing GoalRange with an enumeration is a bit ... 
strange.  There will probably be less than a few hundred 
goals...but they will come and go.  (It's a backwards chaining 
"Expert System Shell"...of the old and "simple" variety.)

The two languages are VERY different, and the right way to do 
things frequently changes GROSSLY when I translate it.  (At one 
point I nearly gave up and said.."I'll just do a literal 
translation!", but really, that's the wrong approach.  I wouldn't 
be doing this at all if I weren't trying to learn from the process.)

OTOH, a sparse array / hash table is definitely NOT what I want 
where I'm just looking to enforce type safety.  (But I sure do 
want it at other times!)
June 22, 2005
Re: Array indexing & typedef
Derek Parnell wrote:
> On Mon, 20 Jun 2005 13:34:14 -0700, Charles Hixson wrote:
> 
>> Jarrett Billingsley wrote:
>>> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message 
>>> news:d950du$2u4c$1@digitaldaemon.com...
>>>
>>>> Does:
>>>> typedef int MyType;
>>>> char[MyType] val;
>>>>
>>>> result in val being an associative array, rather than an arrays whose 
>>>> indicies are typechecked to be instances of MyType?
>>>
>>> Yes.  Any time you put _any_ kind of type inside the brackets, it's an AA. 
>>> This works too:
>>>
>>> char[int] val;
>>>
>>> val is an AA of chars indexed by ints.  I think an AA with an int key is 
>>> usually called a "sparse array." 
>>>
>>>
>> Thanks for the info.
>>
>> Unfortunately, what I wanted wasn't a sparse array, but an array 
>> that required it's indexes to be a particular type... O, well. 
>> At other times I want sparse arrays...so that's all to the good.
>>
>> Any suggestions as to how to require indexes to be of a 
>> particular type?  (All I can think of is building a class, and 
>> implementing optAssign, etc.)
> 
> Maybe a struct might be useful ...
> 
> struct arrtype
> {
>     int x;
> }
> 
> void main()
> {
>     char[][ arrtype ] c;
>     
>     arrtype a,b;
>     a.x = 1;
>     b.x = 2;
>     c[a] = "one";
>     c[b] = "two";
> }
> 
> 
But doesn't that still give you a hash table / sparse array?
No, I think what I need to do (if I decide it's worth it) is 
create a class and implement opIndex, opIndexAssign, and possibly 
opSlice (two versions).  That would (apparently?) allow me to 
specify the index type required...but that's a bit heavy duty 
just to implement a bit of type checking...so I'll probably skip 
that unless I find an easier way, or unless I find another reason 
to create it as a class.
June 22, 2005
Re: Array indexing & typedef
On Tue, 21 Jun 2005 20:37:44 -0700, Charles Hixson wrote:

> Derek Parnell wrote:
>> On Mon, 20 Jun 2005 13:34:14 -0700, Charles Hixson wrote:
>> 
>>> Jarrett Billingsley wrote:
>>>> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message 
>>>> news:d950du$2u4c$1@digitaldaemon.com...
>>>>
>>>>> Does:
>>>>> typedef int MyType;
>>>>> char[MyType] val;
>>>>>
>>>>> result in val being an associative array, rather than an arrays whose 
>>>>> indicies are typechecked to be instances of MyType?
>>>>
>>>> Yes.  Any time you put _any_ kind of type inside the brackets, it's an AA. 
>>>> This works too:
>>>>
>>>> char[int] val;
>>>>
>>>> val is an AA of chars indexed by ints.  I think an AA with an int key is 
>>>> usually called a "sparse array." 
>>>>
>>>>
>>> Thanks for the info.
>>>
>>> Unfortunately, what I wanted wasn't a sparse array, but an array 
>>> that required it's indexes to be a particular type... O, well. 
>>> At other times I want sparse arrays...so that's all to the good.
>>>
>>> Any suggestions as to how to require indexes to be of a 
>>> particular type?  (All I can think of is building a class, and 
>>> implementing optAssign, etc.)
>> 
>> Maybe a struct might be useful ...
>> 
>> struct arrtype
>> {
>>     int x;
>> }
>> 
>> void main()
>> {
>>     char[][ arrtype ] c;
>>     
>>     arrtype a,b;
>>     a.x = 1;
>>     b.x = 2;
>>     c[a] = "one";
>>     c[b] = "two";
>> }
>> 
> But doesn't that still give you a hash table / sparse array?
> No, I think what I need to do (if I decide it's worth it) is 
> create a class and implement opIndex, opIndexAssign, and possibly 
> opSlice (two versions).  That would (apparently?) allow me to 
> specify the index type required...but that's a bit heavy duty 
> just to implement a bit of type checking...so I'll probably skip 
> that unless I find an easier way, or unless I find another reason 
> to create it as a class.

Sorry, I misunderstood. 

So you want a standard array, that is one in which the index is an offset
from the start of the array, but you want to specify that only certain
types of integers are allowed as indexes in array references.

Sounds like a useful idea; a natural extension of static type checking.

-- 
Derek
Melbourne, Australia
22/06/2005 1:52:49 PM
June 22, 2005
Re: Array indexing & typedef
On Tue, 21 Jun 2005 20:28:31 -0700, Charles Hixson  
<charleshixsn@earthlink.net> wrote:
> Regan Heath wrote:
>> On Mon, 20 Jun 2005 13:34:14 -0700, Charles Hixson   
>> <charleshixsn@earthlink.net> wrote:
>>> Jarrett Billingsley wrote:
>>>> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message   
>>>> news:d950du$2u4c$1@digitaldaemon.com...
>>>>
>>>>> Does:
>>>>> typedef int MyType;
>>>>> char[MyType] val;
>>>>>
>>>>> result in val being an associative array, rather than an arrays  
>>>>> whose  indicies are typechecked to be instances of MyType?
>>>>   Yes.  Any time you put _any_ kind of type inside the brackets,  
>>>> it's  an AA. This works too:
>>>>  char[int] val;
>>>>  val is an AA of chars indexed by ints.  I think an AA with an int  
>>>> key  is usually called a "sparse array."
>>> Thanks for the info.
>>>
>>> Unfortunately, what I wanted wasn't a sparse array, but an array that   
>>> required it's indexes to be a particular type... O, well. At other  
>>> times  I want sparse arrays...so that's all to the good.
>>>
>>> Any suggestions as to how to require indexes to be of a particular   
>>> type?  (All I can think of is building a class, and implementing   
>>> optAssign, etc.)
>>  I dont understand what you want. It seems like you're saying you want  
>> a  normal array i.e. sequentialy indexed items and you want to limit  
>> which  type can be used to index it to? why?
>>  I'd guess you have something like:
>>  class Bar{}
>> enum Foo {
>>  A,B,C,D,E,F,G
>> }
>> Bar[G] data;
>>  and you're trying to get the compiler to limit the indices to the  
>> 'data'  array to the enumerated type, or similar.
>>  Am I on the right track?
>>  Regan
> Sort of right.  I'm translating a program from Ada (learning 2 languages  
> at once).  In Ada if one defines a type and then defines an array type,  
> say:
>     Type GoalRange is Integer range 0...MaxGoal;
>     Type Goals is array (GoalRange) of Goal;
>
> then it's a compilation error if you index the array with a variable of  
> any type other than GoalRange.  This isn't necessary in order to  
> translate the program...but it does add a layer of error checking at  
> compile time that I think useful...

Yeah, that is basically what I figured you wanted. I agree it would be  
useful.

> *!*IF*!* I can add it without too much work.  (OTOH, Ada doesn't have  
> built in hash tables...and I've been scratching my head around how to  
> replace tree searches that aren't well documented [now where is this  
> "out" variable used...and WHY??].)

I'm sorry I can't help with Ada. I've never used it.

> OTOH, replacing GoalRange with an enumeration is a bit ... strange.

Yeah, but it's the closest built in basic type to a 'range' that D has I  
reckon. I D got ranges I'd imagine them built onto the enum syntax eg.

enum myInt : int { 0,...,100 }
enum myInt : int { 0 ... 100 }

or something similar.

There have been a number of requests for a range type in D, I can't  
remember Walter's position (or even if he gave one). I suspect at this  
stage 'ranges', while useful, are not high on the list of things to do.

> OTOH, a sparse array / hash table is definitely NOT what I want where  
> I'm just looking to enforce type safety.  (But I sure do want it at  
> other times!)

Sure, they're kind of overkill really for this problem. Tho, they'd do the  
job, right?

Regan
June 22, 2005
Re: Array indexing & typedef
Charles Hixson wrote:
>    Type GoalRange is Integer range 0...MaxGoal;
>    Type Goals is array (GoalRange) of Goal;
> 
> then it's a compilation error if you index the array with a variable of 
> any type other than GoalRange.  This isn't necessary in order to 

Couldn't you just use a static array?  Or am I missing something?

-- Chris Sauls
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home