Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. sparse sparse :: operator+ (const sparse& s) {
  2. sparse ans(this*);
  3. int sn = sizeof(s.xy) / (2 * sizeof(int));
  4. for (int i = 0; i < sn; ++i)
  5. ans[s.xy[i]] += s.A[i];
  6.  
  7. int n = sizeof(ans.xy) / (2 * sizeof(int)), k = 0;
  8. for (int i = 0; i < n; ++i) {
  9. if (ans.A[i] == 0) {
  10. ++k;
  11. ans.xy[i] = ans.xy[n - 1 - k];
  12. ans.A[i] = ans.A[n - 1 - k];
  13. }
  14. }
  15.  
  16. indices new_xy = new indices[n - k];
  17. double new_A = new double[n - k];
  18. for (int i = 0; i < n - k; ++i) {
  19. new_xy[i] = ans.xy[i];
  20. new_A[i] = ans.A[i];
  21. }
  22. delete[] ans.xy;
  23. delete[] ans.A;
  24. ans.xy = new_xy;
  25. ans.A = new_A;
  26.  
  27. return ans;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement