Difference between revisions of "Iterator"

From CDOT Wiki
Jump to: navigation, search
m
(References)
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
An Iterator provides the user with a way to access the elements of some kind of list sequentially, while keeping the elements of that list from exposing their underlying representation.
 
__TOC__  
 
__TOC__  
The '''Iterator Pattern''' is commonly used in Computer Science as a way to access elements of a collection, regardless of how they were implemented. An iterator may also be called a cursor.
 
== Description ==
 
An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection. Depending on the language the iterator is implemented in, other functionality may be added to the iterator object.
 
  
The purpose of an iterator is to give a user a way to process each element of a container while separating the user from the internal structure of the container. This allows the container to store elements however it wants to, while allowing the user to use it as if it were just a simple list or sequence. An iterator class is usually designed in coordination with the container class, which usually provides methods or functions for creating iterators.
+
== Introduction to Iterators ==
 +
 
 +
An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection (current, and current.next). Depending on the language the iterator is implemented in, other functionality may be added to the iterator object, such as remove and update and so on.
 +
 
 +
Iterator ties together the [http://en.wikipedia.org/wiki/Object_oriented_programming object-oriented programming] principles known as encapsulation and [http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 polymorphism]. Using an iterator, you can manipulate the objects in a collection without explicitly knowing how the collection is implemented or what the collection is made up of (Different types of objects perhaps?). An iterator provides an interface to different iteration implementations, which contain the details of how to manipulate a specific collection, including which items in the collection to show (filtering) and in what order (sorting).
 +
 
 +
An easy way to think of iterators, is to also think of, [http://en.wikipedia.org/wiki/List_%28computing%29 Lists], [http://en.wikipedia.org/wiki/Linked_list Linked Lists], [http://en.wikipedia.org/wiki/Binary_tree Binary Trees], and [http://en.wikipedia.org/wiki/Hash_tables Hash Tables], because they operate very much in the same way that iterators do.  
  
An easy way to think of iterators, is to also think of, Lists, Linked Lists, Binary Trees, and Hash Tables, because they operate very much in the same way that iterators do.
 
  
 
=== Implicit Iterators ===
 
=== Implicit Iterators ===
 +
 
Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object. Some of these language include:
 
Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object. Some of these language include:
 +
 
* C#
 
* C#
 
* Java (After 5.0)
 
* Java (After 5.0)
 
* Python
 
* Python
 
* PERL
 
* PERL
 +
  
 
=== UML ===
 
=== UML ===
  
Here is a UML Diagram of the Iterator Pattern.
+
Design class diagram in UML of the Iterator Pattern being used in a system.
 +
 
 +
[[Image:Iterator1.png]]
  
[[Image:Iterator.gif]]
 
  
 
== Code Samples ==
 
== Code Samples ==
  
The following are samples of code from C#, Java, Python, and PERL, displaying how they implement iterators.
+
The following are samples of code from C#, Java, Python, and PERL, displaying how they use their implicit iterators.
 +
 
  
 
=== C# ===
 
=== C# ===
Line 30: Line 38:
 
  foreach (Value v in list)
 
  foreach (Value v in list)
 
     Console.WriteLine(v);
 
     Console.WriteLine(v);
 +
  
 
=== Java ===
 
=== Java ===
Line 36: Line 45:
 
  for (Value v : list)
 
  for (Value v : list)
 
     System.out.print(v);
 
     System.out.print(v);
 +
  
 
=== Python ===
 
=== Python ===
Line 42: Line 52:
 
  for Value in List:
 
  for Value in List:
 
     print Value
 
     print Value
 +
  
 
=== PERL ===
 
=== PERL ===
Line 48: Line 59:
 
  foreach $val (@list) {
 
  foreach $val (@list) {
 
     print "$val\n";
 
     print "$val\n";
}
+
}
 +
 
 +
== Examples ==
 +
 
 +
=== C++ ===
 +
 
 +
This is a code snippet from a file called [http://www.google.com/codesearch?hl=en&q=show:3faa7gjspWs:MYoIVGFTT9Q:S6XmQxn_Gd8&sa=N&ct=rd&cs_p=http://gentoo.osuosl.org/distfiles/gnome-vfsmm-1.3.5.tar.gz&cs_f=gnome-vfsmm-1.3.5/libgnomevfs/libgnomevfsmm/transfer.cc transfer.cc], created by the Gnome VFS Development Team. This particular function, transfer_list, transfers a list of URI's from the source list to the target list. The source code repository can be found [http://www.google.com/codesearch?hl=en&q=show:7egWPqDRuQg:6mN6dZ6BKgU&sa=N&ct=rdp&cs_p=http://gentoo.osuosl.org/distfiles/gnome-vfsmm-1.3.5.tar.gz here].
 +
 
 +
void transfer_list(const Glib::StringArrayHandle& source_uri_list, const Glib::StringArrayHandle& target_uri_list,
 +
      TransferOptions options,
 +
      ErrorMode error_mode,
 +
      OverwriteMode overwrite_mode,
 +
      const SlotProgress& slot)
 +
{
 +
  typedef std::list< Glib::RefPtr<Uri> > uri_list;
 +
  uri_list sources, targets;
 +
 +
  //Build lists of RefPtr<Uri>s from the strings:
 +
  Glib::StringArrayHandle::const_iterator iter_target = target_uri_list.begin();
 +
  for(Glib::StringArrayHandle::const_iterator iter = source_uri_list.begin(); iter != source_uri_list.end(); ++iter)
 +
  {
 +
    if(iter_target != target_uri_list.end())
 +
    {
 +
      sources.push_back( Uri::create(*iter) );
 +
      targets.push_back( Uri::create(*iter_target) );
 +
      iter_target++;
 +
    }
 +
  }
 +
  transfer_list_uris(sources, targets, options, error_mode, overwrite_mode, slot);
 +
}
 +
 
 +
 
 +
 
 +
== References ==
 +
 
 +
* [http://en.wikipedia.org/wiki/Iterator Wikipedia entry on Iterator]
 +
* [http://thor.info.uaic.ro/ Universitatea Alexandru Ioan Cuza]
 +
* [http://www.google.com/codesearch?hl=en&q=show:7egWPqDRuQg:6mN6dZ6BKgU&sa=N&ct=rdp&cs_p=http://gentoo.osuosl.org/distfiles/gnome-vfsmm-1.3.5.tar.gz Gnome VFS Development Source Code]

Latest revision as of 10:53, 21 January 2007

An Iterator provides the user with a way to access the elements of some kind of list sequentially, while keeping the elements of that list from exposing their underlying representation.

Introduction to Iterators

An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection (current, and current.next). Depending on the language the iterator is implemented in, other functionality may be added to the iterator object, such as remove and update and so on.

Iterator ties together the object-oriented programming principles known as encapsulation and polymorphism. Using an iterator, you can manipulate the objects in a collection without explicitly knowing how the collection is implemented or what the collection is made up of (Different types of objects perhaps?). An iterator provides an interface to different iteration implementations, which contain the details of how to manipulate a specific collection, including which items in the collection to show (filtering) and in what order (sorting).

An easy way to think of iterators, is to also think of, Lists, Linked Lists, Binary Trees, and Hash Tables, because they operate very much in the same way that iterators do.


Implicit Iterators

Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object. Some of these language include:

  • C#
  • Java (After 5.0)
  • Python
  • PERL


UML

Design class diagram in UML of the Iterator Pattern being used in a system.

Iterator1.png


Code Samples

The following are samples of code from C#, Java, Python, and PERL, displaying how they use their implicit iterators.


C#

// C#, implicit iteration
foreach (Value v in list)
   Console.WriteLine(v);


Java

// Java, J2SE 5.0, implicit iteration
for (Value v : list)
   System.out.print(v);


Python

# Python, implicit iteration
for Value in List:
   print Value


PERL

# Perl, implicit iteration
foreach $val (@list) {
   print "$val\n";
}

Examples

C++

This is a code snippet from a file called transfer.cc, created by the Gnome VFS Development Team. This particular function, transfer_list, transfers a list of URI's from the source list to the target list. The source code repository can be found here.

void transfer_list(const Glib::StringArrayHandle& source_uri_list, const Glib::StringArrayHandle& target_uri_list,
     TransferOptions options,
     ErrorMode error_mode,
     OverwriteMode overwrite_mode,
     const SlotProgress& slot)
{
 typedef std::list< Glib::RefPtr<Uri> > uri_list;
 uri_list sources, targets;

 //Build lists of RefPtr<Uri>s from the strings:
 Glib::StringArrayHandle::const_iterator iter_target = target_uri_list.begin();
 for(Glib::StringArrayHandle::const_iterator iter = source_uri_list.begin(); iter != source_uri_list.end(); ++iter)
 {
   if(iter_target != target_uri_list.end())
   {
     sources.push_back( Uri::create(*iter) );
     targets.push_back( Uri::create(*iter_target) );
     iter_target++;
   }
 }
 transfer_list_uris(sources, targets, options, error_mode, overwrite_mode, slot);
}


References