473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

from vector of pairs to a map

Hopefully I'm not asking too much here, nontheless consider the test
source:

#include <vector>
#include <utility>

using std::vector;
using std::pair;

#define INVALID_ID -1

struct lsp_input_def
{
int segment;
lsp_input_def(v oid):
segment(INVALID _ID)
{
}
};

typedef vector<int> proccessor_vec;
typedef pair< unsigned int, proccessor_vec > segment_proc_pa ir;
typedef vector< segment_proc_pa ir > vec_pair;

// Transform the input to the output in sorted form.
void Transform(lsp_i nput_def* pStruct, unsigned int waveformCount,
unsigned int procCount, vec_pair& rsVect)
{
rsVect.resize(w aveformCount);
size_t curOff = waveformCount * procCount;
unsigned int i = waveformCount;
while(i--)
{
rsVect[i].first = i;
unsigned int j = procCount;
while(j--)
{
const lsp_input_def& curStruct = pStruct[--curOff];
if(curStruct.se gment != INVALID_ID) // ID number is valid. Add
it to the vector.
rsVect[curStruct.segme nt].second.push_ba ck(j);
}
}
}

int main()
{
typedef unsigned int uint_type;
uint_type const max_segments = 7;
uint_type const max_processors = 24;

lsp_input_def wf[max_segments][max_processors];

wf[0][0].segment = 1; // element first is the segment / element
second processor
wf[1][0].segment = 0;
wf[2][0].segment = 3;
//wf[3][0].segment = 2;
//wf[0][1].segment = 3;

wf[0][1].segment = 1;
//wf[1][1].segment = 2;
wf[1][1].segment = 0;

// Transform the input to the output.
vec_pair sampleVect;
Transform((lsp_ input_def*)wf, max_segments, max_processors,
sampleVect);

printf(" segment processor\n");
printf("-------------------------------\n");
for(size_t i = 0; i < sampleVect.size (); i++)
{
proccessor_vec& curVect = sampleVect[i].second;
if(curVect.size ())
{
printf("\t%i\t" , sampleVect[i].first);
printf("%i", curVect[0]);
for(size_t j = 1; j < curVect.size(); j++)
printf(", %i", curVect[j]);
printf("\n");
}
}

printf("\n");

return 0;
}

I'm trying to work my way through understanding how I could achieve the
same results using a map:

typedef std::map< unsigned int, proccessor_vec > id_vec_map;
Source snippet appreaciated.

Thanks

Jan 17 '06 #1
5 2581
Yikes... I tried for about a half hour but gave up on deciphering this
spaghetti. No question that you can get the job done using std::map,
but without a clear specification of the intended behavior, it's hard
to provide more specific help. Your code is clearly influenced by some
very dated C idioms -- I'd recommend reading up on more modern C++
practices, and just getting exposure to code that uses the STL as
intended. If you can manage the paradigm shift, you'll find that
problems like the one you're posting become much more approachable, or
even moot.

Luke

Jan 17 '06 #2

"ma740988" <ma******@gmail .com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hopefully I'm not asking too much here, nontheless consider the test
source:

#include <vector>
#include <utility>

using std::vector;
using std::pair;

#define INVALID_ID -1

struct lsp_input_def
{
int segment;
lsp_input_def(v oid):
segment(INVALID _ID)
{
}
};

typedef vector<int> proccessor_vec;
typedef pair< unsigned int, proccessor_vec > segment_proc_pa ir;
typedef vector< segment_proc_pa ir > vec_pair;

// Transform the input to the output in sorted form.
void Transform(lsp_i nput_def* pStruct, unsigned int waveformCount,
unsigned int procCount, vec_pair& rsVect)
{
rsVect.resize(w aveformCount);
size_t curOff = waveformCount * procCount;
unsigned int i = waveformCount;
while(i--)
{
rsVect[i].first = i;
unsigned int j = procCount;
while(j--)
{
const lsp_input_def& curStruct = pStruct[--curOff];
if(curStruct.se gment != INVALID_ID) // ID number is valid. Add
it to the vector.
rsVect[curStruct.segme nt].second.push_ba ck(j);
}
}
}

int main()
{
typedef unsigned int uint_type;
uint_type const max_segments = 7;
uint_type const max_processors = 24;

lsp_input_def wf[max_segments][max_processors];

wf[0][0].segment = 1; // element first is the segment / element
second processor
wf[1][0].segment = 0;
wf[2][0].segment = 3;
//wf[3][0].segment = 2;
//wf[0][1].segment = 3;

wf[0][1].segment = 1;
//wf[1][1].segment = 2;
wf[1][1].segment = 0;

// Transform the input to the output.
vec_pair sampleVect;
Transform((lsp_ input_def*)wf, max_segments, max_processors,
sampleVect);

printf(" segment processor\n");
printf("-------------------------------\n");
for(size_t i = 0; i < sampleVect.size (); i++)
{
proccessor_vec& curVect = sampleVect[i].second;
if(curVect.size ())
{
printf("\t%i\t" , sampleVect[i].first);
printf("%i", curVect[0]);
for(size_t j = 1; j < curVect.size(); j++)
printf(", %i", curVect[j]);
printf("\n");
}
}

printf("\n");

return 0;
}

