Few days ago, i saw a piece of a geoprocessing algorithm code. This algorithm get a vector of points, each point with two spatial coordinates: (x,y), and calculate the euclidean distancebetween of each point with the whole points of the vector. The result is a beatifull matrix of distance between each points.
The problem was the follow: this algorithm use "Iterators" on the vector, as follows:
but it has a problem in optimization terms, because the distance between point #1 and point #2 is the same that between point #2 and point #1, i.e.,an optimized algorithm only need compute the superior diagonal of the matrix, beacuse the matrix of distances is simetric.
In above sample, with optimized algorithm, we need 3 iterations on the vector and symmetrize the matrix; in the other hand without optimization we need 9 iterations: n^2.
If we assume that the objects in the vector are unique, the best solution in order of do not lose the semantic of Iterators is the use the follows code: