# Copyright (c) 2023 Yuki Kimoto
# MIT License

class List {
  version_from SPVM;
  
  interface Cloneable;
  interface Countable;
  
  use Fn;
  use Array;
  
  # Fields
  has capacity : virtual ro int
    get {
      return capacity $self->{array};
    }
  ;
  
  has length : virtual ro int
    get {
      return @{$self->{array}};
    }
  ;
  
  has array : object[];
  
  # Class methods
  static method new : List ($array : object[] = undef, $capacity : int = -1) {
    my $self = new List;
    
    $self->init($array, $capacity);
    
    return $self;
  }
  
  protected method init : void ($array : object[] = undef, $capacity : int = -1) {
    
    my $length : int;
    
    if ($array) {
      $length = @$array;
    }
    else {
      $length = 0;
    }
    
    $self->init_len($array, $length, $capacity);
    
    if ($array) {
      Array->memcpy_object_address($self->{array}, 0, $array, 0, $length);
    }
    
  }
  
  static method new_len : List ($proto_array : object[], $length : int, $capacity : int = -1) {
    my $self = new List;
    
    $self->init_len($proto_array, $length, $capacity);
    
    return $self;
  }
  
  static method new_ref : List ($array : object[]) {
    
    unless ($array) {
      die "The array \$array must be defined.";
    }
    
    my $self = new List;
    
    $self->{array} = $array;
    
    return $self;
  }
  
  protected method init_len : void ($proto_array : object[], $length : int, $capacity : int = -1) {
    
    unless ($proto_array) {
      $proto_array = new object[0];
    }
    
    unless ($length >= 0) {
      die "The length \$length must be greater than or equal to 0.";
    }
    
    $self->{array} = Array->new_proto($proto_array, 0);
    
    if ($capacity >= 0) {
      set_capacity($self->{array}, $capacity);
    }
    
    set_length($self->{array}, $length);
  }
  
  # Instance methods
  method get : element ($index : int) {
    my $length = $self->length;
    
    unless ($index >= 0) {
      $index = $length + $index;
    }
    
    unless ($index >= 0) {
      die "The index \$index must be greater than or equal to 0.";
    }
    
    unless ($index < $length) {
      die "The index \$index must be less than the length of \$list.";
    }
    
    my $element = $self->{array}[$index];
    
    return $element;
  }
  
  method insert : void ($index : int, $element : object) {
    my $length = $self->length;
    
    unless ($index >= 0) {
      die "The index \$index must be greater than or equal to 0.";
    }
    
    unless ($index <= $length) {
      die "The index \$index must be less than or equal to the length of \$list.";
    }
    
    my $remove_length = 0;
    
    my $array = (object[])undef;
    
    if ($element) {
      $array = Array->new_array_proto_element($element, 1);
      $array->[0] = $element;
    }
    
    $self->replace($index, $remove_length, $array);
  }
  
  method pop : element () {
    my $length = $self->length;
    
    unless ($length > 0) {
      die "The length of the list \$list must be greater than 0.";
    }
    
    my $index = $length - 1;
    
    my $ret = $self->get($index);
    
    $self->set($index, undef);
    
    set_length($self->{array}, $self->length - 1);
    
    return $ret;
  }
  
  method push : void ($element : object) {
    
    my $length = $self->length;
    
    my $new_length = $length + 1;
    
    $self->_maybe_extend($new_length);
    
    my $index = $length;
    
    set_length($self->{array}, $new_length);
    $self->set($index, $element);
  }
  
  method remove : element ($index : int) {
    
    unless ($index >= 0) {
      die "The index \$index must be greater than or equal to 0.";
    }
    
    my $length = $self->length;
    
    unless ($index < $length) {
      die "The index \$index must be less than the length of \$list.";
    }
    
    my $remove_value = $self->get($index);
    
    my $remove_length = 1;
    $self->replace($index, $remove_length, undef);
    
    return $remove_value;
  }
  
  method replace : void ($offset : int, $remove_length : int, $replace : object[] = undef) {
    $self->splice($offset, $remove_length, $replace);
  }
  
  method splice : element[] ($offset : int, $remove_length : int, $replace : object[] = undef) {
    unless ($offset >= 0) {
      die "The offset \$offset must be greater than or equal to 0.";
    }
    
    unless ($remove_length >= 0) {
      die "The removal length \$remove_length must be greater than or equal to 0.";
    }
    unless ($offset + $remove_length <= $self->length) {
      die "The offset \$offset + the removal length \$remove_length must be less than or equal to the length of this list.";
    }
    
    my $replace_length = 0;
    if ($replace) {
      $replace_length = @$replace;
    }
    
    my $removed_elements = Array->copy_object_address($self->{array}, $offset, $remove_length);
    
    my $new_length = $self->length - $remove_length + $replace_length;
    $self->_maybe_extend($new_length);
    
    my $diff_length = $replace_length - $remove_length;
    
    if ($diff_length == 0) {
      # Do nothing
    }
    elsif ($diff_length > 0) {
      my $move_length = $self->length - $offset - $remove_length;
      
      set_length($self->{array}, $self->length + $diff_length);
      
      for (my $i = $move_length - 1; $i >= 0; $i--) {
        my $element = $self->get($offset + $remove_length + $i);
        $self->set($offset + $replace_length + $i, $element);
      }
    }
    elsif ($diff_length < 0) {
      my $move_length = $self->length - $offset - $remove_length;
      
      for (my $i = 0; $i < $move_length; $i++) {
        my $element = $self->get($offset + $remove_length + $i);
        $self->set($offset + $replace_length + $i, $element);
        $self->set($offset + $remove_length + $i, undef);
      }
    }
    
    set_length($self->{array}, $new_length);
    
    if ($replace) {
      for (my $i = 0; $i < $replace_length; $i++) {
        my $element = $replace->[$i];
        $self->set($offset + $i, $element);
      }
    }
    
    return $removed_elements;
  }
  
  method reserve : void ($new_capacity : int) {
    unless ($new_capacity >= 0) {
      die "The new capacity \$new_capacity must be greater than or equal to 0.";
    }
    
    my $capacity = $self->capacity;
    
    if ($new_capacity > $capacity) {
      set_capacity($self->{array}, $new_capacity);
    }
  }
  
  method resize : void ($new_length : int) {
    unless ($new_length >= 0) {
      die "The new length \$new_length must be greater than or equal to 0.";
    }
    
    my $length = $self->length;
    
    if ($new_length > $length) {
      $self->_maybe_extend($new_length);
    }
    elsif ($new_length < $length) {
      for (my $index = $new_length; $index < $length; $index++) {
        $self->set($index, undef);
      }
    }
    set_length($self->{array}, $new_length);
  }
  
  method set : void ($index : int, $element : object) {
    my $length = $self->length;
    
    unless ($index >= 0) {
      $index = $length + $index;
    }
    
    unless ($index >= 0) {
      die "The index \$index must be greater than or equal to 0.";
    }
    
    unless ($index < $length) {
      die "The index \$index must be less than the length of \$list.";
    }
    
    $self->{array}[$index] = $element;
  }
  
  method shift : element () {
    my $length = $self->length;
    
    unless ($length > 0) {
      die "The length of the list \$list must be greater than 0.";
    }
    
    my $elements = $self->{array};
    
    my $element = $elements->[0];
    
    Array->memmove_object_address($elements, 0, $elements, 1, $length - 1);
    
    $elements->[$length - 1] = undef;
    
    set_length($self->{array}, $self->length - 1);
    
    return $element;
  }
  
  method to_array : element[] () {
    my $length = $self->length;
    
    my $new_array = Array->new_proto($self->{array}, $length);
    
    my $array = $self->{array};
    
    Array->memcpy_object_address($new_array, 0, $array, 0, $length);
    
    return $new_array;
  }
  
  method unshift : void ($element : object) {
    my $length = $self->length;
    
    my $new_length = $length + 1;
    $self->_maybe_extend($new_length);
    
    my $elements = $self->{array};
    
    set_length($self->{array}, $new_length);
    Array->memmove_object_address($elements, 1, $elements, 0, $length);
    $elements->[0] = $element;
  }
  
  protected method _maybe_extend : void ($min_capacity : int, $just : int = 0) {
    my $capacity = $self->capacity;
    
    unless ($min_capacity > $capacity) {
      return;
    }
    
    if ($capacity < $min_capacity) {
      $capacity = $min_capacity;
    }
    
    my $new_capacity = $capacity * 2;
    
    set_capacity($self->{array}, $new_capacity);
  }
  
  method clone : List () {
    
    my $clone = &new_len($self->{array}, $self->length, $self->capacity);
    
    for (my $i = 0; $i < $self->length; $i++) {
      if ($self->{array}->[$i]) {
        $clone->{array}->[$i] = $self->{array}->[$i]->(Cloneable)->clone;
      }
    }
    
    return $clone;
  }
  
  method push_ : void ($elements : object[]) {
    
    unless ($elements) {
      die "The elements \$elements must be defined.";
    }
    
    for my $element (@$elements) {
      $self->push($element);
    }
    
  }
  
  method unshift_ : void ($elements : object[]) {
    
    unless ($elements) {
      die "The elements \$elements must be defined.";
    }
    
    for (my $i = @$elements - 1; $i >= 0; $i--) {
      my $element = $elements->[$i];
      $self->unshift($element);
    }
    
  }
  
  method get_array : element[] () {
    return $self->{array};
  }
  
}