I'm trying to work my way through understanding how I could achieve the
same results using a map:

typedef std::map< unsigned int, proccessor_vec > id_vec_map;
Source snippet appreaciated.


Rather than letting us try to untangle that to see what it
does (or is supposed to do), and possibly/probably misunderstand
your meaning, I suggest you state your objective in English.
Include small code snippets if you feel they help clarify.

-Mike
Jan 17 '06 #3

ma740988 wrote:
Hopefully I'm not asking too much here, nontheless consider the test
source:

#include <vector>
#include <utility>

using std::vector;
using std::pair;

#define INVALID_ID -1
Not good to do that. Better to put it here:
struct lsp_input_def
{
const int INVALID_ID = -1; // here
int segment;
lsp_input_def() : // don't put void inside the brackets
segment(INVALID _ID)
{
}
};
(and would be better not to use all caps).
typedef vector<int> proccessor_vec;
typedef pair< unsigned int, proccessor_vec > segment_proc_pa ir;
typedef vector< segment_proc_pa ir > vec_pair;

// Transform the input to the output in sorted form.
void Transform(lsp_i nput_def* pStruct, unsigned int waveformCount,
unsigned int procCount, vec_pair& rsVect)
{
rsVect.resize(w aveformCount);
size_t curOff = waveformCount * procCount;
unsigned int i = waveformCount;
while(i--) // will loop from i-1 to 1 (will never populate rsVect[0] )
{
rsVect[i].first = i;
unsigned int j = procCount;
while(j--) // see comment above
{ const lsp_input_def& curStruct = pStruct[--curOff]; // notable not modifying pStruct // or what it points to. Therefore parameter above should be const
lsp_input_def * ...
if(curStruct.se gment != INVALID_ID) // ID number is valid. Add
it to the vector.
// now I made INVALID_ID part of the class but we could have a member
function that
// returns bool to determine whether our curStruct is valid.
rsVect[curStruct.segme nt].second.push_ba ck(j);
}
}
}


I'm going to give up at this point.

Jan 17 '06 #4

Howdy, Mike,
I'm going to deviate from my initial post here for a minute. Here's
my 'dilema' in a nutshell. I've got 24 sources ( call them 0 .. 23 )
sending me ( call me 'X' ) data. The data from each source has
what's called an 'id' (call it segment id) assosciated with it.
There's two id's ( 0 and 1 ). X will receive said from all sources and
perform some processing on them. X knows (via a structure that was
sent to it) that the processing order is 0 and 1. i.e process segment
0 first followed by segment 1.
So on X, I have a table that tells me

segment what_to_do
0 IFFT
1 FFT
The trouble on X is all the 'required house keeping that goes along
with receipt of the segments' from the 24 sources. Having said that I
need to track three things.
1. The size of the segment per source
2. The address where I store the segment - for _each_ source on X
3. A count parameter to keep track of things.

In it's simplest form. Lets assume source 0 transmits segment 0, with
a size of 0x10000; Upon receipt of the segment (i.e 0) the code
executing on "X' will. Increment the count parameter associated for
_that_ source (i.e source 0), store the size of data from source 0 and
the offset in memory where I stored source 0's data.

Now do that for the remainder. If the count == 24, then X has
received results from every source - for segment 0. The X will do an
IFFT on the data. Once 'X' is complete with segment 0. X will
'request' segment 1 from the sources. I'm executing in a
multi-threaded environment but enviornment 'details' aside, that's it
in a nutshell.

Now here's where it gets tricky for me - the design aspect of this.
Pictorially my thoughts were a map where first would be the
'what_to_do', second is a struct that entails - segment id, sources,
source counts, source data size, offset in memory. So now:

__ what_to_do_ segment_id
IFFT 0
0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10 .... 24 -- (24) sources
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, ..... 0 -- (24) counts ( zero initially )

-- (24) data size - this we'll get from
every source

-- address in memory - this part is tricky
but doable.

So a map perhaps:

enum algo { IFFT, FFT };
struct holder {
unsigned int segment_id;
unsigned int source;
unsigned int count;
unsigned int data_size;
unsigned char* ptr_addr_in_mem ;
};

typedef std::vector<hol der> holder_vec;
typedef std::map < unsigned int, holder_vec > the_map;
^ ^
| |
what_to_do here's all the
information

