Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » VectorMap inside VectorMap
VectorMap inside VectorMap [message #19924] Mon, 02 February 2009 23:08 Go to next message
White_Owl is currently offline  White_Owl
Messages: 27
Registered: January 2009
Location: New York
Promising Member
What is wrong with this code?
#include <Core/Core.h>

using namespace Upp;

class mycontainer: Moveable<mycontainer> {
public:
	VectorMap<String, String> data;
};

VectorMap<String, mycontainer> vm;

CONSOLE_APP_MAIN
{
	mycontainer ca = vm.GetAdd("First");
	ca.data.FindPut("first-first", "aa");
	ca.data.FindPut("first-second", "bb");
	
	mycontainer cb = vm.GetAdd("First");
	cb.data.FindPut("first-first", "cc"); // run-time error
	cb.data.FindPut("first-second", "dd");
}
Why do I get a run-time error in debug mode?
---------------------------
Fatal error
---------------------------
Assertion failed in C:/upp/uppsrc/Core/Vcont.h, line 18
Broken pick semantics

---------------------------
OK   
---------------------------

Re: VectorMap inside VectorMap [message #19925 is a reply to message #19924] Tue, 03 February 2009 01:17 Go to previous messageGo to next message
kodos is currently offline  kodos
Messages: 111
Registered: March 2008
Experienced Member
mycontainer ca = vm.GetAdd("First");

With this line you "pick" the content of the returned mycontainer. You probably want to use a reference:
mycontainer &ca = vm.GetAdd("First");
Re: VectorMap inside VectorMap [message #19932 is a reply to message #19924] Tue, 03 February 2009 17:24 Go to previous messageGo to next message
White_Owl is currently offline  White_Owl
Messages: 27
Registered: January 2009
Location: New York
Promising Member
I thought for "pick" you have to use GetAddPick() method?

Anyway, I am looking for a way to redefine items in my dictionaries. And both dictionaries (inner and outer) should have unique sets of keys.
Re: VectorMap inside VectorMap [message #19933 is a reply to message #19924] Tue, 03 February 2009 17:58 Go to previous messageGo to next message
White_Owl is currently offline  White_Owl
Messages: 27
Registered: January 2009
Location: New York
Promising Member
Ok, here is the code which does exactly what I want:
#include <Core/Core.h>

using namespace Upp;

class mycontainer: Moveable<mycontainer> {
public:
	VectorMap<String, String> data;
};

VectorMap<String, mycontainer> vm;

void define_word(const String& outer_key, const String& inner_key, const String& value) {
	mycontainer *ca = vm.FindPtr(outer_key);
	if(!ca)
		ca = &(vm.Add(outer_key));

	if(ca->data.Find(inner_key)>=0)
		ca->data.RemoveKey(inner_key);
	ca->data.Add(inner_key, value);
}

CONSOLE_APP_MAIN
{
	define_word("First", "first-first", "aa");
	puts(vm.Get("First").data.Get("first-first"));
	puts("Sizeof(vm)=" + FormatInt(vm.GetCount()));
	puts("Sizeof(vm{First}.data)=" + FormatInt(vm.Get("First").data.GetCount()));

	define_word("First", "first-first", "bb");
	puts(vm.Get("First").data.Get("first-first"));
	puts("Sizeof(vm)=" + FormatInt(vm.GetCount()));
	puts("Sizeof(vm{First}.data)=" + FormatInt(vm.Get("First").data.GetCount()));
}
I think GetAdd()/FindPut and other "double" methods already have all this checking and removing old values. But I can not find which one of them actually does it without invalidating objects inside the container.
Re: VectorMap inside VectorMap [message #19934 is a reply to message #19932] Tue, 03 February 2009 18:12 Go to previous messageGo to next message
kodos is currently offline  kodos
Messages: 111
Registered: March 2008
Experienced Member
White_Owl wrote on Tue, 03 February 2009 17:24

I thought for "pick" you have to use GetAddPick() method?

The methods with "pick" in the name, pick the argument that you pass the method. But in your first example you use the assignment operator which "picks" the value from the right to the left variable.

I think you want something like this:
#include <Core/Core.h>

using namespace Upp;

class mycontainer: Moveable<mycontainer> {
public:
	VectorMap<String, String> data;
};

VectorMap<String, mycontainer> vm;

CONSOLE_APP_MAIN
{
	mycontainer &ca = vm.GetAdd("First");
	ca.data.FindPut("first-first", "aa");
	ca.data.FindPut("first-second", "bb");
	
	mycontainer &cb = vm.GetAdd("First");
	cb.data.FindPut("first-first", "cc"); // run-time error
	cb.data.FindPut("first-second", "dd");
}


Like that you don't call the assignment operator but you just use the reference that get's returned from GetAdd. So there is no picking.
Re: VectorMap inside VectorMap [message #19939 is a reply to message #19934] Tue, 03 February 2009 19:33 Go to previous messageGo to next message
White_Owl is currently offline  White_Owl
Messages: 27
Registered: January 2009
Location: New York
Promising Member
Ok. Right now I have this:
void define_word(const String& outer_key, const String& inner_key, const String& value) {
	mycontainer &ca = vm.GetAdd(outer_key);
	ca.data.GetAdd(inner_key) = value;
}
That works exactly as I want.
I'm considering this problem solved.
Re: VectorMap inside VectorMap [message #19981 is a reply to message #19939] Sat, 07 February 2009 15:03 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
You can save one line:

vm.GetAdd(outer_key).data.GetAdd(inner_key) = value;


Mirek
Previous Topic: Quick bi-array
Next Topic: Core package build flags
Goto Forum:
  


Current Time: Fri Apr 19 11:33:54 CEST 2024

Total time taken to generate the page: 0.02363 seconds