Way(Optimized) to Implement following Scenario : Split Object in a List<T>

Ashwin

Here, I need to split (clone) the original object based on the quantity. Whenever I am splitting an object, new BaseID is assigned to newly created Object. ( Please Bear with me)

What I would like to achieve:

Whenever an object (source) splits, a newly created object should be added very next to the source object (if source index is 0, then at index 1).

**This is how I am maintaining Object **

I am using a List to store the objects, that means newly created objects will be added at the end.

What is the Bottleneck

1) Unknown Source Index- Based on some filtering I am choosing the source and hence I don't know the index of the source object.

2) Avg Number of split I will perform - The Avg case will be 300 to 400 per second.

3) Size of the list - With current approach, I started with Object Count 300 (in a List), and after half processing, it has reached 6 Million.

For example: In input, I have the following list, note BaseID changes for each newly created object, Whereas Org_BaseID never changes and equals BaseID when the inputs are transformed into Objects.

The input list is ordered based on some fields(for now Say Date), so the newly split Object will have the same value as the Original Object for Order by fields.

Input

One of the examples of the split

Step1: Split Object1( BaseID: S1 having quantity 100) based on quantity, into say 80, 20. Please note Org_BaseID never changes, whereas New BaseID is assigned to the new object. Here is the resulting output.

Step 1

Step2:Split Object with BaseID: S5 based on quantity, say 15, 5. Here is resulting list.

Step 2

Step3:Split Object with BaseID: S1 based on Quantity, Say 70, 10.

Step 3

Step 4: If I order by the final List, I will get objects sorted based on Insertion order.

Step 4

Likewise, any Object can split any number of time given that quantity is greater than 0.

Approaches I Have tried:

1) Include Sort Order field, whenever a source object splits, update the sort order for all other objects. (If object with sort order 2 splits, 3 sort order will be assigned to the new object and hence update sort order of every object having sort order greater than 2 to current sort order value + 1), It is updating all list i.e O(n).

enter image description here

2) Maintain Dictionary with key As Org_BaseID, Instead of updating all list, update sort order for Objects with specific Org_baseID.

enter image description here

Is there is any other approach, which I can try?

Thanks in Advance, and appreciate all suggestions.

Ashwin

Approaches I tried

1) Allan S Approach- Insert an Object instead of adding.

  • It is a good approach for a smaller number of splits (up to 30,000), but as records are growing, it's started taking more time because of O(n) complexity.

I Tried New approach

-Instead of Inserting or Adding Split Object in the list (AVG 300 to 400 Add mentioned in the question), I Used Dictionary < String , Object > (to ensures O(1)) to track the splits.

How it works

  • For Every split, Add the Object in the dictionary (BaseID of source Object as key and value will be new Object). One Object will split only one for current loop so key will be unique.

  • Once splits over (One of the loops among Several), I will Loop through the main list and create a new list. For each Object in Main List, I will Add add it in the New list, and then will check is there any key in the dictionary for Given BaseID, if it's there then Add it in New list.

  • Using this approach, I will recreate List only once, and it is working fine for larger input (better than my Old Approach i.e updating list for every split).

enter image description here

I will try to use Linked List and will check the performance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to map the custom object with corresponding Task in the following scenario?

How can I use list comprehension in following scenario?

Implement a linked list program with the following functions

How to copy for the following scenario?

Don't understand cause of this AttributeError: 'list' object has no attribute 'split'

Persistence and Split brains Scenario

How to write scenario outline to capture list of object in a single argument?

Drools Business Central Workbench - Test Scenario: List subproperty on object

What pattern to use for the following scenario?

SQL Query - stuck on the following scenario:

How to get result in following scenario

How to deal with following scenario in QTP?

Extracting list of the split of a lmtree object

List Object has no Attribute Split

pandas split list like object

'list' object has no attribute 'split'

Is there a way to avoid the Cast<T>() if I already have the correct type in the following C# scenario?

How to implement list<T>::iterator?

How do I split up the following Python string in to a list of strings?

Split and Convert string to List<T>

Fill complex object property List<T> based on string.split condition

How to avoid duplicates in following SQL scenario

How to make the checkbox working properly in following scenario?

How to show the groupwise result for the following scenario?

How would I improve the performance in the following scenario

How to use python metaclass for the following scenario?

Create grid then color it following given scenario in tkinter

How to improve typescript's performance in the following scenario

How to apply conditional aggregation in SQL the following scenario?