fix a bug in cncom.h & speed up AddVMerged(const TVec&) #224
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi! I have refined two functions : IsNIdIn(const TInt&) and AddVMerged(TVec&).
The value member NIdV of the class TCnCom is unsorted, so the original IsNIdIn(const TInt&) worked incorrectly. I fixed it simply by replacing the binary search with the forwarding search. I add a few lines of test codes to show the incorrectness/correctness of the original/refined implementation.
Another issue I discovered when using snap is that the AddVMerged(TVec&) was slower than I expected. And my contributions to this issue are :
The original codes are simple, clear, and robust. However, its complexity is O(xy), where x is this->Len() and y is the length of ValV. I reimplemented this function with complexity approximately O(ylgy+x). The old implementation would move the elements backward multiply times. The new implementation moves these elements just once because it moves the elements at the end of this TVec first.
Since no unit test for the classes and functions in ds.h, I put my unit tests for the new implement of this function at: https://github.com/DaosWelcome/snap/blob/dev/test/test-TVec.cpp. And the new implementation passed the tests.
A simple efficiency comparison experiment is also provided at https://github.com/DaosWelcome/snap/blob/dev/test/test-TVec.cpp. On my mac laptop, the new implementation is around 50 times faster.
I would hope these two simple refinements would be helpful.
Zhao