Project

General

Profile

Feature #328

Swap

Added by John Abbott about 11 years ago. Updated about 10 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
CoCoA-5 function: new
Target version:
Start date:
05 Mar 2013
Due date:
% Done:

100%

Estimated time:
Spent time:

Description

I think C5 should have a swap procedure.

Presumably it could simply be

Define swap(ref A, ref B)
  tmp := A;
  A := B;
  B := tmp;
EndDefine;

Perhaps an internal impl could avoid actually making a copies of the values.

History

#1 Updated by John Abbott almost 11 years ago

  • Target version changed from CoCoA-5.0.3 to CoCoA-5.0.9

#2 Updated by John Abbott about 10 years ago

  • Status changed from New to In Progress
  • Assignee set to John Abbott

Added first basic impl to coclib.cpkg5.

It should be possible to swap most types of value in C++ without actually copying them! I'll do this soon (maybe?)

Added manual entry too.

#3 Updated by John Abbott about 10 years ago

  • % Done changed from 0 to 20

Giovanni any good hints on how I could implement an efficient swap function in C++ (rather than doing 3 assignments in the interpreter)?

#4 Updated by Giovanni Lagorio about 10 years ago

If I remember correctly, the swap above should not make any useless copy (that is, objects should be shared until they are modified). I don't remember the details of the implementation, in C++ you could probably just swap a couple of pointers, but it shouldn't be noticeably faster than the 3 assignments above (unless you call swap a gazillion times in a row ;-) ).

#5 Updated by John Abbott about 10 years ago

  • Status changed from In Progress to Feedback
  • % Done changed from 20 to 80

I tried swapping two long lists (10000000 entries), and it was pretty quick; so I deduce that the values are not copied -- thanks for the tip-off, Giovanni.

#6 Updated by John Abbott about 10 years ago

  • Status changed from Feedback to Closed
  • % Done changed from 80 to 100

No trouble after 1 month in feedback.

Minor revision to the documentation.

Closing.

Also available in: Atom PDF