2007-02-12 23:48:05

by Joe Perches

[permalink] [raw]
Subject: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

Now that most of the sizeof(array)/sizeof(array[0])
conversions have been done (there are about 800 done
and about another 130 left), perhaps it could be
useful to change the code to use a define similar
to the list_for_each

#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)

perhaps

#define array_for_each(index, array) \
for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)



2007-02-13 00:21:10

by Ben Nizette

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

Joe Perches wrote:
> Now that most of the sizeof(array)/sizeof(array[0])
> conversions have been done (there are about 800 done
> and about another 130 left), perhaps it could be
> useful to change the code to use a define similar
> to the list_for_each
>
> #define list_for_each(pos, head) \
> for (pos = (head)->next; prefetch(pos->next), pos != (head); \
> pos = pos->next)
>
> perhaps
>
> #define array_for_each(index, array) \
> for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
>

I like the idea, my only concern would be potential confusion. That is,
the list_for_each macro sets pos to each list_head in turn where
array_for_each just sets the index /in to/ the array. While I think the
way you have is nicer, for compatibility between the two styles maybe
something more like

#define array_for_each(element, array) \
for (int __idx = 0; __idx < ARRAY_SIZE((array)); \
__idx++, (element) = &(array[__idx]))

would help. Of course the other option is to name array_for_each
something different to avoid comparisons with list_for_each.

--Ben.

2007-02-13 00:47:23

by Joe Perches

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

On Tue, 2007-02-13 at 11:20 +1100, Ben Nizette wrote:
> #define array_for_each(element, array) \
> for (int __idx = 0; __idx < ARRAY_SIZE((array)); \
> __idx++, (element) = &(array[__idx]))

This requires all interior loop code be changed.

2007-02-13 04:19:19

by Nick Piggin

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

Joe Perches wrote:
> On Tue, 2007-02-13 at 11:20 +1100, Ben Nizette wrote:
>
>> #define array_for_each(element, array) \
>> for (int __idx = 0; __idx < ARRAY_SIZE((array)); \
>> __idx++, (element) = &(array[__idx]))
>
>
> This requires all interior loop code be changed.

Ben is right though. Making this thing confusing to use is going
to be worse than sticking with the very simple and unconfusing
loops.

If you really wanted to introduce your loop, then please call it
array_for_each_idx, or something to distinguish it.

--
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com

2007-02-13 07:34:09

by Joe Perches

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

On Tue, 2007-02-13 at 15:19 +1100, Nick Piggin wrote:
> >> #define array_for_each(element, array) \
> >> for (int __idx = 0; __idx < ARRAY_SIZE((array)); \
> >> __idx++, (element) = &(array[__idx]))
> If you really wanted to introduce your loop, then please call it
> array_for_each_idx, or something to distinguish it.

perhaps:

#define array_for_each(element, array) \
for ((element) = (array); \
(element) < ((array) + ARRAY_SIZE((array))); \
(element)++)

#define array_for_each_index(index, array) \
for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)


2007-02-13 07:37:43

by Muli Ben-Yehuda

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

On Mon, Feb 12, 2007 at 03:47:50PM -0800, Joe Perches wrote:

> Now that most of the sizeof(array)/sizeof(array[0]) conversions have
> been done (there are about 800 done and about another 130 left),
> perhaps it could be useful to change the code to use a define
> similar to the list_for_each
>
> #define list_for_each(pos, head) \
> for (pos = (head)->next; prefetch(pos->next), pos != (head); \
> pos = pos->next)
>
> perhaps
>
> #define array_for_each(index, array) \
> for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)

Could we please stop "improving" the C language? it has served us fine
so far.

Cheers,
Muli

2007-02-13 07:43:10

by Nick Piggin

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

Joe Perches wrote:
> On Tue, 2007-02-13 at 15:19 +1100, Nick Piggin wrote:
>
>>>> #define array_for_each(element, array) \
>>>> for (int __idx = 0; __idx < ARRAY_SIZE((array)); \
>>>> __idx++, (element) = &(array[__idx]))
>>
>>If you really wanted to introduce your loop, then please call it
>>array_for_each_idx, or something to distinguish it.
>
>
> perhaps:
>
> #define array_for_each(element, array) \
> for ((element) = (array); \
> (element) < ((array) + ARRAY_SIZE((array))); \
> (element)++)

If you're going for consistency, then shouldn't this be
array_for_each_entry()?

> #define array_for_each_index(index, array) \
> for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)

--
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com

2007-02-13 07:49:05

by YOSHIFUJI Hideaki

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

In article <1171324070.1528.25.camel@localhost> (at Mon, 12 Feb 2007 15:47:50 -0800), Joe Perches <[email protected]> says:

> Now that most of the sizeof(array)/sizeof(array[0])
> conversions have been done (there are about 800 done
> and about another 130 left), perhaps it could be
> useful to change the code to use a define similar
> to the list_for_each
>
> #define list_for_each(pos, head) \
> for (pos = (head)->next; prefetch(pos->next), pos != (head); \
> pos = pos->next)
>
> perhaps
>
> #define array_for_each(index, array) \
> for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)

I dislike this; it is overkill.

list_for_each etc. are for list_head etc., of structures.
On the other hand, arrays are not.

It is very, very obvious how to access its members.

--yoshfuji

2007-02-13 10:36:42

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

On Tue, 2007-02-13 at 18:42 +1100, Nick Piggin wrote:
> Joe Perches wrote:
[...]
> > perhaps:
> >
> > #define array_for_each(element, array) \
> > for ((element) = (array); \
> > (element) < ((array) + ARRAY_SIZE((array))); \
> > (element)++)
>
> If you're going for consistency, then shouldn't this be
> array_for_each_entry()?

That depends on the decision between consistency to array_for_each_index
or consistency to list_for_each.

> > #define array_for_each_index(index, array) \
> > for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)

Bernd
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services

2007-02-13 10:54:42

by Nick Piggin

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

Bernd Petrovitsch wrote:
> On Tue, 2007-02-13 at 18:42 +1100, Nick Piggin wrote:
>
>>Joe Perches wrote:
>
> [...]
>
>>>perhaps:
>>>
>>>#define array_for_each(element, array) \
>>> for ((element) = (array); \
>>> (element) < ((array) + ARRAY_SIZE((array))); \
>>> (element)++)
>>
>>If you're going for consistency, then shouldn't this be
>>array_for_each_entry()?
>
>
> That depends on the decision between consistency to array_for_each_index
> or consistency to list_for_each.

I don't follow.

list_for_each gives you a list_head.
list_for_each_entry gives you a pointer to an entry in the list, which
is equivalent to the above loop which gives a pointer to an entry in the
array. Accordingly, it should be called array_for_each_entry. What sort
of logic leads to another conclusion?

array_for_each_index gives an index into the array.

I offer no opinion on the merit of such macros, just their names.

--
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com

2007-02-13 11:07:57

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

On Tue, 2007-02-13 at 21:54 +1100, Nick Piggin wrote:
> Bernd Petrovitsch wrote:
> > On Tue, 2007-02-13 at 18:42 +1100, Nick Piggin wrote:
> >
> >>Joe Perches wrote:
> >
> > [...]
> >
> >>>perhaps:
> >>>
> >>>#define array_for_each(element, array) \
> >>> for ((element) = (array); \
> >>> (element) < ((array) + ARRAY_SIZE((array))); \
> >>> (element)++)
> >>
> >>If you're going for consistency, then shouldn't this be
> >>array_for_each_entry()?
> >
> >
> > That depends on the decision between consistency to array_for_each_index
> > or consistency to list_for_each.
>
> I don't follow.

Yes, thinko on my side. Sorry.

> list_for_each gives you a list_head.
> list_for_each_entry gives you a pointer to an entry in the list, which
> is equivalent to the above loop which gives a pointer to an entry in the
> array. Accordingly, it should be called array_for_each_entry. What sort
> of logic leads to another conclusion?

The wrong logic that list_for_each gives an entry. Sorry f.t. confusion.

Bernd
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services

2007-02-13 16:33:43

by Randy Dunlap

[permalink] [raw]
Subject: Re: Coding style RFC: convert "for (i=0;i<ARRAY_SIZE(array);i++)" to "array_for_each(index, array)"

On Tue, 13 Feb 2007 09:37:38 +0200 Muli Ben-Yehuda wrote:

> On Mon, Feb 12, 2007 at 03:47:50PM -0800, Joe Perches wrote:
>
> > Now that most of the sizeof(array)/sizeof(array[0]) conversions have
> > been done (there are about 800 done and about another 130 left),
> > perhaps it could be useful to change the code to use a define
> > similar to the list_for_each
> >
> > #define list_for_each(pos, head) \
> > for (pos = (head)->next; prefetch(pos->next), pos != (head); \
> > pos = pos->next)
> >
> > perhaps
> >
> > #define array_for_each(index, array) \
> > for ((index) = 0; (index) < ARRAY_SIZE((array)); (index)++)
>
> Could we please stop "improving" the C language? it has served us fine
> so far.

I'm with Muli. The open-coded for loop is fine (using ARRAY_SIZE).

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***