Trouble is, something just doesn't seem right about the use of map
here. This seem like I need a better design which will perhaps include
a class. Trouble is, does this require for me to create 24 instances
of said class or ..... Confused. Your thoughts!
I chose the map because I thought perhaps I could iterate through map
second - check count. Count 24, yes. Everything I need to do is in
the remainder of the struct. 'what_to_do' is the first element of the
map.

Thanks in advance

Jan 17 '06 #5

ma740988 wrote:
Howdy, Mike,
I'm going to deviate from my initial post here for a minute. Here's
my 'dilema' in a nutshell.
Far from deviating, this is the correct way to go. Have a look at the
spec.

I've got 24 sources ( call them 0 .. 23 ) sending me ( call me 'X' ) data. The data from each source has
what's called an 'id' (call it segment id) assosciated with it.
Now break it down a bit at a time. You have multiple sources that do
things. A "source" is a class (it has attributes and methods). You have
multiple ones so that suggests some kind of collection. The only
question is choosing which collection to use.

If they are numbered 0 to 23 then your best bet is vector.
There's two id's ( 0 and 1 ). X will receive said from all sources and
perform some processing on them. X knows (via a structure that was
sent to it) that the processing order is 0 and 1. i.e process segment
0 first followed by segment 1. So on X, I have a table that tells me

segment what_to_do
0 IFFT
1 FFT
X "knows" that there are two so you could have some hard-coded table.
If you are not certain that there will always be two then you might
want some kind of abstract base class indicating an "id" and have a
virtual method "whatToDo" (probably not exactly with that name). Might
be an idea for you to do that even with 2. Your Id0 will implement
"whatToDo() " with IFFT, and Id1 will implement with FFT.
The trouble on X is all the 'required house keeping that goes along
with receipt of the segments' from the 24 sources. Having said that I
need to track three things.
1. The size of the segment per source
2. The address where I store the segment - for _each_ source on X
3. A count parameter to keep track of things.

In it's simplest form. Lets assume source 0 transmits segment 0, with
a size of 0x10000; Upon receipt of the segment (i.e 0) the code
executing on "X' will. Increment the count parameter associated for
_that_ source (i.e source 0), store the size of data from source 0 and
the offset in memory where I stored source 0's data.


Put that in the source class. Remember that the source class you have
on your side is a source from your perspective, i.e. how X uses a
source.

That's probably enough to be getting on with for now.

As for using a map, a map is used when:
1. You have a collection
2. You need to look up by an id.

However when the ids are numeric and not too sparse you should consider
using a vector.

Incidentally, although vector is copyable, it is recommended to avoid
copying them too much because it is generally a major overhead. You
could use shared_ptr<vect or> or shared_ptr<Wrap per> where Wrapper is a
class that contains a vector (and probably has some other methods as
well). shared_ptr is part of the boost library and is also part of
std::tr1. Although they are not standard they are pretty much in common
use and you can download the source free. (It is likely that a version
will be included in the next standard, thus it has already been
promoted to std::tr1).

Jan 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2015
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what I want is to create another vector, vector<int> vec2, which store all the first 8 bit heads of the integer in vec1, for the above example, 0x00000000 & 0xFF000000 ==> 0x00000000,
13
4617
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector is of size 2. How much memory can I save by storing my pointers in c++ arrays, rather than vectors.
5
12070
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
6
14894
by: ma740988 | last post by:
Oh what fun it is to get acclimated with the various containers and algorithms.. I'm trying to determine if I could use find/find_first_of algorithms to achieve the same object of a for loop when searching for the first element in a vector of pairs. So now: typedef std::pair<int, int > int_pair; typedef std::vector<int_pair> vec_int_pair; int main() { vec_int_pair p;
10
3222
by: Whybother | last post by:
I have this typedef map<__int64, int> Results_Map; __int64 is defined as 8 bytes ranging from -9,223,372,036.854,775,808 to 9,223,372,036.854,775,807 I have loaded it with approx 34 million hash keys and when the program is shutting down it takes forever (10+ mins) while its deleting the objects. The memory it uses according to task manager is varies from 500,000 k then
23
1960
by: sidney | last post by:
Hi all, I am trying to make a vector containing objects the have a reference member. However, as soon as I try to push_back an element into this vector, g++ balks at the fact that it needs to instantiate an operator= on the class containing the reference members (see below for what I try to do). #include <vector>
13
2080
by: arnuld | last post by:
this is the code: ------------------------------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Pair { std::string name;
17
3183
by: mosfet | last post by:
Could someone tell me why it's considered as bad practice to inherit from STL container ? And when you want to customize a STL container, do you mean I need to write tons of code just to avoid to derive from it ?
1
1836
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have looked into using the instream iterator to initialize the vector of pairs of floats. Any thoughts would be appreciated. given the test.txt file which contains:
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.