Friday, May 4, 2012

DelayedSortedListStore: Gtk.ListStore // An Optimization


In-place replacement, for display lists, that elapses its updates by the timer function. I wondered about another version besides the previous one under the Gnu Public License. I hated my teacher sound to an audience with anonmity by the version I trashed. C/C++ project managers wanted the contract styled headers that does not appear in C#, so they avoid it because it puts them out of their contracted career. You can only imagine why they moved optimal code out of the headers and what that delays.

C# lets us model by interfaces to objects. We have interfaces as alternatives to inheritence. Often, we started with the objects that are then refactored into interfaces. Here is one start.

class DelayedSortedListStore : Gtk.ListStore
        {
        const int         unsorted  = -2 ;
        const int         second    = 1000 ;
        Timer             timer     = new Timer( second ) ;
        int               sortable  = unsorted ;
        public Action     Elapsed   = () => {} ;

        public int Sortable
                {
                set { sortable = value ; DelaySort( false ) ; }
                get { return sortable ; }
                }

        public void DelaySort( bool setDelay )
                {
                int x ;
                Gtk.SortType y ;
                if( setDelay && ! timer.Enabled )
                        {
                        Gtk.Application.Invoke( delegate
                                {
                                GetSortColumnId( out x, out y ) ;
                                SetSortColumnId( unsorted, y ) ;
                                }) ;
                        timer.Enabled = true ;
                        }
                else
                if( ! setDelay )
                        {
                        timer.Enabled = false ;
                        Gtk.Application.Invoke( delegate
                                {
                                GetSortColumnId( out x, out y ) ;
                                SetSortColumnId( sortable, y ) ;
                                }) ;
                        }
                else
                        {
                        timer.Interval = second ;
                        }
                }
             
        void elapsed( object o, System.EventArgs a )
                {
                DelaySort( false ) ;
                Elapsed() ;
                }

        public DelayedSortedListStore( params Type[] types ) : base( types )
                {
                timer.Elapsed += elapsed ;
                SetSortColumnId( unsorted, Gtk.SortType.Ascending ) ;
                }
        }


Its member Sortable is significant, so I put near the top. You know where refactorization starts. Next change involves some rewrite, so that it does not require GTK.

I used lowercase-names for private members, and I noticed javaProgrammers did not use such technique. You know how the visual languages started.