Re: Efficiently Extracting Identical Values From A List/Array
From: Ivan Vecerina (NOT_VALID_please_use_contact_webform_at_vecerina.com)
Date: 02/21/05
- Next message: Cy Edmunds: "Re: Exception Handling and Refactoring Question ..."
- Previous message: Karl Heinz Buchegger: "Re: Efficiently Extracting Identical Values From A List/Array"
- In reply to: Adam Hartshorne: "Re: Efficiently Extracting Identical Values From A List/Array"
- Next in thread: Karl Heinz Buchegger: "Re: Efficiently Extracting Identical Values From A List/Array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 21 Feb 2005 17:58:01 +0100
"Adam Hartshorne" <oracle3001@yahoo.com> wrote in message
news:cvd1rt$2tu$1@wisteria.csv.warwick.ac.uk...
> Maybe I'm missing something, but using this way
>
> > An "easy" way would be to use:
> > std::multimap< int/*nodeIndex*/, std::vector<int/*arrayIndex*/> >
> myList;
NB: I actually meant to write std::map< .... >
> > // for each index:
> > myList[aNodeIndex].push_back( anArrayIndex );
>
> will give me the list of lists, but I only want to consider those nodes
> which are mentioned multiple times in the storage array. The above will
> form me the list of lists, based upon the node indices and for each of
> those a list of array indices.
>
> I would then have to search / sort the whole MyList to isolate the
> elements in the new MyList that had multiple values stored.
> Is that correct?
Yes: sorting first tends to be the fastest way to find
identical values in a list.
This said, in your case, aNodeIndex values are in a know 0-based
interval. Because of that, you could probably use a faster approach:
// initial map filled with -1 to say no ArrayIndex points to that node
std::vector<int> nodeToFirstInd( maxNodeIndex, -1 );
// this will store only nodes with multiple indices
std::map< int/*nodeIndex*/, std::vector<int/*arrayIndex*/ > > multiLinked;
for(....)// for each arrayIndex, nodeIndex pair:
{
if( nodeToFirstInd[nodeIndex]==-1 )
nodeToFirstInd[nodeIndex] = arrayIndex; // mark node as 'used'
else {
std::vector<int/*arrayIndex*/ >& list = multiLinked[nodeIndex];
if( list.empty() ) // put the initial item in
list.push_back( nodeToFirstInd[nodeIndex] )
list.push_back( arrayIndex ); // add the new value index
}
}
// now multiLinked contains what you want
Sorry the code samples are a mess - just written in a rush.
I hope it is understandable and helpful, though.
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
- Next message: Cy Edmunds: "Re: Exception Handling and Refactoring Question ..."
- Previous message: Karl Heinz Buchegger: "Re: Efficiently Extracting Identical Values From A List/Array"
- In reply to: Adam Hartshorne: "Re: Efficiently Extracting Identical Values From A List/Array"
- Next in thread: Karl Heinz Buchegger: "Re: Efficiently Extracting Identical Values From A List/Array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|