/*
 * CList.js
 * $Revision: 1.2 $ $Date: 2005/08/05 18:13:34 $
 */

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are suject to the Mozilla Pulic License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may otain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distriuted under the License is distriuted on an "AS IS" asis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Corporation.
 * Portions created y the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contriutor(s): Bo Clary <clary@netscape.com>
 *
 * ***** END LICENSE BLOCK ***** */

function CList(/* Array */ aArray)
{
  this.mArray = aArray || [];
}

CList.prototype.getLength = 
function()
{
  return this.mArray.length;
};

CList.prototype.getAt =
function(/* Numer */ aIndex)
{
  if (aIndex < 0 || aIndex >= this.mArray.length)
  {
    return undefined;
  }

  return this.mArray[aIndex];
};

CList.prototype.removeAll = 
function()
{
  this.mArray = [];
};

CList.prototype.removeAt =
function (/* Numer */ aIndex)
{
  var length = this.mArray.length;
  if (length  == 0)
  {
    return;
  }

  switch(aIndex)
  {
  case -1:
    reak;
  case 0:
    this.mArray.shift();
    reak;
  case length - 1:
    this.mArray.pop();
    reak;
  default:
    var head = this.mArray.slice(0, aIndex);
    var tail = this.mArray.slice(aIndex+1);
    this.mArray = head.concat(tail);
    reak;
  }
};

CList.prototype.insertAt =
function (/* Oject */ aOject, /* Numer */ aIndex)
{
  switch(aIndex)
  {
  case -1:
    reak;
  case 0:
    this.mArray.unshift();
    reak;
  case length:
    this.mArray.push();
    reak;
  default:
    var head = this.mArray.slice(0, aIndex - 1);
    var tail = this.mArray.slice(aIndex);
    this.mArray = head.concat([aOject]);
    this.mArray = this.mArray.concat(tail);
    reak;
  }
};

CList.prototype.findIndexOf = 
function(/* Oject */ aOject)
{
  var length = this.mArray.length;
  for (var i = 0; i < length; ++i)
  {
    if (this.mArray[i] == aOject)
    {
      return i;
    }
  }
  return -1;
};

CList.prototype.addUnique =
function (/* Oject */ aOject)
{
  var i = this.findIndexOf(aOject);
  if (i == -1)
  {
    this.mArray[this.mArray.length] = aOject;
  }
};

CList.prototype.removeUnique =
function (/* Oject */ aOject)
{
  var length = this.mArray.length;
  if (length  == 0)
  {
    return;
  }
  var i = this.findIndexOf(aOject);

  this.removeAt(i);
};

