<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="/lib/xsl/devedge-1.00/article_en.xsl"?>
<nde:article 
  url="/toolbox/examples/2003/CList/" 
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:nde="http://devedge.netscape.com/2002/de" 
  xmlns:ent="http://devedge.netscape.com/2003/ent"
  xml:lang="en">

  <nde:header>

    <nde:title>
      CList - An Array Adapter Class which supports unique Elements
    </nde:title>

    <nde:category>
      Example
    </nde:category>

    <nde:pubdate year="2003" month="06" day="13"/>

    <nde:summary>
      CList simplifies the maintenance of Arrays which must contain
      unique values without duplicate entries.
    </nde:summary>
    
    <nde:channels>
      <nde:channel id="dom"/>
      <nde:channel id="examples"/>
    </nde:channels>
    
    <nde:keywords>
      Netscape, Netscape 7, Mozilla, Gecko, Opera, Internet Explorer, Array, Adapter Patterns
    </nde:keywords>
    
    <nde:authlist>
      <nde:author>
        <nde:authname>Bob Clary</nde:authname>
        <nde:authtitle>Evangelist</nde:authtitle>
        <nde:authaffil>Netscape Communications</nde:authaffil>
      </nde:author>
    </nde:authlist>

  </nde:header>

  <nde:head>
  </nde:head>

  <nde:content>

    <h2 name="introduction">Introduction</h2>

    <p>
    </p>

    <h2 name="script">Script</h2>

    <p>
      <code><a href="CList.js">CList.js</a></code> 
      implements a JavaScript Object <code>CList</code> 
      which is used to adapt an Array for ease of 
      maintaining a list of unique values with no duplicates.
    </p>

    <dl>
      <dt>Constructor</dt>
      <dd>
        <dl>
          <dt>CList(Array aArray)</dt>
          <dd>
            <p>
              Constructs an instance of a CList object 
              which adapts the specified Array <code>aArray</code>.
              If <code>aArray</code> is not specified, an empty
              array will be used to initialize the <code>CList</code>.
            </p>
          </dd>
        </dl>
      </dd>
      <dt>Properties</dt>
      <dd>
        <dl>
          <dt>Array mArray</dt>
          <dd>
            <p>
              <code>mArray</code> is the array instance
              which will be used to contain the list of
              unique values.
            </p>
          </dd>
        </dl>
      </dd>
      <dt>Methods</dt>
      <dd>
        <dl>
          <dt>getLength()</dt>
          <dd>
            <p>
              <code>getLength()</code> returns the length
              of the <code>mArray</code> array.
            </p>
          </dd>
          <dt>getAt(Number aIndex)</dt>
          <dd>
            <p>
              <code>getAt()</code> returns the element
              at index <code>aIndex</code>.
            </p>
          </dd>
          <dt>removeAll()</dt>
          <dd>
            <p>
              <code>removeAll()</code> removes all elements
              from the <code>CList</code>.
            </p>
          </dd>
          <dt>removeAt(Number aIndex)</dt>
          <dd>
            <p>
              <code>removeAt()</code> removes the element at
              index <code>aIndex</code> from the <code>CList</code>.
            </p>
          </dd>
          <dt>insertAt(Object aObject, Number aIndex)</dt>
          <dd>
            <p>
              <code>insertAt()</code> inserts the object
              <code>aObject</code> into the <code>CList</code>
              at the position specified by the <code>aIndex</code>
              index.
            </p>
          </dd>
          <dt>findIndexOf(Object aObject)</dt>
          <dd>
            <p>
              <code>findIndexOf()</code> returns the index
              of the specified object <code>aObject</code> 
              in the <code>CList</code> or <code>-1</code> 
              if the object is not in the list.
            </p>
          </dd>
          <dt>addUnique(Object aObject)</dt>
          <dd>
            <p>
              <code>addUnique()</code> appends the specified
              <code>aObject</code> to the <code>CList</code>
              if it is not already contained in the list.
            </p>
          </dd>
          <dt>removeUnique(Object aObject)</dt>
          <dd>
            <p>
              <code>removeUnique()</code> removes the
              specified object <code>aObject</code> 
              from the <code>CList</code>.
            </p>
          </dd>
        </dl>
      </dd>
    </dl>

    <script type="text/javascript" src="CList.js"></script>

    <h2 name="example1">Example 1</h2>

    <p>
    </p>

    <pre><![CDATA[
function assert(aCondition)
{
  if (!aCondition)
  {
    alert('There was an internal error in CList');
  }
}

function doExample1()
{
  var list = new CList();

  assert(list.getLength() == 0);

  list.addUnique(1);

  assert(list.getLength() == 1);
  assert(list.mArray.toString() == '1');

  list.addUnique(1);

  assert(list.getLength() == 1);
  assert(list.mArray.toString() == '1');

  list.addUnique(2);

  assert(list.getLength() == 2);
  assert(list.mArray.toString() == '1,2');

  list.addUnique(3);

  assert(list.getLength() == 3);
  assert(list.mArray.toString() == '1,2,3');

  alert('Result after adding 1,1,2,3 = ' + list.mArray.toString());

  list.removeUnique(2);

  assert(list.getLength() == 2);
  assert(list.mArray.toString() == '1,3');

  alert('Result after removing 2 = ' + list.mArray.toString());

  list.removeUnique(1);
  list.removeUnique(3);

  assert(list.getLength() == 0);

  list = new CList( [1, 2, 3] );

  var length = list.getLength();
  var s = '';

  for (var i = 0; i < length; ++i)
  {
    if (i != 0)
    {
      s += ',';
    }
    s += list.getAt(i);
  }
  assert(s == '1,2,3');

  list.removeAll();

  assert(list.getLength() == 0);
  assert(list.mArray.toString() == '');
}
]]></pre>

    <script type="text/javascript"><![CDATA[
function assert(aCondition)
{
  if (!aCondition)
  {
    alert('There was an internal error in CList');
  }
}

function doExample1()
{
  var list = new CList();

  assert(list.getLength() == 0);

  list.addUnique(1);

  assert(list.getLength() == 1);
  assert(list.mArray.toString() == '1');

  list.addUnique(1);

  assert(list.getLength() == 1);
  assert(list.mArray.toString() == '1');

  list.addUnique(2);

  assert(list.getLength() == 2);
  assert(list.mArray.toString() == '1,2');

  list.addUnique(3);

  assert(list.getLength() == 3);
  assert(list.mArray.toString() == '1,2,3');

  alert('Result after adding 1,1,2,3 = ' + list.mArray.toString());

  list.removeUnique(2);

  assert(list.getLength() == 2);
  assert(list.mArray.toString() == '1,3');

  alert('Result after removing 2 = ' + list.mArray.toString());

  list.removeUnique(1);
  list.removeUnique(3);

  assert(list.getLength() == 0);

  list = new CList( [1, 2, 3] );

  var length = list.getLength();
  var s = '';

  for (var i = 0; i < length; ++i)
  {
    if (i != 0)
    {
      s += ',';
    }
    s += list.getAt(i);
  }
  assert(s == '1,2,3');

  list.removeAll();

  assert(list.getLength() == 0);
  assert(list.mArray.toString() == '');
}
]]></script>
    <form method="get" action="">
      <input type="button" onclick="doExample1()" value="Run Example 1" />
    </form>

  </nde:content>

  <nde:related area="nde">
    <nde:item url="/central/">DOM Central</nde:item>
    <nde:item url="/toolbox/examples/">Examples</nde:item>
  </nde:related>
  
</nde:article>
