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.
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.
Step2:Split Object with BaseID: S5 based on quantity, say 15, 5. Here is resulting list.
Step3:Split Object with BaseID: S1 based on Quantity, Say 70, 10.
Step 4: If I order by the final List, I will get objects sorted based on Insertion order.
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).
2) Maintain Dictionary with key As Org_BaseID, Instead of updating all list, update sort order for Objects with specific Org_baseID.
Is there is any other approach, which I can try?
Thanks in Advance, and appreciate all suggestions.
Approaches I tried
1) Allan S Approach- Insert an Object instead of adding.
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).
I will try to use Linked List and will check the performance.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments