image thumbnail

Data Structure: A Cell Array List Container

by

 

14 Aug 2009 (Updated )

Provides a useful 1D container for storing an ordered heterogeneous set of elements

Test Script for Class CellArrayList

Test Script for Class CellArrayList

Step through and execute this script cell-by-cell to verify a cell array implementation of the List Abstract Data Type (ADT).

Written by Bobby Nedelkovski MathWorks Australia Copyright 2009-2010, The MathWorks, Inc.

Contents

Clean Up

clear classes
clc
Warning: Objects of 'onCleanup' class exist.  Cannot clear this class or any of its super-classes. 

Create Instance of CellArrayList

myList = CellArrayList();

Check Number of Elements

empty = 1 len = 0

empty = myList.isempty()
len = myList.length()
empty =
     1
len =
     0

Append Arbitrary Elements to End of List

myList.add(5);        % a single integer
myList.add(rand(2));  % a 2x2 matrix
myList.add({50,55});  % 2 integers as 2 unique elements

Check Number of Elements

empty = 0 len = 4

empty = myList.isempty()
len = myList.length()
empty =
     0
len =
     4

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

myList
***List #1***
list[1]{1} =
     5
list[1]{2} =
    0.9593    0.1386
    0.5472    0.1493
list[1]{3} =
    50
list[1]{4} =
    55

Seek an Element

count = 1 location = 3

count = myList.countOf(50)
location = myList.locationsOf(50)
count =
     1
location =
     3

Insert Elements in Arbitrary Locations

myList.add({rand(3),5:7},2);  % a 3x3 matrix and a 1x3 array
myList.add(myList,5);         % reference to self!

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

myList
***List #1***
list[1]{1} =
     5
list[1]{2} =
    0.2575    0.8143    0.3500
    0.8407    0.2435    0.1966
    0.2543    0.9293    0.2511
list[1]{3} =
     5     6     7
list[1]{4} =
    0.9593    0.1386
    0.5472    0.1493
list[1]{5} =
  CellArrayList handle with no properties.
list[1]{6} =
    50
list[1]{7} =
    55

Insert Elements in Arbitrary Locations

myList.add({10,11;12,13},6);  % a 2x2 cell array
myList.add({150,160,170},4);  % 3 integers as 3 unique elements

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

myList
***List #1***
list[1]{1} =
     5
list[1]{2} =
    0.2575    0.8143    0.3500
    0.8407    0.2435    0.1966
    0.2543    0.9293    0.2511
list[1]{3} =
     5     6     7
list[1]{4} =
   150
list[1]{5} =
   160
list[1]{6} =
   170
list[1]{7} =
    0.9593    0.1386
    0.5472    0.1493
list[1]{8} =
  CellArrayList handle with no properties.
list[1]{9}{1,1} =
    10
list[1]{9}{2,1} =
    12
list[1]{9}{1,2} =
    11
list[1]{9}{2,2} =
    13
list[1]{10} =
    50
list[1]{11} =
    55

Check Number of Elements

len = 11

len = myList.length()
len =
    11

Retrieve Elements

elt = myList.get(4)  % elt = 150
elt = myList.get(7)  % elt = 2x2 rand matrix
elt = myList.get(9)  % elt = 2x2 cell array
elt =
   150
elt =
    0.9593    0.1386
    0.5472    0.1493
elt = 
    [10]    [11]
    [12]    [13]

Retrieve Multiple Elements

elts = {150;160;3x3 rand matrix}

elts = myList.get([4:5,2])
elts = 
    [       150]
    [       160]
    [3x3 double]

Add Duplicate Element

myList.add(5,7);

Seek an Element

count = 2 locations = [1;7]

count = myList.countOf(5)
locations = myList.locationsOf(5)
count =
     2
locations =
     1
     7

Try to Remove Elements

This yields an empty set [ ] since myList.length()=12 (refer to the specification for remove() in the List abstract class)

elts = myList.remove([5:6,20])
elts =
     []

Remove Some Elements

elts = {160;170;reference to self}

elts = myList.remove([5:6,9])
elts = 
    [              160]
    [              170]
    [1x1 CellArrayList]

Check Number of Elements

len = 9

len = myList.length()
len =
     9

Remove All Elements

elts = myList.remove(1:myList.length())
elts = 
    [         5]
    [3x3 double]
    [1x3 double]
    [       150]
    [         5]
    [2x2 double]
    {2x2 cell  }
    [        50]
    [        55]

Check Number of Elements

empty = 1 len = 0

empty = myList.isempty()
len = myList.length()
empty =
     1
len =
     0

Create an Array of CellArrayLists

Gives a 2x2 CellArrayList

myList(2,2) = CellArrayList();

Add Elements to All Lists

myList.add({rand(3),5:7},2);  % a 3x3 matrix and a 1x3 array
myList.add(500,5);            % a single integer

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

myList
***List #1***
list[1]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[1]{2} =
     5     6     7
list[1]{3} =
   500

***List #2***
list[2]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[2]{2} =
     5     6     7
list[2]{3} =
   500

***List #3***
list[3]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[3]{2} =
     5     6     7
list[3]{3} =
   500

***List #4***
list[4]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[4]{2} =
     5     6     7
list[4]{3} =
   500

Add Elements to 2 Lists

myList(:,1).add({99,100});  % 2 integers

Check Number of Elements

len = [5,3; 5,3]

len = myList.length()
len =
     5     3
     5     3

Insert Element to 2 Lists in Location 4

myList(1,:).add(rand(2),4);  % a 2x2 rand matrix

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

myList
***List #1***
list[1]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[1]{2} =
     5     6     7
list[1]{3} =
   500
list[1]{4} =
    0.7537    0.5678
    0.3804    0.0759
list[1]{5} =
    99
list[1]{6} =
   100

***List #2***
list[2]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[2]{2} =
     5     6     7
list[2]{3} =
   500
list[2]{4} =
    99
list[2]{5} =
   100

***List #3***
list[3]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[3]{2} =
     5     6     7
list[3]{3} =
   500
list[3]{4} =
    0.7537    0.5678
    0.3804    0.0759

***List #4***
list[4]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[4]{2} =
     5     6     7
list[4]{3} =
   500

Check Number of Elements

len = [6,4; 5,3]

len = myList.length()
len =
     6     4
     5     3

Append Element to Single List

myList(2,1).add(99);  % an integer

Check Number of Elements

len = [6,4; 6,3]

len = myList.length()
len =
     6     4
     6     3

Seek an Element

count = [1,0; 2,0] locations = {5,[ ]; [4;6],[ ]}

count = myList.countOf(99)
locations = myList.locationsOf(99)
count =
     1     0
     2     0
locations = 
    [         5]    [0x1 double]
    [2x1 double]    [0x1 double]

Remove Some Elements

elts = {{2x2 rand matrix; 99},[ ]; {99; 100},[ ]}

elts = myList.remove([4,5])
elts{1,1}
elts{2,1}
elts = 
    {2x1 cell}    []
    {2x1 cell}    []
ans = 
    [2x2 double]
    [        99]
ans = 
    [ 99]
    [100]

Display 'myList'

Alternatively, you can execute "myList.display()" which produces the same output.

myList
***List #1***
list[1]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[1]{2} =
     5     6     7
list[1]{3} =
   500
list[1]{4} =
   100

***List #2***
list[2]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[2]{2} =
     5     6     7
list[2]{3} =
   500
list[2]{4} =
    99

***List #3***
list[3]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[3]{2} =
     5     6     7
list[3]{3} =
   500
list[3]{4} =
    0.7537    0.5678
    0.3804    0.0759

***List #4***
list[4]{1} =
    0.6160    0.8308    0.9172
    0.4733    0.5853    0.2858
    0.3517    0.5497    0.7572
list[4]{2} =
     5     6     7
list[4]{3} =
   500

Contact us