16 December 2009

PHP array_insert

I had the need the other day to create an ordered collection object. I wanted to be able to add elements to the end of the collection as well as at any point in the middle of the collection. Since I was using PHP as the language to create this object, an array was the obvious construct to use as the internal implementation for this collection. Adding elements to the end of an array is trivial, but inserting elements into the middle of the array turned out to be a bit more difficult.

My first thought was that PHP has all kinds of array functions so there must be an array_insert function. This assumption turned out to be wrong. My second thought is that I would have to cut the array into two pieces, insert the new element to the end of the first piece and then glue to two arrays back together. In fact, I coded this before reading the documentation for the array_splice function more closely.

The array_splice function has two optional parameters which are very helpful in creating an array_insert function. The first optional parameter is the length option. This option specifies how many elements to remove from the original array starting at the specified offset. The second optional parameter is the replacement option. This parameter specified what to replace the removed elements with. So, in other words, you can tell array_splice to remove 6 elements from an array starting at offset 2 and to replace the 6 elements with what ever is in the replacement array.

This is perfect for mimicking an array_insert function. If you specify to remove 0 elements and pass in the new element as an array of 1 item, it gets inserted at the specified index. Now for the example:
function array_insert($array, $index, $new_element)
{
    return array_splice($array, $index, 0, array($new_element));
}
There you have it, array_insert nice and clean and optimized in c.

13 September 2009

Foundation Series


Right before Candace went to the hospital to deliver Genevieve she was trying to find a good book to read. A good friend suggested to Candace that she read the Foundation Series by Isaac Asimov. A few day later I picked it up, as it was sitting near by, and started reading it. Each Chapter was only a few pages long. This is a very positive quality in a book for me. I usually only have 5-10 minutes at a time to read, so a short chapter lends itself to my reading habits. As the series went on the chapters became longer and longer which meant that as I moved through the series it started to take longer and longer to finish each book. I read only the original three books in the series. There are three more that were written at a later time, but I think it is time for me to move on to something else.

I found the series to be quite intriguing. Much of the plot was based on philosophical misdirection. This meant that each new chapter brought new information as well as new unknowns. This kept the plot alive in my mind and kept me thinking about what might happen next. I found this to be both good and bad. It was good because it made the story interesting. It was bad because I was focused on the plot and not of the messages and philosophy presented by the author.

Generally, when I read I am not worried about the story's plot. I enjoy pondering the information presented by the author much more. Reading this series, where both were present, created a real conflict as I read. In the end the plot was too much of a distraction and so I just quickly finished the book to feed my desire to know what was going to happen next. Thus I probably missed a lot of interesting information which was weaved into the story.

Because of the way the first book was constructed I had more time to think about what was being discussed in the book. I really enjoyed thinking about Asimov's ideas about the interaction between education, technology, religion, science, societal advancement and stagnation. It was interesting to think about where our current society stands in comparison to what he was describing.

I highly suggest the foundation series on many levels. The science fiction was great. The depth of the story was wonderful. The writing was clean and fun to read.