473,795 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing map keys (no reordering of keys)

Hello world,

I currently have implemented a sparse array needed by a class as a
map<int, boost::shared_p tr<my_type.
However, one of the derived classes needs to periodically "tighten"
the sparse array (i.e. make it non-sparse). For example:

a[0] = 1
a[4] = 2
a[42] = 54

=>

a[0] = 1
a[1] = 2
a[2] = 54
I'm currently somewhat puzzled at how I would rearrange the keys. One
possible solution I thought would be:
/*where inv is the member containing the map*/
int lim = inv.size();
map<int, boost::shared_p tr<my_type::ite rator it = inv.begin();
for( int i = 0; i < lim; ++i, ++it){
inv[i] = it->second;
}
/*delete everything after the last*/
inv.erase(it, inv.end());

However, this does create some new objects each time I need to
tighten; granted that my current application uses only smart pointers,
it's not a big deal, but suppose that a future map with similar
requirements is needed, where the value is no longer a pointer but a
reasonably costly object? Is there a better way of implementing this?

Nov 28 '07 #1
3 1492
alan wrote:
Hello world,

I currently have implemented a sparse array needed by a class as a
map<int, boost::shared_p tr<my_type.
However, one of the derived classes needs to periodically "tighten"
the sparse array (i.e. make it non-sparse). For example:

a[0] = 1
a[4] = 2
a[42] = 54

=>

a[0] = 1
a[1] = 2
a[2] = 54
I'm currently somewhat puzzled at how I would rearrange the keys. One
possible solution I thought would be:
/*where inv is the member containing the map*/
int lim = inv.size();
map<int, boost::shared_p tr<my_type::ite rator it = inv.begin();
for( int i = 0; i < lim; ++i, ++it){
inv[i] = it->second;
}
/*delete everything after the last*/
inv.erase(it, inv.end());
I would probably copy everything into a new map and then simply swap
the new one with the old one (and let the new one be disposed of).
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 28 '07 #2
On Nov 28, 10:57 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
alan wrote:
Hello world,
I currently have implemented a sparse array needed by a class as a
map<int, boost::shared_p tr<my_type.
However, one of the derived classes needs to periodically "tighten"
the sparse array (i.e. make it non-sparse). For example:
a[0] = 1
a[4] = 2
a[42] = 54
=>
a[0] = 1
a[1] = 2
a[2] = 54
I'm currently somewhat puzzled at how I would rearrange the keys. One
possible solution I thought would be:
/*where inv is the member containing the map*/
int lim = inv.size();
map<int, boost::shared_p tr<my_type::ite rator it = inv.begin();
for( int i = 0; i < lim; ++i, ++it){
inv[i] = it->second;
}
/*delete everything after the last*/
inv.erase(it, inv.end());

I would probably copy everything into a new map and then simply swap
the new one with the old one (and let the new one be disposed of).
Hmm. I thought of that too. However, suppose for the sake of
argument that the copying of the value objects are nontrivial (even if
arguably, when object copying is nontrivial, you probably really want
to use pointers of some kind). Is there some way to avoid the copy?

It's an admittedly academic question, of course, since a pointer of
some sort, or at least a wrapper object which copies the
implementation object only on mutation would be better.
>
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 28 '07 #3
alan wrote:
On Nov 28, 10:57 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>alan wrote:
[..]
>>I'm currently somewhat puzzled at how I would rearrange the keys.
One possible solution I thought would be:
/*where inv is the member containing the map*/
int lim = inv.size();
map<int, boost::shared_p tr<my_type::ite rator it = inv.begin();
for( int i = 0; i < lim; ++i, ++it){
inv[i] = it->second;
}
/*delete everything after the last*/
inv.erase(i t, inv.end());

I would probably copy everything into a new map and then simply swap
the new one with the old one (and let the new one be disposed of).
Hmm. I thought of that too. However, suppose for the sake of
argument that the copying of the value objects are nontrivial (even if
arguably, when object copying is nontrivial, you probably really want
to use pointers of some kind). Is there some way to avoid the copy?
Store pointers to begin with. Or, you could actually introduce some
kind of a wrapper map (instead of destroying the initial one), which
would store pointers to the objects in the other map instead of copying
the values. When done, destroy both maps.
It's an admittedly academic question, of course, since a pointer of
some sort, or at least a wrapper object which copies the
implementation object only on mutation would be better.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 28 '07 #4

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

Similar topics

1
2240
by: raj | last post by:
how do i display primary keys, foreign keys that a table has.
1
1434
by: sandeep Kanwal | last post by:
Hi, I don't know if this is the right forum for this.. but let me know the right forum also if this one is wrong...I have written a small software utility which I am ready to sell now. I want to put some licence keys/serial keys information into it. I don't know how do I proceed with that. Any ideas 3rd party softwares ? Thanks kanwal
4
15221
by: pixel | last post by:
hi i just discovered something strange when i use in ProcessCmdKey this construction switch(keyData) { case Keys.Left: {
1
2415
by: news.microsoft.com | last post by:
Hi, How to load the list of keys enumeration name and value in a combo box. Thanks saran
5
1678
by: micklee74 | last post by:
hi in my code, i use dict(a) to make to "a" into a dictionary , "a" comes from user input, so my program does not know in the first place. Then say , it becomes a = { '-A' : 'value1' , '-B' : "value2" , "-C" : "value3" , '-D' : 'value4' } somewhere next in my code, i will check for these..:
14
3474
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys() will produce but it could also...
1
1878
by: Udi | last post by:
Hi, I have a user control derived from RichTextBox. I recently ported the control from 1.1 to 2.0. While everything worked fine on 1.1, for some reason it doesn't on 2.0. Any ideas why? The code: protected override void OnKeyDown(KeyEventArgs e) {
2
3324
by: Charles Law | last post by:
I'll kick myself when you tell me, but ... I have a user control on a form, and I want the user control to see the arrow keys when I press them. If I press just about any other key the control's KeyDown event is fired, but not when I press a direction key. I want to see them in the KeyDown event so that I can respond as soon as the key is depressed, and I want to detect when the key is held down so that I can perform an action...
3
4226
by: =?Utf-8?B?S1NP?= | last post by:
how can i put some text into these key by C# code ? In the old days you could do it by the PROMPT command i DOS but it seems NOT to work any more - the "change key" facility has disappeared. Can I do the change from C# so that it will stay changed when I start another program ? Best regards KSor, Denmark
0
1194
by: Menlalen | last post by:
K E Y S , A L I C I A M P 3 D O W N L O A D *Fallin 2001* 'Keys, Alicia - Fallin' (http://tinyurl.com/4zg3sr) (3:30 4.82Mb 192kbps) 'Keys, Alicia - Fallin (ft BustaRhymes and Rampage)' (http://tinyurl.com/47swaf) (4:15 5.86Mb 192kbps) *If I Ain't Got You 2004*
0
9522
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
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
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...
0
10002
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...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
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
6783
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
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.