Creating byte buffers in rust [duplicate] - buffer

This question already has answers here:
Creating a vector of zeros for a specific size
(4 answers)
Closed 7 years ago.
What is the most efficient way to get access to &mut [u8]? Right now I'm borrowing from a Vec but it would be easier to just allocate the buffer more directly.
The best I can do right now is to preallocate a vector then push out its length but there is no way this is idiomatic.
let mut points_buf : Vec<u8> = Vec::with_capacity(points.len() * point::POINT_SIZE);
for _ in (0..points_buf.capacity()) {
points_buf.push(0);
}
file.read(&mut points_buf[..]).unwrap();

You could just create the vec with a given size directly:
vec![0; num_points]
Or use iterators:
repeat(0).take(num_points).collect::<Vec<_>>()

Related

Check if integer divided by 2. SWIFT [duplicate]

This question already has answers here:
How to know if a number is odd or even in Swift?
(5 answers)
Closed 11 months ago.
im absolutely new to development, and trying to learn swift.
Right now i know how to make random number, and my next step is:
Im trying to understand how to check if my random number (127) could be divided by 2 without decimals ?
I have no idea how to do it.
There is a specific API isMultiple(of:) in Standard Library for this purpose
let random = Int.random(in: 0..<100)
let isEven = random.isMultiple(of: 2)
You can use operator % - remainder operator
example:
if randomNumber % 2 == 0 {
print("\(randomNumber) is even")
} else {
print("\(randomNumber) is odd")
}

Get uint64_t out of __m128 intrinsic? [duplicate]

This question already has answers here:
C++ SSE Intrinsics: Storing results in variables [closed]
(1 answer)
print a __m128i variable
(4 answers)
SSE: Difference between _mm_load/store vs. using direct pointer access
(3 answers)
How to extract bytes from an SSE2 __m128i structure?
(1 answer)
Is `reinterpret_cast`ing between hardware SIMD vector pointer and the corresponding type an undefined behavior?
(2 answers)
Closed 1 year ago.
I can use _mm_set_epi64 to store two uint64_ts into a __m128 intrinsic. But hunting around, I see various ways to get the values back out: There's reinterpret_cast (and it's evil twin C-style casts), it's sibling union { __m128; uint64[2]; }; and memcpy, there's accessing fields of __m128. There's __m128i _mm_load_si128(__m128i *p);, but I'm not seeing a _mm_get_* function. Am I missing something? If there's a _mm_set_epi64 then there must be a non-cast way to get the uint64_ts back out, right? (Otherwise why would they bother providing _mm_set_epi64?)
I see Get member of __m128 by index? but the "correct answer" has a broken link and implies there's a load function, but all the loads I see map __m128 to __m128. Shouldn't there be a void _mm_get_epi64(__m128, uint64_t* outbuf)?

How to round a number in dart? [duplicate]

This question already has answers here:
How to convert a double to an int in Dart?
(11 answers)
How do you round a double in Dart to a given degree of precision AFTER the decimal point?
(28 answers)
Closed 3 years ago.
I want to round a double.
Double x = 5.56753;
x.toStringAsFixed(2);
When i put this, it gives 5.57000.
But i want to get 5.57. How do i get it?
there is num class contained function round():
Num
double numberToRound = 5.56753;
print(numberToRound.round());
//prints 6
If you want decimals
double n = num.parse(numberToRound.toStringAsFixed(2));
print(n);
//prints 5.57
check comment sujestion
For rounding doubles checkout: https://api.dartlang.org/stable/2.4.0/dart-core/double/round.html
Rounding won't work in your case because docs says:
Returns the integer closest to this.
So it will give 6 instead 5.57.
Your solution:
double x = 5.56753;
String roundedX = x.toStringAsFixed(2);
print(roundedX);

Why NSMutableData change address of pointer? [duplicate]

This question already has an answer here:
NSMutableData datawithBytesNoCopy:length:freeWhenDone: seems to make a copy of the buffer provided to it
(1 answer)
Closed 7 years ago.
I want to quick preallocate memory and have it wrapped by NSMutableData but with access via pointer.
So I have this:
var vertex = UnsafeMutablePointer<Float>.alloc(numberOfVertex * 3)
vertex.initialize(0)
vertexData = NSMutableData(bytesNoCopy: vertex, length: numberOfVertex * 3 * sizeof(Float))
But when I check address of vertex pointer I see different value then vertexData.bytes so as I understand I can't use pointer anymore to change data by index.
How to avoid this and have only one memory block?
According to the documentation, you cannot use NSMutableData with the no-copy flag that way actually:
NSMutableData responds to these methods, too, but the bytes are
copied anyway and the buffer is freed immediately.
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/BinaryData/Tasks/WorkingBinaryData.html#//apple_ref/doc/uid/20000717-149014

How can I convert function result? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to format a number with padding in Erlang
I developed this function :
check() ->
case get_val(Text, H) of
{ok, {Montant}} -> io:format("~s", [Montant]);
{error, pas_echeance} -> io:format("erreur")
end.
this function displayed Montant, this is an example of Montant :45
My goal is to convert this value "45" (which denotes Tunisian dinars) in the "45000" form (so I would have Tunisian millime -- each dinar consist of 1000 millimes).
Why dont you convert the value to integer (using list_to_integer("45") or list_to_integer(get_val(Text,H)) and multiply it by 1000.

Resources