/*
 *      Copyright (c) 2023, an unpublished work by CodeSecure, Inc.
 *                      ALL RIGHTS RESERVED
 *
 *      Copyright (c) 2005-2023, an unpublished work by GrammaTech, Inc.
 *                      ALL RIGHTS RESERVED
 *
 *      This software is furnished under a license and may be used and
 *      copied only in accordance with the terms of such license and the       
 *      inclusion of the above copyright notice.  This software or any
 *      other copies thereof may not be provided or otherwise made
 *      available to any other person.  Title to and ownership of the
 *      software is retained by CodeSecure, Inc.
 */

/* my_iterator.c
 *
 * contains definitions for an iterator
 */
#include <stdlib.h>
#include "my_iterator.h"


it *my_iterator_New(void){
    return (it *)malloc(sizeof(it));
}


void my_iterator_Dispose(it *i){
        free(i);
}

               /* sets up the iterator */
void my_iterator_Init(it *i){
  i->ptr=0;
  i->used=0;
}


               /* sends iterator back to beginning of space*/
void my_iterator_First(it *i){
  i->ptr=0;
}

               /* advances the iterator.  Returns
                * 1 if advanced successfully, 
                * 0 if at end of iterator space or current 
                * position has nothing in it */
int my_iterator_Next(it *i){
  if (i->ptr == MAXSIZE-1)
    return 0;
  if (i->ptr >= i->used)
    return 0;
  i->ptr = i->ptr+1;
  return 1;
}


               /* checks that current iterator position 
                * has a valid value */
int my_iterator_IsValid(it *i){
  if (i->ptr <= i->used)
    return 1;
  return 0;
}


             /* gets value at current iterator position */
int my_iterator_GetVal(it *i){
  return i->buf[i->ptr];
}


             /* sets value at current iterator position to val */ 
void my_iterator_SetVal(it *i, int val){

  i->buf[i->ptr] = val;

  if (i->used <= i->ptr){
    i->used = i->ptr + 1;
  }
}
