Let us say that I have a 2D matrix, given by vector<vector<double>> matrix
, and that matrix
has already been initialized to have R
rows and C
columns.
There is also a list-of-coordinates (made up of say, N
(x,y)
pairs), that we are going to process, such that for each co-ordinate, we get a mapping to a particular row (r
) and column (c
) in our matrix. So, we basically have [r, c] = f(x,y)
. The particularities of the mapping function f
are not important. However, what we want to do, is keep track of the rows r
and columns c
that are used, by inserting them into another list, called list-of-indicies.
The problem is that, I do not want to keep adding the same r
and c
to the list if that (r,c)
pair already exists in that list. The brute force method would be to simply scan the entire list-of-indicies every time I want to check, but that is going to be very time consuming.
For example, if we have the co-ordinate (x=4, y=5), this yields (r=2, c=6). So, we now add (r=2, c=6) to the list-of-indicies. Now we get a new point, given by (x=-2, y=10). This also ends up falling under (r=2, c=6). However, since I have already added (r=2, c=6) to my list, I do not want to add it again! But without doing a brute-force scan of the list-of-indicies, is there a better way?