Given two references, this function swaps their values.
@tparam a The type of the values stored in the references.
@param lhs The left-hand side reference.
User
ref and abort are effect types, parameters after "in" keyword are second-class values such as effect handlers or closures.
Assistant
/**
@brief Swaps the values of two references.
Given two references, this function swaps their values.
@tparam a The type of the values stored in the references.
User
in swap, lhs and rhs are effect handlers, not references. they use the ref type which REPRESENTS a reference effect.
Assistant
/**
@param rhs The right-hand side reference.
*/
func swap in (lhs: ref, rhs: ref): unit {
let tmp = lhs.get();
lhs.set(rhs.get());
rhs.set(tmp);
()
}
/**
@brief Swaps the values of two pointers.
Given two pointers, this function swaps their values.
@tparam a The type of the values pointed to by the pointers.
@param lhs The left-hand side pointer.
@param rhs The right-hand side pointer.
@exception raise Raised when the pointer is null or invalid.
*/
func swap_ptr(lhs: ptr, rhs: ptr) in (raise: abort): unit {
let lhs_brw = ptr2brw(lhs, raise);
let rhs_brw = ptr2brw(rhs, raise);
swap_brw(lhs_brw, rhs_brw)
}
/**
@brief Swaps the values of two borrowed references.
Given two borrowed references, this function swaps their values.
@tparam a The type of the values borrowed by the references.
@param lhs The left-hand side borrowed reference.
@param rhs The right-hand side borrowed reference.
*/
func swap_brw(lhs: brw, rhs: brw): unit {
use lhs_ref = brw2ref(lhs);
use rhs_ref = brw2ref(rhs);
swap(lhs_ref, rhs_ref)
}
/**
@brief Converts a borrowed reference to a mutable reference.
Converts a borrowed reference to a mutable reference, and performs an action on the mutable reference.
@tparam a The type of the values pointed to by the references.
@tparam b The return type of the action function.
@param brw_ The borrowed reference to convert.
@param action The action to perform on the mutable reference.
@return The result of the action function.
@exception ref_ The mutable reference obtained from the conversion.
@exception Resume An empty return to resume execution after performing the action.
*/
func brw2ref<a, b>(brw_: brw) in (action: fn(ref_: ref) -> b): b {
try action(ref_) catch ref_: ref {
set(value): resume => brw_.set(value); resume(()),
get: resume => resume(brw_.get()),
}
}
@param lhs The left-hand side reference.
@param rhs The right-hand side reference.
@effects None
*/
func swap in (lhs: ref, rhs: ref): unit {
let tmp = lhs.get();
lhs.set(rhs.get());
rhs.set(tmp);
()
}
/**
@brief Swaps the values of two pointers.
Given two pointers, this function swaps their values.
@tparam a The type of the values pointed to by the pointers.
@param lhs The left-hand side pointer.
@param rhs The right-hand side pointer.
@exception raise Raised when the pointer is null or invalid.
@effects None
*/
func swap_ptr(lhs: ptr, rhs: ptr) in (raise: abort): unit {
let lhs_brw = ptr2brw(lhs, raise);
let rhs_brw = ptr2brw(rhs, raise);
swap_brw(lhs_brw, rhs_brw)
}
/**
@brief Swaps the values of two borrowed references.
Given two borrowed references, this function swaps their values.
@tparam a The type of the values borrowed by the references.
@param lhs The left-hand side borrowed reference.
@param rhs The right-hand side borrowed reference.
@effects None
*/
func swap_brw(lhs: brw, rhs: brw): unit {
use lhs_ref = brw2ref(lhs);
use rhs_ref = brw2ref(rhs);
swap(lhs_ref, rhs_ref)
}
/**
@brief Converts a borrowed reference to a mutable reference.
Converts a borrowed reference to a mutable reference, and performs an action on the mutable reference.
@tparam a The type of the values pointed to by the references.
@tparam b The return type of the action function.
@param brw_ The borrowed reference to convert.
@param action The action to perform on the mutable reference.
@return The result of the action function.
@exception ref_ The mutable reference obtained from the conversion.
@exception Resume An empty return to resume execution after performing the action.
Given two references, this function swaps their values.
@tparam a The type of the values stored in the references.
@param lhs The left-hand side effect handler representing a reference.
@param rhs The right-hand side effect handler representing a reference.
@effects Modifies the values referenced by lhs and rhs.
*/
func swap in (lhs: ref, rhs: ref): unit {
let tmp = lhs.get();
lhs.set(rhs.get());
rhs.set(tmp);
()
}
/**
@brief Swaps the values of two pointers.
Given two pointers, this function swaps their values.
@tparam a The type of the values pointed to by the pointers.
@param lhs The left-hand side effect handler representing a pointer.
@param rhs The right-hand side effect handler representing a pointer.
@exception raise Raised when the pointer is null or invalid.
@effects Modifies the values pointed to by lhs and rhs.
*/
func swap_ptr(lhs: ptr, rhs: ptr) in (raise: abort): unit {
let lhs_brw = ptr2brw(lhs, raise);
let rhs_brw = ptr2brw(rhs, raise);
swap_brw(lhs_brw, rhs_brw)
}
/**
@brief Swaps the values of two borrowed references.
Given two borrowed references, this function swaps their values.
@tparam a The type of the values borrowed by the references.
@param lhs The left-hand side borrowed reference.
@param rhs The right-hand side borrowed reference.
@effects None
*/
func swap_brw(lhs: brw, rhs: brw): unit {
use lhs_ref = brw2ref(lhs);
use rhs_ref = brw2ref(rhs);
swap(lhs_ref, rhs_ref)
}
/**
@brief Converts a borrowed reference to a mutable reference.
Converts a borrowed reference to a mutable reference, and performs an action on the mutable reference.
@tparam a The type of the values pointed to by the references.
@tparam b The return type of the action function.
@param brw_ The borrowed reference to convert.
@param action The action to perform on the mutable reference.
@return The result of the action function.
@exception ref_ The mutable reference obtained from the conversion.
@exception Resume An empty return to resume execution after performing the action.