Re: Support for negative index values in array fetching

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Florian Pflug <fgp(at)phlo(dot)org>
Cc: Peter Eisentraut <peter_e(at)gmx(dot)net>, "Valtonen, Hannu" <hannu(dot)valtonen(at)hut(dot)fi>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Support for negative index values in array fetching
Date: 2011-01-05 17:36:49
Message-ID: AANLkTin9ADoaWxbx9mgDXzkS_AUM_kYY-uDaMQ5QQMfD@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

2011/1/5 Florian Pflug <fgp(at)phlo(dot)org>:
> On Jan5, 2011, at 15:17 , Pavel Stehule wrote:
>> 2011/1/5 Florian Pflug <fgp(at)phlo(dot)org>:
>>> How so? You'd still be able to get the last element by simply writing
>>>
>>>  array_relative(some_array, array[-1]).
>>>
>>> Or, if we made the function variadic, by writing
>>>
>>>  array_relative(some_array, -1).
>>
>> Sorry, but It isn't too intuitive. Minimally for me. Why you don't
>> thinking about simple functions with only positive arguments. There
>> are only four combinations. I don't think we must have only one super
>> function.
>>
>> we need functionality for:
>>
>> a) get first n items
>> b) get items without last n items
>> c) get last n items
>> d) skip first n items
>
> Now you've moved the goalpost - the OP wanted to access individual
> elements, not slices! To support slices, a three-argument version
> of array_relative() would be required, with the signature
>

I am not sure. Usually need both

when I play with a stack I need

a) FIFO - first element from array and all others without first element
b) LIFO - last element from array and all others without last element

The game with queues is only one use case that I know where I need
access to relative indexed items in array.

Maybe is other, but I don't know it. ??? I don't know why I need a
access to relative indexed items?

Pavel

>  array_relative(some_array anyarray, first int[], last int[])
>
> Your requirements (a) to (d) are then easily satisfied
>
> a) array_relative(ary, array[0], array[n-1])
> b) array_relative(ary, array[0], array[-n-1])
> c) array_relative(ary, array[-n], array[-1])
> d) array_relative(ary, array[n], array[-1])
>

what is n?? it's not implementable.

> The individual function approach might be a tad more readable for
> one-dimensional arrays, but they don't scale well to the general
> case.
>
> Maybe the OP could comment on whether any of these solutions
> would fit his needs?
>
> best regards,
> Florian Pflug
>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2011-01-05 17:59:50 Re: Visual Studio 2010/Windows SDK 7.1 support
Previous Message Magnus Hagander 2011-01-05 17:33:32 Re: Visual Studio 2010/Windows SDK 7.1 support