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.
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