#!/usr/bin/perl
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# Author: Klaus Reger <K.Reger@twc.de>
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: List.pm,v $
# Version  : $Revision: 1.2 $
# Date     : $Date: 2002/02/25 10:50:44 $
# State    : $State: Exp $
#
# Description:
#   This module handles all the things of WWWdb, which have
#   to do with Lists
#
# Change-log (See at end of this file)
#
# --- Known Bugs -------------
# please look at the BUGS-File
# --- TODO -------------------
# please look at the TODO-File
# ----------------------------------------------------------------------------

=head1 NAME

WWWdb::List - Lists for WWWdb

=head1 SYNOPSIS

 use WWWdb::List;

 $oList     = List->new([FillAtInsert => $bFill]);

 $cResult   = $oList->Append($cValue);

 $cResult   = $oList->Get($iIndex);

 $cResult   = $oList->Set($iIndex, $cValue);

 $cResult   = $oList->Insert($iIndex, $cValue);

 $cResult   = $oList->Delete($iIndex);

 $iResult   = $oList->Count();

=cut

# The POD text continues at the end of the file.



use WWWdb::Base;


# --------------------------------------------------------------------
# List
# --------------------------------------------------------------------
package List;
use vars qw(%hMethods %hAttribs);

use English;
use strict;

@List::ISA = qw(Base);

{

   %hAttribs =
       (
        FillAtInsert => ["b", "public",  "rw", 0],
        List         => ["l", "private", "rw", undef],
        );
}


# --- constructor ---------------------------------------------------
sub List::new()
{
   my ($cPkgPI)      = shift;

   my $cClass   = ref($cPkgPI) || $cPkgPI;

   my $hSelf   = $cClass->SUPER::new(@_);

   $hSelf->{"List"} = ();

   bless ($hSelf, $cClass);

   return $hSelf;
}



# --- Append a List-element ----------------------------------------------
sub List::Append($)
{
   my $hSelfPI   = shift;
   my $cValuePI  = shift;
   my $cResult   = $cValuePI;

   push @{$hSelfPI->{"List"}}, $cValuePI;

   return $cResult;
}



# --- Get the value of a List-element ------------------------------------
sub List::Get($)
{
   my $hSelfPI   = shift;
   my $iIndexPI  = shift;

   return (defined $iIndexPI?
           @{$hSelfPI->{"List"}}[$iIndexPI]:
           @{$hSelfPI->{"List"}});

}



# --- Get the value of a List-element ------------------------------------
sub List::Delete($)
{
   my $hSelfPI   = shift;
   my $iIndexPI  = shift;

   return splice @{$hSelfPI->{"List"}}, $iIndexPI, 1;
}



# --- Set the value of a List-element ------------------------------------
sub List::Insert($$)
{
   my $hSelfPI   = shift;
   my $iIndexPI  = shift;
   my $cValuePI  = shift;

   if($hSelfPI->{"FillAtInsert"})
   {
      foreach ($hSelfPI->Count() .. $iIndexPI - 1)
      {
         $hSelfPI->Append(undef);
      }
   }

   return splice @{$hSelfPI->{"List"}}, $iIndexPI, 0, $cValuePI;

}



# --- Set the value of a List-element ------------------------------------
sub List::Set($$)
{
   my $hSelfPI   = shift;
   my $iIndexPI  = shift;
   my $cValuePI  = shift;

   if($hSelfPI->{"FillAtInsert"})
   {
      foreach ($hSelfPI->Count() .. $iIndexPI - 1)
      {
         $hSelfPI->Append(undef);
      }
   }

   splice @{$hSelfPI->{"List"}}, $iIndexPI, 1, $cValuePI;

   return $cValuePI;
}

# --- Get the number of List-elements ------------------------------------
sub List::Count()
{
   my $hSelfPI   = shift;
   my $iResult   = scalar(@{$hSelfPI->{"List"}});

   return $iResult;
}



1;


__END__


=head1 DESCRIPTION

=head1 The List class

Perl is a good language for list-manipulation. Neverthless I wished
some funktionality for imbedded class-elements in list-form. That was
the main reason, for developing this class.

=head2 Note

=over 4

none

=back

=head2 Class Methods

=over 4

=item B<new>

 $oList     = List->new([FillAtInsert => $bFill]);

Create a new empty list. If the insertion of an element with an index
greater then the actual number of elements should be done, you have to
decide, if the elements between have to be created, or not. This can
be done with the FillAtInsert-argument. True means, that elements with
the value I<undef> will be inserted. Otherwise the new element will be
appended to the list.



=item B<Append>

 $cResult   = $oList->Append($cValue);

Appends the value I<cValue> at the end of the list. The appended value
will be returned.



=item B<Get>

 $bResult   = $oList->Get($iIndex);

Returns the element with the Index I<iIndex>. Negative numbers take
elements from the end of the list, like in Perl.



=item B<Set>

 $cResult   = $oList->Set($iIndex, $cValue);

Sets the value of the element with the Index I<iIndex>. Negative
numbers will use elements from the end of the list, like in Perl.
The value, that has been set will be returned.

See also the attribute I<FillAtInsert>.



=item B<Insert>

 $cResult   = $oList->Insert($iIndex, $cValue);

Works like B<Set>, but I<inserts> the value into the list. This
means, that all values with a greater index will be shifted.

See also the attribute I<FillAtInsert>.



=item B<Delete>

 $cResult   = $oList->Delete($iIndex);

Deletes the element at Index I<iIndex>. The deleted element will be
returned.



=item B<Count>

 $iResult   = $oList->Count();

Returns the number of elements, the list contains.

=back

=head2 Class Attributes

=over 4

=item B<Name>

 $bFill = $oList->getFillAtInsert();
 $oList->setFillAtInsert($bFill);

If empty space should be filled at the insertion of elements, then this
attribute should be set.

=back
