1. Map Needs Merge

Anybody want to try their hand at a fast and efficient map:merge() function?

map:merge( map_one, map_two ) -- merge map_two into map_one (?) 

or something like that.

This needs to be in std/map.e. smile

new topic     » topic index » view message » categorize

2. Re: Map Needs Merge

euphoric said...

Anybody want to try their hand at a fast and efficient map:merge() function?

map:merge( map_one, map_two ) -- merge map_two into map_one (?) 

or something like that.

This needs to be in std/map.e. smile

Is this what you want?

map:copy( map_one, map_two ) 
delete( map_two ) 

From the docs... (emphasis mine)

docs said...

Returns:
If dest_map was not provided, an exact duplicate of source_map otherwise dest_map, which does not have to be empty, is returned with the new values copied from source_map, according to the put_operation value.

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: Map Needs Merge

ghaberek said...
euphoric said...

Anybody want to try their hand at a fast and efficient map:merge() function?

map:merge( map_one, map_two ) -- merge map_two into map_one (?) 

or something like that.

This needs to be in std/map.e. smile

Is this what you want?

map:copy( map_one, map_two ) 
delete( map_two ) 

From the docs... (emphasis mine)

docs said...

Returns:
If dest_map was not provided, an exact duplicate of source_map otherwise dest_map, which does not have to be empty, is returned with the new values copied from source_map, according to the put_operation value.

No, that is obviously NOT what I wanted. That is a copy and delete. I want a merge.

HAHAHAA! tongue

OK, OK. Sure. I'll take it.

But map:merge() is a much better name for merging two (or more) maps.

P.S. Your example deletes the "destination" map... So... tongue

new topic     » goto parent     » topic index » view message » categorize

4. Re: Map Needs Merge

The phix solution:

constant map1 = new_dict({{"key1","data1"}}), 
         map2 = new_dict({{"key2","data2"}}), 
         map3 = new_dict(map1) 
 
function merge(object key, object data, integer map3) 
    setd(key,data,map3) 
    return 1 
end function 
traverse_dict(routine_id("merge"),map3,map2) 
include builtins\map.e 
?{"map1:",pairs(map1)} 
?{"map2:",pairs(map2)} 
?{"map3:",pairs(map3)} 

output

{"map1:",{{"key1","data1"}}} 
{"map2:",{{"key2","data2"}}} 
{"map3:",{{"key1","data1"},{"key2","data2"}}} 

Obviously should the same key occur in map1 and map2, the data from map2 will overwrite the corresponding map1 value.

I have (just) added this as a second example to the traverse_dict() docs.

Note that other forms of merge are possible/may be required, for example if key is file size and data is a list of files of that size, the merge operation would probably want to concatenate rather than replace values, eg:

constant map1 = new_dict({{1234,{"this.txt"}}}), 
         map2 = new_dict({{1234,{"that.txt"}}}), 
         map3 = new_dict(map1) 
 
function merge(object key, object data, integer map3) 
    integer node = getd_index(key,map3) 
    if node=NULL then 
        setd(key,data,map3) 
    else 
        setd(key,getd_by_index(node,map3)&data,map3) 
    end if 
    return 1 
end function 
traverse_dict(routine_id("merge"),map3,map2) 
include builtins\map.e 
?{"map1:",pairs(map1)} 
?{"map2:",pairs(map2)} 
?{"map3:",pairs(map3)} 

output

{"map1:",{{1234,{"this.txt"}}}} 
{"map2:",{{1234,{"that.txt"}}}} 
{"map3:",{{1234,{"this.txt","that.txt"}}}} 

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu