Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 0.79 KB | None | 0 0
  1. Index: tango/core/Array.d
  2. ===================================================================
  3. --- tango/core/Array.d  (revision 5688)
  4. +++ tango/core/Array.d  (working copy)
  5. @@ -3625,18 +3625,19 @@
  6.  
  7.         ParameterTupleOf!(Pred)[0][] filter(Array array, Pred pred, ParameterTupleOf!(Pred)[0][] buf = null)
  8.         {
  9. -           // Unfortunately, we don't know our output size -- it could be empty or
  10. -           // the length of the input array. So we won't try to do anything fancy
  11. -           // with preallocation.
  12. -           buf.length = 0;
  13. -           foreach (i, e; array)
  14. +           size_t idx = 0;
  15. +           foreach (e; array)
  16.             {
  17.                 if (pred(e))
  18.                 {
  19. -                   buf ~= e;
  20. +                   if (idx < buf.length)
  21. +                       buf[idx] = e;
  22. +                   else
  23. +                       buf ~= e;
  24. +                   idx++;
  25.                 }
  26.             }
  27. -           return buf;
  28. +           return buf[0..idx];
  29.         }
  30.     }
Add Comment
Please, Sign In to add comment