[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/artest.h objects.cpp onlyobj.h
Chris Lattner
lattner at cs.uiuc.edu
Mon Oct 4 13:01:30 PDT 2004
Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects:
artest.h added (r1.1)
objects.cpp added (r1.1)
onlyobj.h added (r1.1)
---
Log message:
Initial checkin of all of the source
---
Diffs of the changes: (+480 -0)
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/artest.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/artest.h:1.1
*** /dev/null Mon Oct 4 15:01:24 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/artest.h Mon Oct 4 15:01:14 2004
***************
*** 0 ****
--- 1,159 ----
+ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ // Written by Ashok Sreenivasan, TRDDC, Pune, India. 1993. May be
+ // distributed freely, provided this comment is displayed at the top.
+ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ #ifndef __TBL__
+
+
+
+ #ifndef TRUE
+ #define TRUE (1)
+ #define FALSE (0)
+ #endif
+
+ enum TblType
+ {
+ PERS, // For "persistent" tables
+ VOLAT, // For "volatile" tables
+ };
+
+ class Table : public Object
+ {
+ public:
+
+ // Constructor
+
+ Table (enum TblType t = PERS) : type (t) { Init (); }
+
+ // Insertion functions
+
+ virtual int Insert (Object *, int pos=0) = 0;
+ virtual int Append (Object *p) { return Insert (p, Nelem ()); }
+ Table &operator << (Object *p) { Append (p); return *this; }
+
+ // Overwrite (assign) function
+
+ virtual int Assign (Object *o, int pos = 0)
+ { Fetch (pos); return Insert (o, pos); }
+
+ // Fetch functions
+
+ virtual Object *Get (int pos) = 0;
+ Object *Cur () { return Get (CurInd ()); }
+ Object *First () { return Get (0); }
+ Object *Next () { return Get (CurInd()+1); }
+ Object *Prev () { return Get (CurInd()-1); }
+ Object *Last () { return Get (Nelem()-1); }
+ Object *operator [] (int pos) { return Get (pos); }
+
+ // Removal functions
+ // Remove functions delete the object, while Fetch functions get the object
+ // from the table, and the table itself "forgets" the object, but it is not
+ // destroyed.
+
+ virtual Object *Fetch (int pos = 0) = 0;
+ virtual int Remove (int pos=0)
+ { Object *o = Fetch (pos); delete o; return o == 0 ? 0 : 1; }
+
+ // Search function
+
+ virtual int Search (Object &) ;
+
+ // Enquiry functions
+
+ virtual int CurInd () { return curind; }
+ virtual int Nelem () { return nelem; }
+ char *Type () { return "Table"; }
+ enum TblType TblType () { return type; }
+
+ // Equality Operator for tables
+
+ int operator == (Object &o) ;
+
+ protected:
+
+ int nelem, curind;
+ enum TblType type;
+ void Init () { nelem = 0; curind = -1; }
+ };
+
+ class Array : public Table
+ {
+ public:
+
+ // Constructor
+
+ Array (int sz=100, enum TblType t = PERS);
+
+ // Destructor
+
+ ~Array ();
+
+ // Insertion functions
+
+ int Append (Object *p);
+ int Insert (Object *, int pos=0);
+
+ // Overwrite (assign) function
+
+ virtual int Assign (Object *o, int pos = 0);
+
+ // Fetch functions
+
+ Object *Get (int pos) ;
+ Object *Fetch (int pos=0);
+
+ // Enquiry function
+
+ int Size () { return size; }
+ char *Type () { return "Array"; }
+
+ protected:
+
+ Object **array;
+ int size;
+ };
+
+ class SpArray : public Array
+ {
+ public:
+
+ // Constructor
+
+ SpArray (int sz=100, enum TblType t = PERS) : Array (sz, t) {}
+
+ // Destructor - use array destructor.
+
+ ~SpArray ();
+
+ // Overwrite (assign) function
+
+ virtual int Assign (Object *o, int pos = 0);
+
+ // Fetch functions
+
+ Object *Get (int=0) ;
+ Object *Fetch (int=0);
+
+ // Search function
+
+ int Search (Object &) ;
+
+ // Enquiry function
+
+ char *Type () { return "SpArray"; }
+ int IsEmptySlot (int pos) { return ((Get (pos) == 0) ? 1 : 0); }
+
+ private :
+
+ // Insertion functions dont make sense for a sparse array - hence hidden away
+
+ int Append (Object *) { return 0; }
+ int Insert (Object *, int=0) { return 0; }
+ };
+
+
+
+ #define __TBL__
+ #endif
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/objects.cpp
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/objects.cpp:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/objects.cpp Mon Oct 4 15:01:14 2004
***************
*** 0 ****
--- 1,280 ----
+ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ // Written by Ashok Sreenivasan, TRDDC, Pune, India. 1993. May be
+ // distributed freely, provided this comment is displayed at the top.
+ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ #include "onlyobj.h"
+ int strlen(char *str1) {return 0;}
+ int strcmp(char *str1, char *str2) {return 0;}
+ char *strcpy(char *str1, char *str2) {return "";}
+ char *strncpy(char *str1, char *str2, int i) {return "";}
+ char *strcat(char *str1, char *str2) {return "";}
+
+ #include "artest.h"
+
+ #define abs(x) ((x) > 0 ? (x) : (-x))
+
+ // The search function for a table searches for the element linearly and
+ // returns the index of the element in the list. If not found, a -1 is
+ // returned. The comparison of objects is done through the '==' operator.
+
+ int Table::Search (Object &obj)
+ {
+ for (int i = 0; i < nelem; i++)
+ if (obj == * (Get(i)))
+ return i;
+ return -1;
+ }
+
+ // The table == operator is used to check for equality of tables themselves!
+ // This operator checks first if the types of the two tables are the same,
+ // then if the no. of elements are the same, and finally if each and every
+ // element is the same. Only then are the tables deemed equal. Note that
+ // if the tables are large, this could be an expensive operation.
+
+ int Table::operator == (Object &tblobj)
+ {
+ char *a;
+ if (strcmp (a=tblobj.Type (), Type ())) // Type check
+ return 0;
+ Table *tblptr = (Table *) &tblobj;
+ if (tblptr->Nelem () == nelem) // Count check
+ {
+ Object *b;
+ for (int i = 0; i < nelem; i++)
+ if (*(b=tblptr->Get (i)) != *(Get(i))) // Elementwise check
+ return 0;
+ }
+ else
+ return 0;
+ return 1;
+ }
+
+ // The array constructor allocates the reqd space and initialises the elems
+ // explicitly to NULL.
+
+ Array::Array (int sz, enum TblType t) : Table (t)
+ {
+ array = new Objp [sz];
+ size = sz;
+ for (int i = 0; i < sz; i++) // Explicitly NULLify
+ array [i] = 0;
+ }
+
+ // The array destructor - deletes the elements from the rightmost to leftmost
+ // first. Once the elements are rid, the array pointer itself is deleted.
+
+ Array::~Array ()
+ {
+ if (type == VOLAT)
+ {
+ for (int i = nelem - 1; i >= 0; i--)
+ Remove (i);
+ }
+
+ if (array)
+ {
+ delete array;
+ array = 0;
+ }
+ Init ();
+ }
+
+ // The array append function increments nelem and sets curind and adds the
+ // passed element at the end. Appending to an empty array is an error.
+
+ int Array::Append (Object *o)
+ {
+ if (nelem == size)
+ return 0;
+ array [curind = nelem] = o;
+ nelem ++;
+ return 1;
+ }
+
+ // The Array insert function inserts the object at the given position and
+ // moves the objects to the right by one position as required. Cannot insert
+ // far to the right of rightmost element. Returns a boolean success / fail
+ // value. Insertion also fails when the array is full.
+
+ int Array::Insert (Object *obj, int pos)
+ {
+ if (pos > nelem || pos < 0 || nelem >= size)
+ return 0;
+ for (int i = nelem; i > pos; i--)
+ array [i] = array [i - 1]; // Move objects right.
+ array [pos] = obj;
+ nelem ++;
+ curind = pos;
+ return 1;
+ }
+
+ // The Array assign function overwrites the object at the given position with
+ // the passed object if the passed position is valid.
+
+ int Array::Assign (Object *obj, int pos)
+ {
+ if (pos > nelem || pos < 0 || nelem >= size)
+ return 0;
+ if (pos == nelem)
+ nelem ++; // Adding new element
+ array [curind = pos] = obj;
+ return 1;
+ }
+
+ // The array get function just performs a simple array access on the array
+ // after validating bounds.
+
+ Object *Array::Get (int idx)
+ {
+ if (idx >= nelem || idx < 0)
+ return 0;
+ curind = idx;
+ return array [idx];
+ }
+
+ // The Fetch function gets an object from a list and returns it to the caller
+ // without deleting it. It also takes care of shifting elements
+ // appropriately etc.
+
+ Object *Array::Fetch (int idx)
+ {
+ if (idx >= nelem || idx < 0)
+ return 0;
+ Object *ret = Get (idx);
+ for (int i = idx; i < nelem - 1; i++)
+ array [i] = array [i + 1];
+ array [nelem - 1] = 0;
+ if (idx == nelem - 1) // Right most element
+ curind = idx - 1;
+ else
+ curind = idx;
+ nelem --;
+ return ret;
+ }
+
+ // The assign function is the only way to add / overwrite elements in a
+ // sparse array. It just overwrites the element in the position with the new
+ // object, while deleting the old one if one existed.
+
+ int SpArray::Assign (Object *o, int pos)
+ {
+ if (pos >= size || pos < 0)
+ return 0;
+ if (array [pos] == 0)
+ nelem ++; // increase element count
+ array [curind = pos] = o;
+ return 1;
+ }
+
+ // The Get function gets the required array element and returns it.
+
+ Object *SpArray::Get (int idx)
+ {
+ if (idx >= size || idx < 0)
+ return 0;
+ curind = idx;
+ return array [idx];
+ }
+
+ // The Fetch function just gets an array element and returns it, and forgets
+ // it from the sparse array.
+
+ Object *SpArray::Fetch (int pos)
+ {
+ Object *ret = Get (pos);
+ if (ret) // Location was occupied
+ {
+ array [pos] = 0;
+ if (pos == nelem - 1)
+ curind = pos - 1;
+ else
+ curind = pos;
+ nelem --;
+ }
+ return ret;
+ }
+
+ // Search all the elements in the array - as nelem is no limiting factor !
+
+ int SpArray::Search (Object &o)
+ {
+ for (int i = 0; i < size; i++)
+ {
+ Object *p = array [i];
+ if (p != 0) // Location is occupied
+ if (*p == o)
+ return i;
+ }
+ return -1;
+ }
+
+ // The sparse array destructor - deletes the elements from the rightmost to
+ // leftmost first. Once the elements are rid, the array pointer itself is
+ // deleted.
+
+ SpArray::~SpArray ()
+ {
+ if (type == VOLAT)
+ {
+ for (int i = size - 1; i >= 0; i--)
+ if (array [i] != 0)
+ Remove (i);
+ }
+
+ if (array)
+ {
+ delete array;
+ array = 0;
+ }
+ Init ();
+ }
+
+ class A : public Object
+ {
+ public :
+ int operator == (Object &o)
+ { char *a;
+ if (strcmp (a=o.Type (),Type ())) return 0;
+ return i == ((A&)o).i; }
+ char *Type () { return "A"; }
+ A (int j = 0) : i(j) {}
+ operator int () { return i; }
+ ~A () {}
+ protected :
+ int i;
+ };
+
+ class B : public A
+ {
+ public :
+ int operator == (Object &o)
+ { char *a;
+ if (strcmp (a=o.Type (),Type ())) return 0;
+ return j == ((B&)o).j; }
+ char *Type () { return "B"; }
+ B (int j=0) {}
+ operator int () { return j; }
+ ~B () {}
+ protected :
+ int j;
+ };
+
+
+ main ()
+ {
+ A *a1 = new A(1);
+ A *a2 = new A(2);
+ A *a3;
+ Array *ar = new Array(3,VOLAT);
+ SpArray *sar = new SpArray(3,VOLAT);
+ Array *a;
+ if (0)
+ a = ar;
+ else
+ a = sar;
+ a->Assign(a1);
+ a->Assign(a2, 2);
+ a3 = (A *)a->Fetch(2);
+ a3->Type();
+ }
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/onlyobj.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/onlyobj.h:1.1
*** /dev/null Mon Oct 4 15:01:30 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C++/objects/onlyobj.h Mon Oct 4 15:01:14 2004
***************
*** 0 ****
--- 1,41 ----
+ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ // Written by Ashok Sreenivasan, TRDDC, Pune, India. 1993. May be
+ // distributed freely, provided this comment is displayed at the top.
+ // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+ #ifndef __OBJ__
+
+ extern "C" {
+ int strlen(char *str1);
+ int strcmp(char *str1, char *str2);
+ char *strcpy(char *str1, char *str2);
+ char *strncpy(char *str1, char *str2, int i);
+ char *strcat(char *str1, char *str2);
+ }
+
+ class Object
+ {
+ public :
+
+ // Constructor - Dummy, useful for breakpoints !
+
+ Object () {}
+
+ // Destructor - also dummy but also virtual
+
+ virtual ~Object () {}
+
+ // (In)Equality operators
+
+ virtual int operator == (Object &) = 0;
+ int operator != (Object &o) { return !(*this == o); }
+
+ // Type enquiry function
+
+ virtual char *Type () { return "Object"; }
+ };
+
+ typedef Object *Objp;
+
+ #define __OBJ__
+ #endif
More information about the llvm-commits
mailing list