II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
NOTES ON DATA STRUCTURING 127<br />
each occasi<strong>on</strong>. A third method is to split the bitpattern into small parts, and<br />
use table lookup <strong>on</strong> each part, adding together the results.<br />
(5) The up and down operati<strong>on</strong>s can obviously be accomplished by right<br />
or left shifts.<br />
(6) The min of a set can be efficiently discovered by a standardise instruc-<br />
ti<strong>on</strong>, which automatically counts the number of shifts required to move the<br />
first <strong>on</strong>e-bit into the positi<strong>on</strong> next to the sign.<br />
(7) The for statement may also be emciently c<strong>on</strong>structed using standardi-<br />
sati<strong>on</strong>, masking off each <strong>on</strong>e-bit as it is reached.<br />
(8) The range operati<strong>on</strong> can be accomplished by two shifts, the first of<br />
which regenerates the sign bit.<br />
Thus when the cardinality of the domain type is not greater than the<br />
number of bits in the largest computer word to which logical and shift<br />
operati<strong>on</strong>s can be applied, all these operati<strong>on</strong>s can be carried out with great<br />
efficiency. If significantly more than <strong>on</strong>e such word is involved, it will usually<br />
pay to use selective updating operati<strong>on</strong>s rather than the normal result-<br />
producing operators. Furthermore, operati<strong>on</strong>s such as size and min can<br />
become rather inefficient, and it will often pay to store these values re-<br />
dundantly together with the set, and keep them up to date whenever the value<br />
of the set is updated, rather than recomputing them whenever they are<br />
required.<br />
When it is known that the cardinality of the base type is very large (perhaps<br />
even infinite) compared with the size of the typical set, the bitpattern repre-<br />
sentati<strong>on</strong> altogether loses its attracti<strong>on</strong>, since it no l<strong>on</strong>ger pays to store and<br />
operate up<strong>on</strong> large areas of zeroes. The treatment of such sparse sets is<br />
postp<strong>on</strong>ed to Secti<strong>on</strong> 10.<br />
7.3. EXAMPLE<br />
Problem: Write a program to c<strong>on</strong>struct a set<br />
primes:powerset 2.. N;<br />
c<strong>on</strong>taining all prime numbers in its base type.<br />
Use the method of Eratosthenes' sieve to avoid all multiplicati<strong>on</strong>s and<br />
divisi<strong>on</strong>s.<br />
The method of Eratosthenes is first to put all numbers in the "sieve" and<br />
repeat the following until the sieve is empty:<br />
Select and remove the smallest number remaining in the sieve (necessarily a<br />
prime), and then step through the sieve, removing all multiples of that<br />
number.