class ByteList : precompile {
  use Fn;
  
  has values   : byte[];
  has capacity : int;
  has length   : ro int;
  
  private enum {
    DEFAULT_CAPACITY = 16,
  }
  
  static method new : ByteList ($array : byte[]) {
    my $self = new ByteList;
    
    my $length : int;
    if ($array) {
      $length = @$array;
    }
    else {
      $length = 0;
    }
    
    my $capacity = 0;
    if ($length < ByteList->DEFAULT_CAPACITY()) {
      $capacity = ByteList->DEFAULT_CAPACITY();
    }
    else {
      $capacity = $length;
    }
    my $values = new byte[$capacity];
    
    $self->{capacity} = $capacity;
    $self->{length} = $length;
    
    if ($array) {
      Fn->memcpy_byte($values, 0, $array, 0, $length);
    }
    
    $self->{values} = $values;
    
    return $self;
  }
  
  static method new_len : ByteList ($length : int) {
    my $self = new ByteList;
    unless ($length >= 0) {
      die "length must be more than or equals to zero";
    }
    my $capacity = 0;
    if ($length < ByteList->DEFAULT_CAPACITY()) {
      $capacity = ByteList->DEFAULT_CAPACITY();
    }
    else {
      $capacity = $length;
    }
    $self->{capacity} = $capacity;
    $self->{length} = $length;
    $self->{values} = new byte[$capacity];
    return $self;
  }
  
  private method _extend_with_capacity : void ($new_capacity : int) {
    my $new_values = new byte [$new_capacity];
    for (my $i = 0; $i < $self->{length}; ++$i) {
      my $target = $i;
      if ($target < 0) { $target += $self->{capacity}; };
      $new_values->[$i] = $self->{values}[$target];
    }
    $self->{capacity} = $new_capacity;
    $self->{values} = $new_values;
  }

  private method _extend : void () {
    my $capacity = $self->{capacity};
    my $length = $self->{length};
    my $values = $self->{values};
    
    my $new_capacity = $capacity * 2;
    
    my $new_values = new byte[$new_capacity];
    
    Fn->memcpy_byte($new_values, 0, $values, 0, $length);
    
    $self->{values} = $new_values;
    $self->{capacity} = $new_capacity;
  }
  
  method insert : void ($index : int, $value : byte) {
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    unless ($index >= 0 && $index <= $length) {
      die "Out of range";
    }
    
    if ($length >= $capacity) {
      $self->_extend;
    }
    
    my $values = $self->{values};
    if ($index != $length) {
      my $dist_index = $index + 1;
      my $move_length = $length - $index;
      Fn->memmove_byte($values, $dist_index, $values, $index, $move_length);
    }
    $values->[$index] = $value;
    
    $self->{length}++;
  }

  method remove : byte ($index : int) {
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    unless ($index >= 0 && $index < $length) {
      die "Out of range";
    }
    
    my $values = $self->{values};
    my $remove_value = $values->[$index];

    my $dist_index = $index;
    my $src_index = $index + 1;
    my $move_length = $length - $index - 1;
    Fn->memmove_byte($values, $dist_index, $values, $src_index, $move_length);
    
    $self->{length}--;
    
    return $remove_value;
  }

  method push : void ($value : byte) {
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    if ($length >= $capacity) {
      $self->_extend;
    }
    
    $self->{values}[$length] = $value;
    
    $self->{length}++;
  }
  
  method pop : byte () {
    my $length = $self->length;
    
    if ($length == 0) {
      die "Can't pop";
    }
    
    my $value = $self->{values}[$length - 1];
    
    $self->{values}[$length - 1] = 0;
    
    $self->{length}--;
    
    return $value;
  }
  
  method unshift : void ($value : byte) {
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    if ($length >= $capacity) {
      $self->_extend;
    }
    
    my $values = $self->{values};
    
    Fn->memmove_byte($values, 1, $values, 0, $length);
    
    $values->[0] = $value;
    $self->{length}++;
  }
  
  method shift : byte () {
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    if ($length == 0) {
      die "Can't shift";
    }
    
    my $values = $self->{values};
    
    my $value = $values->[0];
    
    Fn->memmove_byte($values, 0, $values, 1, $length - 1);

    $values->[$length - 1] = 0;
    
    $self->{length}--;
    
    return $value;
  }
  
  method set : void ($index : int, $value : byte) {
    my $length = $self->length;
    
    if ($index < 0 || $index > $length - 1) {
      die "Out of range";
    }
    
    $self->{values}[$index] = $value;
  }

  method set_array : void ($array : byte[]) {
    unless ($array) {
      die "Array must be defined";
    }
    
    my $cur_length = $self->length;
    
    my $set_length = @$array;
    
    unless ($set_length == $cur_length) {
      die "The length of argument array must be same as the length of current list array";
    }
    
    
    Fn->memcpy_byte($self->{values}, 0, $array, 0, $cur_length);
  }

  method get : byte ($index : int) {
    my $length = $self->length;
    
    if ($index < 0 || $index > $length - 1) {
      die "Out of range";
    }
    
    my $value = $self->{values}[$index];
    
    return $value;
  }
  
  method to_array : byte[] () {
    my $length = $self->length;
    
    my $array = new byte[$length];
    
    my $values = $self->{values};
    
    Fn->memcpy_byte($array, 0, $values, 0, $length);
    
    return $array;
  }

  method resize : void ($new_length : int) {
    if ($new_length < 0) {
      die "New length must be more than or equals to 0";
    }
    
    my $length = $self->{length};
    my $capacity = $self->{capacity};
    
    if ($new_length > $length) {
      if ($new_length > $capacity) {
        $self->_extend_with_capacity($new_length);
      }
      for (my $i = $length; $i < $new_length; $i++) {
        $self->{values}[$i] = 0;
      }
    }
    elsif ($new_length < $length) {
      for (my $i = $new_length; $i < $length; $i++) {
        $self->{values}[$i] = 0;
      }
    }
    
    $self->{length} = $new_length;
  }
}