RE: [PHP] in the middle of shift and pop

2003-08-21 Thread Ford, Mike [LSS]
> -Original Message-
> From: Decapode Azur [mailto:[EMAIL PROTECTED]
> Sent: 20 August 2003 23:54
> 
> > If you unset an array that isn't associative, will that 

ALL PHP arrays are associative.  It's just that some arrays have only numeric keys.

> mean there will
> > be a gap in the numbers?

 
> [snip]--8<---
> 
> > I'm compelled to ask *why* you want that result.  In most 
> cases it makes
> > no difference to your coding, so the only real reason I can see is a
> > somewhat obsessive desire for neatness!
> 
> Perhaps there is a better way to grab the good points to 
> build the faces,
> but I did not find another solution yet.
> If you know about another better issue to resolve this 
> problem, please let 
> me know about it.

Well, I can't really help here without seeing some relevant code.  You may be right, 
and what you are doing does require no gaps in your keys -- but it may be that you can 
program round that more efficently than you can re-index the array when you delete a 
value.

That said, I'd answer your original question with:

array_splice($arr, $n, 1);

which should actually be pretty efficient!


Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

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



Re: [PHP] in the middle of shift and pop

2003-08-20 Thread Robert Cummings
On Wed, 2003-08-20 at 23:38, Jaap van Ganswijk wrote:
> 
> Finding a single element in a doubly linked list can
> be sped up using a hash-code table, but it also
> makes things like renumbering numerically indexed
> entries a lot harder (I think, I never used a
> combination of these things.)

Doubly threaded red-black trees are good for in order traversal and
arbitrary lookup. As with most structures that provide more speed, you
use more memory :)  O( lg n ) insertion, O( lg n ) deletion, O( lg n )
search. Threaded means the in order nodes are threaded into a linked
list. PHP I believe uses hashes of some sort, I'm not sure how they keep
track of the insertion order for doing a foreach loop -- I guess we
could look. On a more related topic: if he's happy with the unset()
function but wants the indexes renumbered, he could write a C function
which performs the renumbering. Should keep it relatively fast and
provide the option of renumbering -- or not.

