/*
 * This file is part of libdyn.a, the C Dynamic Object library.  It
 * contains the source code for the function DynDelete().
 *
 * There are no restrictions on this code; however, if you make any
 * changes, I request that you document them so that I do not get
 * credit or blame for your modifications.
 *
 * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
 * and MIT-Project Athena, 1989.
 */

#include <stdio.h>
#include "DynObject.H"

/*
 * Checkers!  Get away from that "hard disk erase" button!
 *    (Stupid dog.  He almost did it to me again ...)
 */                                 
int DynObject::Delete(int index)
{
     if (index < 0) {
	  if (debug)
	       fprintf(stderr, "dyn: delete: bad index %d\n", index);
	  return DYN_BADINDEX;
     }
     
     if (index >= num_el) {
	  if (debug)
	       fprintf(stderr, "dyn: delete: Highest index is %d.\n",
		       num_el);
	  return DYN_BADINDEX;
     }

     if (index == num_el-1) {
	  if (paranoid) {
	       if (debug)
		    fprintf(stderr, "dyn: delete: last element, zeroing.\n");
	       bzero(array + index*el_size, el_size);
	  }
	  else {
	       if (debug)
		    fprintf(stderr, "dyn: delete: last element, punting.\n");
	  }
     }	  
     else {
	  if (debug)
	       fprintf(stderr,
		       "dyn: delete: copying %d bytes from %d + %d to + %d.\n",
		       el_size*(num_el - index), array, (index+1)*el_size,
		       index*el_size);
	  
	  bcopy(array + (index+1)*el_size, array + index*el_size,
		el_size*(num_el - index));

	  if (paranoid) {
	       if (debug)
		    fprintf(stderr,
			    "dyn: delete: zeroing %d bytes from %d + %d\n",
			    el_size, array, el_size*(num_el - 1));
	       bzero(array + el_size*(num_el - 1), el_size);
	  }
     }
     
     num_el -= 1;
     
     if (debug)
	  fprintf(stderr, "dyn: delete: done.\n");

     return DYN_OK;
}
