Re: How do I start at a specific position in an array? Is there

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Robert Cummings wrote:
> On Mon, 2005-01-10 at 11:28, Richard Lynch wrote:
>> Thomas Goyne wrote:
>> > On Sun, 09 Jan 2005 15:59:43 +0100, M. Sokolewicz <tularis@xxxxxxx>
>> wrote:
>> >
>> >> that's not a SPECIFIC place in the array, that's just current, next
>> and
>> >> previous. AFAIK there is no way to explicitly set the internal
>> pointer
>> >> of the array to a spcified place. I used a function which basically
>> >> looped trough the array until it got to the correct depth, and then
>> >> returned it by reference....but it's not very efficient :S
>> >>
>> >
>> > Why would you ever want to do that other than to waste cycles?
>>
>> I'll give you a simple case.
>>
>> I have a GTK PHP MP3 ID3 editor application I'm working on.
>>
>> When one opens a file in a directory, I provide next/prev buttons to
>> quickly page to the next/prev file in the directory.
>>
>> Getting the next/prev to work is simple enough, but...
>>
>> I've got the whole array built from opendir/readdir, and sorted it into
>> alphabetical order.
>>
>> I've got a filename inside that array, from the pre-defined
>> GtkFileSelection dialog.
>>
>> So I need to initialize the internal array pointer to the position of
>> the
>> file within that array, there is no such PHP function.
>
> Why not change your structure? Sounds like you want a linked list,
> better yet a doubly linked list. So create a double linked list class,
> link up your entries, and keep a hash available for fast lookup into the
> linked list where the keys are the filenames or whatever you want to use
> as an index, and the value is a reference to the interesting linked list
> node.

Uhhhm.  Yeah.

I could spend hours finding/writing a doubly-linked list class, which will
be a good bit slower and WAY more memory-bloated than the built-in PHP
doubly-linked list (aka 'array') and, in theory, I could code it so that
the 'set_current' function existed and was maybe a bit faster than walking
the PHP array...

But every *other* operation would be a lot slower, and I only need to walk
the list to initialize the internal current pointer once, when the user is
already distracted and slowed down by the Open File dialog.

I think the user experience will be *much* better without all that, though.

>     if( $currNode->hasNext() )
>     {
>         $currNode = &$currNode->next();
>     }

if ($current != $array[count($array) - 1]) $current = next($array);

> Moving backward would be as simple as:
>
>     if( $currNode->hasPrevious() )
>     {
>         $currNode = &$currNode->previous();
>     }

if ($current != $array[0]) $current = prev($array);

Actually, the buttons are disabled if you are on the first/last item, so
the checks for hasNext/hasPrevious are done elsewhere anyway.

> And if the user selects an arbitrary file:
>
>     $currNode = &$lookupArray[$index];

No, no, no.

The FileSelection already returns the filename.

Of course, I just *built* that array based on the directory they chose, so
could initialize the $current_index as the array got built...

So I'd need to keep an *INVERSE* array internally to do:
$current_index = $inversearray[$filename];

So then I'd be keeping track of the $current_index, at which point I'd not
even *NEED* to bother with next/prev and friends:  I'd just have a global
variable for the integer index in the array, and play with that in PHP.

No bloated class objects to track something as simple as an integer from 0
to (count($array) - 1)

Maybe I'll just ditch the internal prev/next stuff and use $CURRENT_INDEX
as a global everywhere and write my own prev/next functions to do integer
arithmetic instead.  Seems kinda silly when it's all already built-in to
PHP *except* set_current() but there ya go.

> This kind of structure works a lot like a doubly threaded red-black tree
> :) Although a red-black tree would keep your entries in sorted order
> whenever you add new entries in O( lg n ) time.

You're comparing O(lg n) in PHP to O(lg n) in built-in C from PHP.  Not
the same at all until we start hitting the kind of numbers that aren't
even useful for the number of files in a single directory.

I'm *not* going to set this box up to have 10,000 files in the same
directory. Puhlease!

The only time something is added to my tree is when they open up a new
directory, at which point the whole tree is re-built from scratch, since
it will have zero (0) elements in common with the previous tree.

As much as I love Object-Oriented programming, it amazes me what lengths
people will go to in order to use it no matter how inappropriate it may be
to the task at hand. :-)

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux