Skip to content

add RangeMap.__mul__ and tests#86

Open
ghost wants to merge 1 commit intomasterfrom
unknown repository
Open

add RangeMap.__mul__ and tests#86
ghost wants to merge 1 commit intomasterfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Apr 6, 2018

Ability to take the product of two RangeMaps, merging their keys and creating tuples from their values.

def test_product():
	rm1 = RangeMap.from_iterable((
		(None, 10, 'a'),
		(10, 25, 'b'),
		))
	rm2 = RangeMap.from_iterable((
		(5, 15, 'c'),
		(15, 25, 'd'),
		(30, None, 'e'),
		))
	assert rm1 * rm2 == RangeMap.from_iterable((
		(None, 5, ('a', None)),
		(5, 10, ('a', 'c')),
		(10, 15, ('b', 'c')),
		(15, 25, ('b', 'd')),
		(30, None, (None, 'e')),
		))
	rm3 = RangeMap.from_iterable((
		(None, 0, 'f'),
		(0, 20, 'g'),
		(35, 40, 'h'),
		))
	assert rm1 * rm2 * rm3 == RangeMap.from_iterable((
		(None, 0, ('a', None, 'f')),
		(0, 5, ('a', None, 'g')),
		(5, 10, ('a', 'c', 'g')),
		(10, 15, ('b', 'c', 'g')),
		(15, 20, ('b', 'd', 'g')),
		(20, 25, ('b', 'd', None)),
		(30, 35, (None, 'e', None)),
		(35, 40, (None, 'e', 'h')),
		(40, None, (None, 'e', None)),
		))

Not sure if the implementation is the best. The class ValueTuple is used to merge tuples of values in products of more than two RangeMaps. Let me know about possibilities for improvement!

@coveralls
Copy link
Copy Markdown

coveralls commented Apr 6, 2018

Coverage Status

Coverage increased (+0.06%) to 98.023% when pulling d82fe03 on MareinK:rangemap-prod into 84db5bc on mlenzen:master.

@ghost ghost changed the title Rangemap prod add RangeMap.__mul__ and tests Apr 6, 2018
@mlenzen
Copy link
Copy Markdown
Owner

mlenzen commented Apr 14, 2018

This is an interesting idea. Regarding implementation, I've been thinking about making RangeMap return views of the underlying data instead of copies (#76). I think that implementation could also be used for this. Since the view would know how many RangeMaps were underlying when each tuple value is created, we wouldn't have to do the nesting and flattening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants