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 » WHY? "Index:: and ArrayIndex::operator[]" returns const T&
WHY? "Index:: and ArrayIndex::operator[]" returns const T& [message #26296] Thu, 22 April 2010 12:08 Go to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
hi there,

the Index/ArrayIndex containers are pretty helpful. their current use compared to VectorMap/ArrayMap is as I understood as follows:

the VectorMap/ArrayMap offer the way to find an object's position inside the container via an object *independant* hash value (key).

the Index/ArrayIndex offer the way to find an object's position inside the container via an object *dependant*, own hash, computed typically over the "value" of an object. therefore objects used in Index/ArrayIndex Container either have to have an own GetHashValue() function, or the container needs to be supplied a HashFn class, which can compute hash values for the object (i.e) if one can not manipulate the class (not own sources..). Indexes are not meant to be used for LAARGE objects, since another objects "value" (for hashing) is used to retrieve the position of an object in Index (temp object creation topics)

thus Index variants are *NOT* replacements for Map container variants.

now a problem arises:

in the Map variants, one can change the key refered to an object.
the Index cant do it because objects provide the hash value themselves, thats why Index may not have a mutator operator[] (like the Maps) to return a changable object. >> i understand that Index/ArrayIndex are meant as immutable readonlys in that case.

But consider the case of Indexes, where we need the objects to be mutable, so we would need means to update the hash table. to still have it pointing to the correct object.

some additions would be to update the hash of an element in the Index/ArrayIndex, and ofcorse the mutator operator[]

Index.hpp:127
	void     SetKey(int i)                   { B::hashfn(key[i]); }

	T&       operator[](int i)               { return key[i]; }


opinions??
Re: WHY? "Index:: and ArrayIndex::operator[]" returns const T& [message #26308 is a reply to message #26296] Fri, 23 April 2010 10:25 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
kohait00 wrote on Thu, 22 April 2010 06:08


in the Map variants, one can change the key refered to an object.



Note "Index::Set" method.

	T&       operator[](int i)               { return key[i]; }


This is impossible (both for Index and *Map) - after setting the key, internal structure has to be updated based on the new key.

Note that for *Map, operator[] returns the *value*, not the key.

[Updated on: Fri, 23 April 2010 10:25]

Report message to a moderator

Re: WHY? "Index:: and ArrayIndex::operator[]" returns const T& [message #26335 is a reply to message #26308] Mon, 26 April 2010 09:29 Go to previous messageGo to next message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
yes, i understand your point, this is what i have written above. as for now, the Index is meant to be a container for *immutable* elements, if you want to modify the content of the container, you delete old elementand place a new element. but what if you want to update the container elements in place ? you'd need means to update the internal hash map..

BTW:

i think in any case, we'd need to more clearly outline what each container type is actually for..(in most cases at least), kind of a table... describing what they are used for in most day to day cases.

currently possible:

Vector - moveable elements random acces
Array - arbitrary elements random access
VectorMap - moveable elements hash access via a key
ArrayMap - arbitrary elements hash access via a key
Index - moveable elements hash access over element's value
ArrayIndex - arbitrary elements hash access over element's value

something in the sense of
http://www.cplusplus.com/reference/stl/
should be made for the upp containers, to make selection/comparison easy
Re: WHY? "Index:: and ArrayIndex::operator[]" returns const T& [message #26336 is a reply to message #26335] Mon, 26 April 2010 10:12 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Like this?

http://www.ultimatepp.org/srcdoc$Core$NTL$en-us.html

Mirek
Re: WHY? "Index:: and ArrayIndex::operator[]" returns const T& [message #26349 is a reply to message #26336] Mon, 26 April 2010 17:41 Go to previous message
kohait00 is currently offline  kohait00
Messages: 939
Registered: July 2009
Location: Germany
Experienced Contributor
yes, something like that Smile thanks
Previous Topic: Strange issue with VectorMap<String, String>
Next Topic: FEATURE: T* ArrayIndex::Detach(int i) AND mini fix
Goto Forum:
  


Current Time: Mon Apr 29 02:15:24 CEST 2024

Total time taken to generate the page: 1.00066 seconds