It should be noted that some programming languages, such as Pascal, has
built-in set types. Although the actual implementation of sets in Pascal
is somewhat limited, it is still worthwhile to be mentioned. In C++, the
STL (standard template library) also include a template class called
set
.
The implementation of sets depends on the application. A set is sparse if
it contains a small number of elements out of a huge number of possible
elements. The implementation of sparse sets often use some kind of data
structures, such as linked lists. To speed up element look up (the
operator), a hash table may be used.
A set that is dense (containing most of the possible elements) can be implemented like a sparse one. However, for storage efficiency, dense sets can also be implemented by bitmaps. In a bitmap implementation, all elements are enumerated as integers. The enumeration position of an element indexes a bit position (in an huge array of bits) that reflects the presence of the element in a set. Set operations like union, intersection and difference are very efficient with a bitmap representation. However, the ``for ... in'' statement is less efficient compared to a hash table approach.
Copyright © 2006-09-27 by Tak Auyeung