GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


_Array.hh

    
#ifndef ALREADY_INCLUDED_2006_LIBD__ARRAY_HH
#define ALREADY_INCLUDED_2006_LIBD__ARRAY_HH

#include "_Single.hh"

namespace dmp
{
   ///
   /// ptr<T> has to make sense, therefore T must subclass Ref_Count
   ///
   template<class X>
   class Array : private Single
   {
      //public:
      //virtual bool            rtti_is_Array_Integer() { return true; }
      //virtual Array<Integer>* rtti_get_Array_Integer() { return this; }

      //POOL1;
   private:
      ///
      /// NOTE: property private_array is initialised by calloc/free
      /// rather than new[]/delete[] so it doesn't show up in the memory
      /// tracker...
      ///
      ptr<X>* private_array;
      int     private_length;
      int     private_capacity;

private:
      Array(int nominal_length = 10) : Single(DCODE_ARRAY)
      {
         private_length   = 0;
         private_capacity = nominal_length;
         private_array    = reinterpret_cast<ptr<X>*>(calloc(sizeof(ptr<X>), nominal_length));
      }

public:
      static ptr<Array> ctor(int nominal_length = 10)
      {
         return new Array(nominal_length);
      }

private:
      ///
      /// NOTE: opens up ~Array() to ptr class
      ///
      template <class T> friend class ptr;
      ~Array()
      {
#ifdef ALLEGRO_ONLINE
         if (!allegro_on)
         {
            return;
         }
#endif /* ALLEGRO_ONLINE */
         if (this == null)
         {
            return;
         }
         if (private_array != null)
         {
            for (int i=0; i<private_length; i++)
            {
               ///
               /// This is really cool!!!
               ///
               /// Only call the destructor on those elements who were ctor'ed...
               ///
               *(private_array + i) = null;
               //(*(private_array + i))->dtor();
            }
            free(private_array);
            private_array    = null;
            private_length   = 0;
            private_capacity = 0;
         }
         else
         {
            assert(private_length   == 0);
            assert(private_capacity == 0);
         }
      }



private:
      ///
      /// NOTE: disables passing and returning by value
      ///
      Array(const Array& a);
      Array& operator = (const Array&);



public:
      int get_length() const
      {
         ASSERT(this != null);
         return private_length;
      }

public:
      /// Analogues of
      //const X* operator [] (int i) const
      //X* operator [] (int i)
      //
      ///
      /// This is not const: Compare with design pattern for const in list.hh
      ///
      ptr<X> get_element_at(int i) const
      {
         ASSERT(this != null);
         ASSERT_INFO(i >= 0, dmp::string() + "i=" + i + ", private_length=" + private_length);
         ASSERT_INFO(i < private_length, dmp::string() + "i=" + i + ", private_length=" + private_length);
         return private_array[i];
      }
      void set_element_at(int i, const ptr<X>& element)
      {
         ASSERT(&element != null);
         set_element_at(i, element.operator->());
      }
      void set_element_at(int i, X* element)
      {
         ASSERT(this != null);
         ASSERT(i >= 0);
         ASSERT(i < private_length);
         private_array[i] = element;
      }

ptr<Array<X> > add_element(const ptr<X>& new_element)
      {
         ASSERT(&new_element != null);
         add_element(new_element.get_ptr());
         return this;
      }
      ptr<Array<X> > add_element(X* new_element)
      {
         ASSERT(this != null);
         if (private_length >= private_capacity)
         {
            bool was_zero = (0 == private_capacity);
            if (was_zero)
            {
               private_capacity = 10;
            }
            else
            {
               private_capacity *= 2;
            }
            //dmp::cout << "*** extending to " << private_capacity << '\n';
            ptr<X>* new_array = reinterpret_cast<ptr<X>*>(calloc(sizeof(ptr<X>), private_capacity));
            //ptr<X>* new_array = reinterpret_cast<ptr<X>*>(1000);
            for (int i=0; i<private_length; i++)
            {
               new_array[i] = private_array[i];
               ///
               /// NOTE: This is really cool!!!
               ///
               (private_array + i)->~ptr();
            }
            if (!was_zero)
            {
               free(private_array);
            }
            private_array = new_array;
         }
         ASSERT(private_length < private_capacity);
         private_array[private_length] = new_element;
         private_length++;
         return this;
      }



void delete_element_at(int i)
      {
         cascade_down(i);
      }

void add_element_at(int i, const ptr<X>& element)
      {
         ASSERT(&element != null);
         add_element_at(i, element.operator->());
      }
      void add_element_at(int i, X* element)
      {
         cascade_up(i);
         private_array[i] = element;
      }

private:
      ///
      /// Inefficient O(n) methods for adding
      /// or deleting from an array at a
      /// given index.
      ///
      void cascade_down(int index)
      {
         for (int i=index; i<private_length-1; i++)
         {
            private_array[i] = private_array[i+1];
         }
         private_length--;
      }
      void cascade_up(int index)
      {
         add_element(null);
         for (int i=private_length-1; i>index; i--)
         {
            private_array[i] = private_array[i-1];
         }
         private_array[index] = null;
      }

#ifdef DAVINS_IO_ONLINE

friend dmp::Writer& operator << (dmp::Writer& w, const Array<X>& a)
      {
         ASSERT(&w != null);
         ASSERT(&a != null);
         // if (&a == null)// || (a == null))
         // {
         //    w << "null";
         // }
         // else
         //{
         w << '(' << "Array";
         for (int i=0; i < a.get_length(); i++)
         {
            w << ' ' << a.get_element_at(i);
         }
         w << ')';
         //}
         return w;
      }

friend dmp::Reader& operator >> (dmp::Reader& r, Array<X>& a)
      {
         //CHECKPOINT;
         ASSERT(&r != null);
         ASSERT(&a != null);
         if (r.looking_at("null"))
         {
            r.next_token(); // more efficient than r >> "null";
         }
         else
         {
            //CHECKPOINT;
            ASSERT(a.get_length() == 0);
            //r.skip('(');
            //r.skip("Array");
            r >> '(' >> "Array";
            while (!r.looking_at(')'))
            {
               ptr<X> t = X::ctor();
               //r >> *(t.get_ptr());
               r >> t;
               a.add_element(t);
               //CHECKPOINT;
            }
            //r.skip(')');
            r >> ')';
            //CHECKPOINT;
         }
         return r;
      }
#endif /* DAVINS_IO_ONLINE */

public:
      int find_element_eq(int start, ptr<X> key)
      {
         const int len = private_length;
         for (int i=start; i<len; i++)
         {
            if (private_array[i] == key)
            {
               return i;
            }
         }
         return -1;
      }

int find_element_equal(int start, ptr<X> key)
      {
         const int LEN = private_length;
         for (int i=start; i<LEN; i++)
         {
            if (*(private_array[i]) == *key)
            {
               return i;
            }
         }
         return -1;
      }

ptr<Array<X> > clone_deep()
      {
         const int LEN = private_length;
         ptr<Array<X> > a = Array<X>::ctor(LEN);
         for (int i=0; i<LEN; i++)
         {
            a->add_element(get_element_at(i)->clone_deep());
         }
         return a;
      }

ptr<X> get_cyclic_at(int index)
      {
         const int LEN = this->get_length();
         ASSERT(LEN != 0);
         while (index < 0)
         {
            index += LEN;
         }
         while (index >= LEN)
         {
            index -= LEN;
         }
         return this->get_element_at(index);
      }

friend bool operator == (const Array<X>& array1, const Array<X>& array2)
      {
         if (array1.get_length() != array2.get_length())
         {
            return false;
         }
         const int LEN = array1.get_length();
         for (int i=0; i<LEN; i++)
         {
            ptr<X> p1 = array1.get_element_at(i);
            ptr<X> p2 = array2.get_element_at(i);
            if (*p1 != *p2)
            {
               return false;
            }
         }
         return true;
      }

friend bool operator != (const Array<X>& array1, const Array<X>& array2)
      {
         return !(array1 == array2);
      }
      // end of class
   };
}

#endif /* ALREADY_INCLUDED_2006_LIBD__ARRAY_HH */
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sat Oct 29 20:43:20 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to