/* cvector.h */

#ifndef _CVECTOR_H
#define _CVECTOR_H 1

/* Vector structure */

typedef struct Vector {
  /* Fill in this structure with fields that you need */
   
} Vector;

/* Implement the following functions */

/* Create a new Vector of objects.  The maxsize arguments specifies
   the maximum number of elements in the vector.   However, specifying
   a maxsize of -1 means that the Vector can be of any size and that
   it will grow to accomodate as many items as are placed into it. 

   Example:

       Vector *v;
       v = new_Vector(1000); 

*/
extern Vector *new_Vector(int maxsize);

/* Delete a vector of objects */
extern void   delete_Vector(Vector *v);

/* Return the length of the vector v.  This is the number of items
   currently stored in the vector. */
extern int    Vector_len(const Vector *v);

/* Add a new item to the vector.  Since the vector can hold 
   any kind of object, items are described by a void * (a generic
   pointer).  When a new item is added, it is simply appended to 
   the end of the vector.  Also, the vector merely holds a copy
   of the pointer value itself---it does not make a copy of
   the actual item.

   The function returns the number of items stored in the vector
   on success.  Otherwise, it returns -1.  This latter case 
   could occur if adding the item would exceed the maximum number
   of items specified to new_Vector().  Note: a vector may
   have no size restriction in which case it should always be
   expanded to hold the new item if necessary.

   Example:  this creates a vector and put 3 strings in it.

         Vector *v = new_Vector(100);
         Vector_append(v,new_String("Hello World"));
         Vector_append(v,new_String("Yikes!"));
         Vector_append(v,new_String("Blah"));
*/

extern int   Vector_append(Vector *v, void *item);

/* Return the nth item from Vector v.   If n is out of
   range, the function should abort with an assertion. 

   Example:

         Vector *v;
         String *s;
         ...

         s = (String *) Vector_getitem(v,1);
*/

extern void *Vector_getitem(const Vector *v, int n);

/* Change the nth item of Vector v.  If n is out of range,
   the function should abort with an assertion. 

   Example:

         Vector *v;
         String *s = new_String("Whatever");
 
         Vector_setitem(v,2,s);
*/

extern void  Vector_setitem(Vector *v, int n, void *newitem);

/* Remove the nth item of Vector v.  If n is out of range, 
   an assertion is thrown.  Otherwise, the item is removed
   and all items above it in the vector are moved down
   one slot.  The length of the vector decreases by 1.

   The function returns the item that was removed.  You might
   use this to delete it or to put the item someplace else.
 */

extern void *Vector_delitem(Vector *v, int n);

/* Return a slice.  This function returns a new vector that 
   contains a subset of another vector.  This works exactly
   like the String_getslice() function from the last assignment
*/

extern Vector *Vector_getslice(const Vector *v, int first, int last);

/* Search.  The following function searches a vector for a specific
   item.   The item argument is the item to search for.  The cmp
   argument is a *function* that compares two items to see if they
   are the same or not.  The cmp function returns 0 for a match, positive if x is greater
   than y, or negative if x is less than y.   The search function returns
   the index >= 0 of the item if found or -1 if it wasn't found.

   Here is an example of using this function with strings.

   int compare_strings(void *x, void *y) {
       String *xs, *ys;
       xs = (String *) x;
       ys = (String *) y;
       return String_strcmp(xs,ys);
   }

   foo() {
       String *s;
       Vector *v = new_Vector(100);

       ... fill in v with a bunch of values ...

       s = new_String("Blah");
       if (Vector_search(v,s,compare_strings) >= 0) {
           printf("Found!\n");
       } else {
           printf("Not found!\n");
       }
   }        
*/

extern int Vector_search(const Vector *v, void *item, int (*cmp)(void *x,void *y));

/* Sort.  Sorts the items in a Vector.   This should work about the same
   as the C qsort() library function */

extern void Vector_qsort(Vector *v, int (*cmp)(void *, void *));

#endif
