1

i wrote an array wrapper class PersonArray which can contain objects of a certain type (Person). Every person has a unique getHash() function which returns the ID + Name as a unique identifier. This allows for speedy retrieval of the Person from the PersonArray. The PersonArray actually holds two internal Arrays. One for the storage of Person objects ($items), and one for the storage of the Hash values ($itemsHash).

I want to create a insertAt(index, Person) function which puts the Person object at the [index] position in the $items array. Is there a way to insertAt a certain position in an array? If so how can I also update the $itemsHash of the PersonArray?

class Person {
    function getHash() {
        return $this->id . $this->name;
    }
}

class PersonArray implements Iterator {
    public $items = array();
    public $itemsHash = array();

    public function Find($pKey) {
        if($this->ContainsKey($pKey)) {
            return $this->Item($this->internalRegisteredHashList[$pKey]);
        }
    }

    public function Add($object) {
        if($object->getHash()) {
            $this->internalRegisteredHashList[$object->getHash()] = $this->Count();
            array_push($this->items, $object);
        }
    }
    public function getItems() {
        return $this->items;
    }

    function ContainsKey($pKey) {}

    function Count() {}

    function Item($pKey) {}

    //Iteration implementation
    public function rewind() {}
    public function current() {}
    public function key() {}
    public function next() {}
    public function valid() {}
}
1
  • Questions: I do not fully grasp your situation. Is this class complete? What is internalRegisteredHashList? Why can't you just keep them indexed by their hashes and skip the $items alltogether? How does the class look that actually handles the iteration? Afaik Iterator is just an interface, right? Could you show some sample code of how you'd like to use this class and the insertAt-function? (Detail, and why does some functions start with uppercase and some don't?) Commented Jun 8, 2009 at 16:14

1 Answer 1

1

You may find it is faster and easier to use PHP's associative arrays rather than re-implementing them.

As an aside you can also implement the simpler IteratorAggregate if you are actually just iterating over an array.

e.g.

class PersonArray implements IteratorAggregate {
    public $items = array();

    public function getItems() {
        return $this->items;
    }

    public function Add($object) {
        if($object->getHash()) {
            $this->items[$object->getHash()] = $object;
        }
    }

    public function Find($pKey) {
        if(isset($this->items[$pKey])) {
            return $this->items[$pKey];
        }
    }

    public function insertAt($index, $person) {
        $tmp = array_slice($this->items, 0, $index);
        $tmp[$person->getHash()] = $person;
        $tmp = array_merge($tmp, array_slice($this->items, $index));

        $this->items = $tmp;
    }

    //IteratorAggregate implementation
    public function getIterator() {
        return new ArrayIterator($this->items);   
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

But how about performance? Isn't foreach() over an associative-array slower then looping over an indexed-array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.