<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wojno: Tag extend</title>
    <link>http://christopher.wojno.com/articles/tag/extend</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Exploration through Code</description>
    <item>
      <title>Extending Ruby: The Sorted Array</title>
      <description>&lt;p&gt;I&amp;#8217;ve been working on my personal projects and I came across a problem with the ruby Array class. I have an array containing approximately 500 elements. I need to find each element approximately 500 times ( O(n^2) as the array search is linear ). As you can guess, it takes about 0.5 seconds for all these searches. I figured, if the linear time is 0.5, the binary search taking log_2(n) should take significantly less time to perform.&lt;/p&gt;


	&lt;h1&gt;Attempt #1: A ruby Sorted Array&lt;/h1&gt;


	&lt;p&gt;I first attempted to make a sorted array class in pure ruby. While creation of the class was easy, it was not as efficient as hoped.  As a matter of fact, according to my tests, the original ruby Array&amp;#8217;s find_index was twice as fast as mine on average. In fact, in order for my set to be as efficient as the original Array, the value being sought had to be over 4/5 the way into the array. Only after that point did my SortedArray overtake the Array in speed. Clearly, this was not the way to go.&lt;/p&gt;


	&lt;p&gt;After several attempts at re-working and simplifying the loop, I decided that the only reason the Array was so fast, was because it was written natively in C, not ruby.&lt;/p&gt;


	&lt;h1&gt;Attempt #2: A native C ruby Sorted Array&lt;/h1&gt;


	&lt;p&gt;Most of the guiding code to the Sorted Array is from the Pragmatic Ruby Programmer&amp;#8217;s Guide: Extending Ruby.&lt;/p&gt;


	&lt;h2&gt;The C-file&lt;/h2&gt;


	&lt;p&gt;First, we start with a new project tree, a-la &lt;span class="caps"&gt;SVN&lt;/span&gt; (this isn&amp;#8217;t too terribly important, but I prefer it). Next, we create the first file: sorted_array.c.&lt;/p&gt;


	&lt;p&gt;In it I included the required ruby file: ruby.h. Next, I went to the subversion repository for the Array code. I&amp;#8217;m using the 1.9.0.2 tag.&lt;/p&gt;


	&lt;p&gt;Take a good look at array.c. I&amp;#8217;s packed full of goodness. Unfortunately, I&amp;#8217;ll need to copy most of the code so that it supports all the functions we&amp;#8217;re used to with the standard Array (though, some cannot be used with this version, such as insert).&lt;/p&gt;


	&lt;p&gt;There, we now have a fully functional SortedArray that doesn&amp;#8217;t sort. It&amp;#8217;s a good start.&lt;/p&gt;


	&lt;h3&gt;Turning a normal Array into a SortedArray&lt;/h3&gt;


	&lt;p&gt;The standard Array and the SortedArray are essentially the same. The exception is that all insertions should be made in such a fashion as to preserve the ordering of the elements, that a special constructor be provided to accept un-natural sorting functions, and that searches can be optimized to take advantage of the knowledge of the arrangement of data.&lt;/p&gt;


	&lt;p&gt;The c-version of the Array has 3 major components in the data structure: length (len), ptr (the start of the array in memory), and aux. Aux has lots of things that support the array, such as the capacity. Keep these in mind when mucking with the internals.&lt;/p&gt;


	&lt;h3&gt;Remap the names&lt;/h3&gt;


	&lt;p&gt;We can&amp;#8217;t just use the code as is. Let&amp;#8217;s rename all the functions from &amp;#8220;rb_ary.&lt;pre style="display:inline;"&gt;*&lt;/pre&gt;&amp;#8221; to &amp;#8220;rb_sorted_ary_.&lt;pre style="display:inline;"&gt;*&lt;/pre&gt;&amp;#8221;. With vim, I do: :%s/rb_ary_/rb_sorted_ary_/g. Poof, all renamed.&lt;/p&gt;


	&lt;h3&gt;Enter the Constructors&lt;/h3&gt;


	&lt;p&gt;One of the constructors takes a variable argument list as the 2nd and above parameters. We need to correct this as it simply inserts values. We need to push each individual element. Yes, this will slow down the construction of these types of arrays, but again, we&amp;#8217;re banking on searching being the primary operation, not construction.&lt;/p&gt;


	&lt;p&gt;My version of array.c (1.9.0.2) looks like this:&lt;/p&gt;


&lt;pre class="code"&gt;VALUE
rb_ary_new3(long n, ...)
{
    va_list ar;
    VALUE ary;
    long i;

    ary = rb_ary_new2(n);

    va_start(ar, n);
    for (i=0; i&amp;lt;n; i++) {
    RARRAY_PTR(ary)[i] = va_arg(ar, VALUE);
    }
    va_end(ar);

    RARRAY(ary)-&amp;gt;len = n;
    return ary;
}&lt;/pre&gt;

	&lt;p&gt;It needs a little bit of changing. Let&amp;#8217;s change: &lt;span class="caps"&gt;RARRAY&lt;/span&gt;_PTR(ary) line to: &amp;#8220;rb_sorted_ary_push(ary,va_arg(ar,VALUE));&amp;#8221; That should fix it&amp;#8217;s wagon. It&amp;#8217;s a variable array, so we need to use the c-library variable argument accessors: va_arg and cast it to Ruby&amp;#8217;s &lt;span class="caps"&gt;VALUE&lt;/span&gt; type. After that, we just push it to the array, one at a time.&lt;/p&gt;


	&lt;p&gt;As you may have guessed, I&amp;#8217;ve decided to make the primary changes to push and delegating all insertion to that method. We&amp;#8217;ll get to this later, however.&lt;/p&gt;


	&lt;p&gt;Next, we tackle the new4 function. This is the &amp;#8220;copy-constructor&amp;#8221; if you&amp;#8217;re used to C++. Essentially, it is expecting an array passed in as the second parameter (&amp;#8220;elts&amp;#8221; in my version). The C-code makes a straight copy form a C-array (not a ruby Array!). However, this is not possible if the incoming array is not sorted. We need to insert them one at a time, inserting each. Originally, it looks like this:&lt;/p&gt;


&lt;pre class="code"&gt;VALUE
rb_sorted_ary_new4(long n, const VALUE *elts)
{
    VALUE ary;

    ary = rb_sorted_ary_new2(n);
    if (n &amp;gt; 0 &amp;#38;&amp;#38; elts) {
    MEMCPY(RARRAY_PTR(ary), elts, VALUE, n);
    RARRAY(ary)-&amp;gt;len = n;
    }

    return ary;
}&lt;/pre&gt;

	&lt;p&gt;The if statement is assuring that the incoming array is not empty or null (nil). That&amp;#8217;s a good check and we&amp;#8217;ll keep it. But we need to augment it. We know how long the array is, so let&amp;#8217;s just iterate through each and push the values onto the array. I threw away the &lt;span class="caps"&gt;MEMCPY&lt;/span&gt; and replaced it with:&lt;/p&gt;


&lt;pre class="code"&gt;
for(long i = 0; i &amp;lt; n; ++i) {
  rb_sorted_ary_push( ary, elts[i] );
}&lt;/pre&gt;

	&lt;p&gt;That does it for the constructors (for now, we still need to add a way to provide an unnatural sorting Proc call, but we&amp;#8217;ll come back to this later after a bit of testing).&lt;/p&gt;


	&lt;h3&gt;ary_make_* and ary_.* functions&lt;/h3&gt;


	&lt;p&gt;There are two functions that do not adhere to the rb_ary_.* syntax: ary_make_shared and ary_make_hash. Stick a &amp;#8220;sorted_&amp;#8221; in front of those definitions and in every place they are used.&lt;/p&gt;


	&lt;p&gt;Do the same with ary_.* functions (such as ary_new and ary_alloc and ary_shared_first). You can do this by simply using vim to search for &amp;#8221;\&amp;lt;ary_&amp;#8221; and it will locate any ary_ that has a space or nothing before it (starts a &amp;#8220;word&amp;#8221;).&lt;/p&gt;


	&lt;h3&gt;to_ary&lt;/h3&gt;


	&lt;p&gt;Keep them as is. I see no need to convert to a sorted array, as we may need to add a parameter to take in a custom sort function (which we&amp;#8217;re coming back to later).&lt;/p&gt;


	&lt;h3&gt;Initialize&lt;/h3&gt;


	&lt;p&gt;We now hit the initialize function. It has 4 forms:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Array.new(size=0,obj=nil)&lt;/li&gt;
		&lt;li&gt;Array.new(array)&lt;/li&gt;
		&lt;li&gt;Array.new(size) {|index| block}&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;The first is easy: create an array of size, size, and then fill it with objects given in obj. This version requires no sorted, as all objects will be the same.  The second version requires a bit of changing. We need to push each value of the incoming array one at a time to ensure consistency. Jump down to rb_sorted_ary_replace.&lt;/p&gt;


	&lt;h4&gt;The Replacement Detour: before fixing initialize&lt;/h4&gt;


&lt;pre class="code"&gt;VALUE
rb_sorted_ary_replace(VALUE copy, VALUE orig)
{
    VALUE shared;
    VALUE *ptr;

    orig = to_ary(orig);
    rb_sorted_ary_modify_check(copy);
    if (copy == orig) return copy;
    shared = sorted_ary_make_shared(orig);
    if (!ARY_SHARED_P(copy)) {
    ptr = RARRAY(copy)-&amp;gt;ptr;
    xfree(ptr);
    }
    RARRAY(copy)-&amp;gt;ptr = RARRAY(orig)-&amp;gt;ptr;
    RARRAY(copy)-&amp;gt;len = RARRAY(orig)-&amp;gt;len;
    RARRAY(copy)-&amp;gt;aux.shared = shared;
    FL_SET(copy, ELTS_SHARED);

    return copy;
}&lt;/pre&gt;

	&lt;p&gt;They simply make a copy of the array&amp;#8217;s pointer and declare the source array to be shared. Since we&amp;#8217;re not sharing the array, there&amp;#8217;s no need to do this anymore, yank out the make_shared line, the line at sets the aux.shared to shared, and the FL_SET. Next, we&amp;#8217;ll yank out the pointer and length copying. Since we&amp;#8217;re replacing our contents, we need to drop the old contents. Simply drop that if-statement around the xfree(ptr); line, and that will be taken care of. We&amp;#8217;ll convert it to a for-loop, iterating through the original, don&amp;#8217;t forget to use Ruby&amp;#8217;s handy macros! To save some work, I&amp;#8217;ll re-use the existing &amp;#8220;constructor:&amp;#8221;&lt;/p&gt;


&lt;pre class="code"&gt;VALUE
rb_sorted_ary_replace(VALUE copy, VALUE orig)
{
    VALUE shared;
    VALUE *ptr;

    orig = to_ary(orig);
    rb_sorted_ary_modify_check(copy);
    if (copy == orig) return copy;
        ptr = RARRAY(copy)-&amp;gt;ptr;
        xfree(ptr);
        // create a new array
        long len = RARRAY_LEN(orig);
        if( len == 0 ) len++;
        RARRAY(copy)-&amp;gt;ptr = ALLOC_N(VALUE, len);
        RARRAY(copy)-&amp;gt;len = 0;
        RARRAY(copy)-&amp;gt;aux.capa = len;
        for( int i = 0; i &amp;lt; RARRAY_LEN(orig); ++i ) {
            rb_sorted_ary_push(copy,RARRAY(orig)-&amp;gt;ptr[i]);
        }

    return copy;
}&lt;/pre&gt;

	&lt;p&gt;OK, this is probably the most confusing part so far. Why did we throw away the shared? Ruby cheats because instead of making a true, deep copy of the array it&amp;#8217;s making a copy of, it declares the contents of the array as &amp;#8220;shared&amp;#8221; and simply copies pointers. We can&amp;#8217;t do that as we need to re-arrange our data. So that&amp;#8217;s why I yanked all the code related to sharing. So now, each object remains unlinked as far as the garbage collector is concerned. Yes, this does make things slower, and yes, does double the memory usage. But we can&amp;#8217;t modify the incoming array as it may not be intended to be sorted. So we&amp;#8217;re stuck. Because we&amp;#8217;re doing a replacement, it&amp;#8217;s best to just start from scratch. We do this by freeing up the c-array pointer, using xfree. Next, we get the length of the original array. We then use this to size-up our copy. Why do I add 1 in the case that the length is zero? Because, allocating an array of size 0 will cause C to throw a fit. So, we avoid that mess entirely by creating a starting array with 1 place to store things, but it is empty. We create the appropriately sized, internal c-array. We then set the size of the array to zero to spoof a blank array. We then set the capacity, to tell the Array that it need not resize itself when pushing the next set of items, one at a time.&lt;/p&gt;


	&lt;p&gt;When all items are added, the array will have the correct length (no longer zero, if the original wasn&amp;#8217;t empty).&lt;/p&gt;


	&lt;h4&gt;Back to Initialize to finish the last case&lt;/h4&gt;


	&lt;p&gt;Back to the &amp;#8220;initializer,&amp;#8221; we now need to take care of the strange case that uses the block to initialize the array, based on the index. We cannot assume that the values created by this block will always be in order. So, after each generation (rb_yield), we need to push them onto the array as usual.  The line looks like: &amp;#8220;rb_sorted_array_store(ary, i, rb_yeild(&lt;acronym title="i"&gt;LONG2NUM&lt;/acronym&gt;))&amp;#8221;. Simply yank that line, and the line incrementing the length (as the length will be modified by the push function). Replace those lines with: &amp;#8220;rb_ary_push(ary, rb_yield(&lt;acronym title="i"&gt;LONG2NUM&lt;/acronym&gt;));&amp;#8221; That should fix initialize.&lt;/p&gt;


	&lt;p&gt;Next, the rb_sorted_ary_s_create needs to be changed to, you guessed it, use push. Drop the length and the &lt;span class="caps"&gt;MEMCPY&lt;/span&gt; and insert the for loop, with the push call:&lt;/p&gt;


&lt;pre class="code"&gt;static VALUE
rb_sorted_ary_s_create(int argc, VALUE *argv, VALUE klass)
{
    VALUE ary = sorted_ary_alloc(klass);

    if (argc &amp;lt; 0) {
    rb_raise(rb_eArgError, "negative array size");
    }
    RARRAY(ary)-&amp;gt;ptr = ALLOC_N(VALUE, argc);
    RARRAY(ary)-&amp;gt;aux.capa = argc;
        for( int i = 0; i &amp;lt; argc; ++i ) {
            rb_sorted_ary_push(ary, argv[i] );
        }

    return ary;
}&lt;/pre&gt;

	&lt;h3&gt;Guessing an index&lt;/h3&gt;


	&lt;p&gt;Next, we need to fix up push to stop simply appending. But before that, we need to create a special function to guess an index. Why guess? Because the value may not yet exist, but if it did, this is the index where it would be located in the array. This will be the basic function for both searching, deleting, and pushing.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ll call my new function: rb_sorted_ary_guess_index(VALUE ary, &lt;span class="caps"&gt;VALUE&lt;/span&gt; item). Not very creative, but very descriptive. Here&amp;#8217;s the problem though: we want to be able to use both natural, and unnatural sorting algorithms. And, either is fine, so long as the ordering is monotonic (goes one-way and is predictable). Before we get into that, let&amp;#8217;s just assume we&amp;#8217;re using natural ordering, for simplicity. I wrote the following code:&lt;/p&gt;


&lt;pre class="code"&gt;
&lt;/pre&gt;

	&lt;p&gt;Head up to sorted_array_alloc. Hmmm&amp;#8230;. We need to add something to our arrays! We need to store the optional sort-by block!&lt;/p&gt;


	&lt;h3&gt;The Sort-by block&lt;/h3&gt;


	&lt;p&gt;Create a new file called &amp;#8220;sorted_array.h&amp;#8221;. Create the data structure:&lt;/p&gt;


&lt;pre class="code"&gt;#include "ruby.h" 

struct RSArray {
    struct RBasic basic;
    long len;
    union {
        long capa;
        VALUE shared;
    } aux;
    VALUE *ptr;
    ID cmp;
};&lt;/pre&gt;

	&lt;p&gt;This is an exact duplicate of the RArray in ruby.h. The only exception is the additional ID *cmp, our comparison function. Be sure you&amp;#8217;ve included this structure file in the sorted_array.c file.&lt;/p&gt;


	&lt;p&gt;Back in the sorted_array.c file, make sure you cap the new cmp variable with a &lt;span class="caps"&gt;NULL&lt;/span&gt; in sorted_ary_alloc: ary-&amp;gt;cmp = &lt;span class="caps"&gt;NULL&lt;/span&gt;; Just put it under the aux.capa assignment. Now, we need to &amp;#8220;fix&amp;#8221; the sort_2 function to fall-back to the default comparison function, but to prefer a specified function if given. Let&amp;#8217;s create a class-specific sort function called: sorted_array_sort_3.&lt;/p&gt;


&lt;pre class="code"&gt;&lt;/pre&gt;</description>
      <pubDate>Mon, 23 Jun 2008 13:14:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:efb5c2bc-5ad2-4cf9-91c7-62dc61dc924b</guid>
      <author>Christopher Wojno</author>
      <link>http://christopher.wojno.com/articles/2008/06/23/extending-ruby-the-sorted-array</link>
      <category>Ruby Snippets</category>
      <category>ruby</category>
      <category>array</category>
      <category>c</category>
      <category>sorted</category>
      <category>extending</category>
      <category>extend</category>
      <category>native</category>
      <category>efficient</category>
      <category>lookup</category>
    </item>
  </channel>
</rss>
