I have to programmatically distribute a set of items to some entities, but there are rules both on the items and on the entities like so:
Item one: 100 units, only entities from Foo
Item two: 200 units, no restrictions
Item three: 100 units, only entities that have Bar
Entity one: Only items that have Baz
Entity one hundred: No items that have Fubar
I only need to be pointed in the right direction, I’ll research and learn the suggested methods.
2
The most straightforward way is something like this:
for item in items
for entity in entities
if entity.allows(item) and item.allows(entity)
entity.associate(item)
break
If you have millions of items and entities, and strict performance requirements, then you’d have to get into creating indices for each of the criteria, like a list of all Foo
entities, etc. That’s going to add a lot of complexity though, so I wouldn’t do it unless absolutely necessary.
1