Some new list.c primitives

Started by Tom Laneover 20 years ago9 messages
#1Tom Lane
tgl@sss.pgh.pa.us

Neil (or anyone else with an opinion),

I'm finding several uses in the planner for some new List primitives
defined as below. I'd like to push these into list.c, but before that,
has anyone got any serious objections? How about suggestions for better
names?

regards, tom lane

/*
* list_add adds the datum to the list if it's not already a member
* (membership is determined by equal()).
*/
static List *
list_add(List *list, void *datum)
{
if (list_member(list, datum))
return list;
else
return lappend(list, datum);
}

/*
* list_add_all does list_add for each element of list2. This is effectively
* the same as list_union(), except that list1 is modified in-place rather
* than being copied.
*/
static List *
list_add_all(List *list1, List *list2)
{
ListCell *cell;

foreach(cell, list2)
{
if (!list_member(list1, lfirst(cell)))
list1 = lappend(list1, lfirst(cell));
}

return list1;
}

#2Gavin Sherry
swm@linuxworld.com.au
In reply to: Tom Lane (#1)
Re: Some new list.c primitives

On Wed, 27 Jul 2005, Tom Lane wrote:

Neil (or anyone else with an opinion),

I'm finding several uses in the planner for some new List primitives
defined as below. I'd like to push these into list.c, but before that,
has anyone got any serious objections? How about suggestions for better
names?

list_add() doesn't really describe what it does. I was thinking either
list_cond_add() or list_merge().

I think list_add_all is also ambiguous. What about list_merge_all() or,
even, list_merge_list()?

Gavin

#3David Fetter
david@fetter.org
In reply to: Tom Lane (#1)
Re: Some new list.c primitives

On Wed, Jul 27, 2005 at 06:01:21PM -0400, Tom Lane wrote:

Neil (or anyone else with an opinion),

I'm finding several uses in the planner for some new List primitives
defined as below. I'd like to push these into list.c, but before that,
has anyone got any serious objections? How about suggestions for better
names?

regards, tom lane

/*
* list_add adds the datum to the list if it's not already a member
* (membership is determined by equal()).
*/
static List *
list_add(List *list, void *datum)
{
if (list_member(list, datum))
return list;
else
return lappend(list, datum);
}

How about list_push for both of these? This opens the door for
possible future functionality like list_pop, list_shift,
list_unshift...

Just my uneducated $.02.

Cheers,
D
--
David Fetter david@fetter.org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: David Fetter (#3)
Re: Some new list.c primitives

David Fetter <david@fetter.org> writes:

How about list_push for both of these?

list_push to me would connote the functionality of lappend, ie,
unconditionally add the item to the list.

regards, tom lane

#5Neil Conway
neilc@samurai.com
In reply to: Gavin Sherry (#2)
Re: Some new list.c primitives

Gavin Sherry wrote:

list_add() doesn't really describe what it does.

I agree -- the functionality itself is fine, of course, but it would be
nice to have a better name.

I was thinking either list_cond_add() or list_merge().

What about list_append_distinct()? (And list_append_all_distinct() for
the "merge two lists" case.)

-Neil

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#5)
Re: Some new list.c primitives

Neil Conway <neilc@samurai.com> writes:

I agree -- the functionality itself is fine, of course, but it would be
nice to have a better name.

Those were just the first names that came to mind, and of course the
reason I asked is that I felt they could be improved upon...

I was thinking either list_cond_add() or list_merge().

What about list_append_distinct()? (And list_append_all_distinct() for
the "merge two lists" case.)

How about list_append_distinct and list_concat_distinct?

regards, tom lane

#7Neil Conway
neilc@samurai.com
In reply to: Tom Lane (#6)
Re: Some new list.c primitives

Tom Lane wrote:

How about list_append_distinct and list_concat_distinct?

Those names are fine with me.

-Neil

#8Thomas Swan
thomas.swan@gmail.com
In reply to: Neil Conway (#7)
Re: Some new list.c primitives

On 7/28/05, Neil Conway <neilc@samurai.com> wrote:

Tom Lane wrote:

How about list_append_distinct and list_concat_distinct?

Those names are fine with me.

list_append_unique and list_concat_unique might be a little clearer, unless
you want to retain the sqlism of distinct.

#9Tom Lane
tgl@sss.pgh.pa.us
In reply to: Thomas Swan (#8)
Re: Some new list.c primitives

Thomas Swan <thomas.swan@gmail.com> writes:

On 7/28/05, Neil Conway <neilc@samurai.com> wrote:

Tom Lane wrote:

How about list_append_distinct and list_concat_distinct?

Those names are fine with me.

list_append_unique and list_concat_unique might be a little clearer, unless
you want to retain the sqlism of distinct.

I like those too --- sold, unless anyone objects?

regards, tom lane