Cheers,
Rob.
-- 
.-.
| Worlds of Carnage - http://www.wocmud.org   |
:-:
| Come visit a world of myth and legend where |
| fantastical creatures come to life and the  |
| stuff of nightmares grasp for your soul.|
`-'

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



Re: [PHP] in the middle of shift and pop

2003-08-20 Thread Jaap van Ganswijk
At 2003-08-21 00:54 +0200, Decapode Azur wrote:
>> Or will PHP realize "OK I need to change the numbers indexing the other
>> elements"?
>
>** as PHP is a high level language I can write very quickly and very easily 
>scripts to make manipulations on my 3D meshes, but the problem is that it 
>often takes more than an hour to execute, so I realy need to learn about 
>writing efficient code and optimizing PHP...

Perhaps you should have a look at the source code of
the PHP-interpreter.

I used to program in C and when I wanted to use flexible
data structures like those in PHP I generally would use
doubly linked lists and trees of them etc. (Meshes are
much harder to maintain of course.) One can also use
hash-tables. But some things can be fast given a certain
implementation and others can be very slow. When you
have an unusual problem (and you seem to have) then
it's crucial to either control the mechanisms yourself
or to know how PHP does it (but you shouldn't count on
the fact that any internal mechanism will stay the same
over time).

As regards doubly linked lists (that I like a lot):
You can travel through such a list in linear speed,
but for example removing a couple of middle elements
will cost in the order of n*0.5 iterations through
the list (to find the first element to remove), so
repeatly removing elements will be very expensive.

Finding a single element in a doubly linked list can
be sped up using a hash-code table, but it also
makes things like renumbering numerically indexed
entries a lot harder (I think, I never used a
combination of these things.)

But it's hard to estimate these things. Merging two
already sorted linked lists into one takes linear
time of course. I once even deviced a quick sort
algorithm for doubly linked lists and was pleasently
surprised that it was possible. (Qsort is of order
n*log(n) unlike most sorting algorithms which are n*n).

You may also want to consider writing some kind
of underlying engine in C and then make it accessible
from PHP for easy scripting. Most programs spend
only 10-20% of their time in the fluffy code, which
can therefore be written in a programmer-friendly
language and 80-90% of their time in computationally
intensive code that had better be optimized for speed.

Years ago we would even hand-optimize the assembler
output of the C-compiler to make the kernel of an
application faster...

Greetings,
Jaap


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



Re: [PHP] in the middle of shift and pop

2003-08-20 Thread Decapode Azur
> If you unset an array that isn't associative, will that mean there will
> be a gap in the numbers?

Yes the items are grabed by their index.
I'm using those arrays to manipulate 3D geometric shapes,
(either vrml/x3d, OpenGL, POV and other ones**)
so cartesian coordinates are first referenced in a first array,
and then faces (facets?) are build giving the index of 3 (or more) points,
so the index need to be the good one and not on a null or the next value.

> Or will PHP realize "OK I need to change the numbers indexing the other
> elements"?

** as PHP is a high level language I can write very quickly and very easily 
scripts to make manipulations on my 3D meshes, but the problem is that it 
often takes more than an hour to execute, so I realy need to learn about 
writing efficient code and optimizing PHP...

[snip]--8<---

> I'm compelled to ask *why* you want that result.  In most cases it makes
> no difference to your coding, so the only real reason I can see is a
> somewhat obsessive desire for neatness!

Perhaps there is a better way to grab the good points to build the faces,
but I did not find another solution yet.
If you know about another better issue to resolve this problem, please let 
me know about it.



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



RE: [PHP] in the middle of shift and pop

2003-08-20 Thread Ford, Mike [LSS]
> -Original Message-
> From: Decapode Azur [mailto:[EMAIL PROTECTED]
> Sent: 20 August 2003 00:10
> 
> > unset($A[2]);
> 
> No this function does not exactly what I want.
> unset() makes empty hole with $A[2] = Null
> 
> I would like to have this result :
> Array
> (
> [0] => a
> [1] => b
> [2] => d
> [3] => e
> [4] => f
> )
> and not this one :
> Array
> (
> [0] => a
> [1] => b
> [3] => d
> [4] => e
> [5] => f
> )

I'm compelled to ask *why* you want that result.  In most cases it makes no
difference to your coding, so the only real reason I can see is a somewhat
obsessive desire for neatness!

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211

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



Re: [PHP] in the middle of shift and pop

2003-08-19 Thread Dan Anderson
If you unset an array that isn't associative, will that mean there will
be a gap in the numbers?  Or will PHP realize "OK I need to change the
numbers indexing the other elements"?

-Dan

On Tue, 2003-08-19 at 22:27, andu wrote:
> On Wed, 20 Aug 2003 00:25:32 +0200
> Decapode Azur <[EMAIL PROTECTED]> wrote:
> 
> > is it possible to remove an element of an indexed array such as this
> > exemple
> > $A = array('a', 'b', 'c', 'd', 'e', 'f');
> > in a way that we can optain this result :
> > $A = array('a', 'b', 'd', 'e', 'f');
> > 
> > something like that perhaps ?
> > array_remove($A, 2);
> > 
> > If such a function does not exists, what would be the more efficient
> > way to do so a lot of time on very big arrays ?
> 
> unset(array[x]);
> 
> > 
> > 
> > 
> > -- 
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> > 
> > 
> > 
> 
> 
> -- 
> Andu


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



Re: [PHP] in the middle of shift and pop

2003-08-19 Thread Dan Anderson
Well, if you knew (for instance) that $A[2] should be removed then you
could do something like:

 $value)
{
  if ($key < 2)
  {  $B[$key] = $value; }
  elseif ($key > 2)
  {  $B[($key - 1)] = $value; }
}
?>

Modify the above code as needed...

-Dan

On Tue, 2003-08-19 at 22:25, Decapode Azur wrote:
> is it possible to remove an element of an indexed array such as this exemple
>   $A = array('a', 'b', 'c', 'd', 'e', 'f');
> in a way that we can optain this result :
>   $A = array('a', 'b', 'd', 'e', 'f');
> 
> something like that perhaps ?
> array_remove($A, 2);
> 
> If such a function does not exists, what would be the more efficient way to 
> do so a lot of time on very big arrays ?
> 
> 


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



Re: [PHP] in the middle of shift and pop

2003-08-19 Thread David Otton
On Wed, 20 Aug 2003 00:25:32 +0200, you wrote:

>is it possible to remove an element of an indexed array such as this exemple
>   $A = array('a', 'b', 'c', 'd', 'e', 'f');
>in a way that we can optain this result :
>   $A = array('a', 'b', 'd', 'e', 'f');
>
>something like that perhaps ?
>array_remove($A, 2);
>
>If such a function does not exists, what would be the more efficient way to 
>do so a lot of time on very big arrays ?

In the specific case, I would unset() the nth element then reindex.

unset ($A[2]);
$A = array_values ($A);

As to speed... *shrug*. Suck it and see.

In the general case (removing n elements), either a loop of unset()s or a
couple of array_slice()s to get the two halves and array_merge() to stick
them together.


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



Re: [PHP] in the middle of shift and pop

2003-08-19 Thread Decapode Azur
> unset($A[2]);

No this function does not exactly what I want.
unset() makes empty hole with $A[2] = Null

I would like to have this result :
Array
(
[0] => a
[1] => b
[2] => d
[3] => e
[4] => f
)
and not this one :
Array
(
[0] => a
[1] => b
[3] => d
[4] => e
[5] => f
)


>> is it possible to remove an element of an indexed array such as this
>> exemple
>>  $A = array('a', 'b', 'c', 'd', 'e', 'f');
>> in a way that we can optain this result :
>>  $A = array('a', 'b', 'd', 'e', 'f');
>>
>> something like that perhaps ?
>> array_remove($A, 2);
>>
>> If such a function does not exists, what would be the more efficient way
>> to do so a lot of time on very big arrays ?



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



Re: [PHP] in the middle of shift and pop

2003-08-19 Thread Decapode Azur
> is it possible to remove an element of an indexed array such as this
> exemple $A = array('a', 'b', 'c', 'd', 'e', 'f');
> in a way that we can optain this result :
>   $A = array('a', 'b', 'd', 'e', 'f');
>
> something like that perhaps ?
> array_remove($A, 2);
>
> If such a function does not exists, what would be the more efficient way
> to do so a lot of time on very big arrays ?

perhaps this way :




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



RE: [PHP] in the middle of shift and pop

2003-08-19 Thread Ralph Guzman
unset($A[2]);

-Original Message-
From: Decapode Azur [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, August 19, 2003 3:26 PM
To: [EMAIL PROTECTED]
Subject: [PHP] in the middle of shift and pop

is it possible to remove an element of an indexed array such as this
exemple
$A = array('a', 'b', 'c', 'd', 'e', 'f');
in a way that we can optain this result :
$A = array('a', 'b', 'd', 'e', 'f');

something like that perhaps ?
array_remove($A, 2);

If such a function does not exists, what would be the more efficient way
to 
do so a lot of time on very big arrays ?



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




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



Re: [PHP] in the middle of shift and pop

2003-08-19 Thread andu
On Wed, 20 Aug 2003 00:25:32 +0200
Decapode Azur <[EMAIL PROTECTED]> wrote:

> is it possible to remove an element of an indexed array such as this
> exemple
>   $A = array('a', 'b', 'c', 'd', 'e', 'f');
> in a way that we can optain this result :
>   $A = array('a', 'b', 'd', 'e', 'f');
> 
> something like that perhaps ?
> array_remove($A, 2);
> 
> If such a function does not exists, what would be the more efficient
> way to do so a lot of time on very big arrays ?

unset(array[x]);

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


-- 
Andu

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



[PHP] in the middle of shift and pop

2003-08-19 Thread Decapode Azur
is it possible to remove an element of an indexed array such as this exemple
$A = array('a', 'b', 'c', 'd', 'e', 'f');
in a way that we can optain this result :
$A = array('a', 'b', 'd', 'e', 'f');

something like that perhaps ?
array_remove($A, 2);

If such a function does not exists, what would be the more efficient way to 
do so a lot of time on very big arrays ?



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