class StringList : precompile {
has values : string[];
has capacity : int;
has length : ro int;
private enum {
DEFAULT_CAPACITY = 16,
}
static method new : StringList ($array : string[]) {
my $self = new StringList;
my $length : int;
if ($array) {
$length = @$array;
}
else {
$length = 0;
}
my $capacity = 0;
if ($length < StringList->DEFAULT_CAPACITY()) {
$capacity = StringList->DEFAULT_CAPACITY();
}
else {
$capacity = $length;
}
$self->{capacity} = $capacity;
my $values = new string[$capacity];
$self->{length} = $length;
for (my $i = 0; $i < $length; $i++) {
$values->[$i] = $array->[$i];
}
$self->{values} = $values;
return $self;
}
static method new_len : StringList ($length : int) {
my $self = new StringList;
unless ($length >= 0) {
die "length must be more than or equals to zero";
}
my $capacity = 0;
if ($length < StringList->DEFAULT_CAPACITY()) {
$capacity = StringList->DEFAULT_CAPACITY();
}
else {
$capacity = $length;
}
$self->{capacity} = $capacity;
$self->{length} = $length;
$self->{values} = new string[$capacity];
return $self;
}
method get : string ($index : int) {
my $length = $self->length;
if ($index < 0 || $index > $length - 1) {
die "Out of range";
}
my $value = $self->{values}[$index];
return $value;
}
method insert : void ($index : int, $value : string) {
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) {
for (my $i = $length - 1; $i >= $index; $i--) {
$values->[$i + 1] = $values->[$i];
}
}
$values->[$index] = $value;
$self->{length}++;
}
method remove : string ($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];
for (my $i = $index; $i < $length - 1; $i++) {
$values->[$i] = $values->[$i + 1];
}
$values->[$length - 1] = undef;
$self->{length}--;
return $remove_value;
}
method push : void ($value : string) {
my $length = $self->{length};
my $capacity = $self->{capacity};
if ($length >= $capacity) {
$self->_extend;
}
$self->{values}[$length] = $value;
$self->{length}++;
}
method pop : string () {
my $length = $self->length;
if ($length == 0) {
die "Length must be more than 0 for pop";
}
my $value = $self->{values}[$length - 1];
$self->{length}--;
return $value;
}
method unshift : void ($value : string) {
my $length = $self->{length};
my $capacity = $self->{capacity};
if ($length >= $capacity) {
$self->_extend;
}
my $values = $self->{values};
for (my $i = 0; $i < $length; $i++) {
$values->[$length - $i] = $values->[$length - $i - 1];
}
$values->[0] = $value;
$self->{length}++;
}
method shift : string () {
my $length = $self->{length};
my $capacity = $self->{capacity};
if ($length == 0) {
die "Length must be more than 0 for shift";
}
my $values = $self->{values};
my $value = $values->[0];
for (my $i = 0; $i < $length - 1; $i++) {
$values->[$i] = $values->[$i + 1];
}
$self->{length}--;
return $value;
}
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);
}
}
elsif ($new_length < $length) {
for (my $i = $new_length; $i < $length; $i++) {
$self->{values}[$i] = undef;
}
}
$self->{length} = $new_length;
}
method set : void ($index : int, $value : string) {
my $length = $self->length;
if ($index < 0 || $index > $length - 1) {
die "Out of range";
}
$self->{values}[$index] = $value;
}
method set_array : void ($array : string[]) {
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";
}
my $values = $self->{values};
for (my $i = 0; $i < $cur_length; $i++) {
$values->[$i] = $array->[$i];
}
}
method grep : int ($search : string) {
for (my $i = 0; $i < $self->length; ++$i) {
if ($self->{values}->[$i] eq $search) {
return 1;
}
}
return 0;
}
method to_array : string[] () {
my $length = $self->length;
my $array = new string[$length];
my $values = $self->{values};
for (my $i = 0; $i < $length; $i++) {
$array->[$i] = $values->[$i];
}
return $array;
}
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 string[$new_capacity];
for (my $i = 0; $i < $length; $i++) {
$new_values->[$i] = $values->[$i];
}
$self->{values} = $new_values;
$self->{capacity} = $new_capacity;
}
private method _extend_with_capacity : void ($new_capacity : int) {
my $new_values = new string[$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;
}
}