Examples¶
Example of initializing avx::Char256¶
#include <iostream>
#include <types/char256.hpp>
int main(int argc, char **argv) {
avx::Char256 a; // Set internal vector value to 0.
avx::Char256 b(3); // Initialize vector with value 3.
avx::Char256 c(std::string("Hello")); // Initialize vector with string "Hello".
avx::Char256 d({1, 2, 3, 4, 5}); // Initializing using initializer list.
avx::Char256 e{"Lorem ipsum"}; // <-- This will produce undefined behaviour (in current version) as constructor assumes argument contains at least 32 bytes.
return 0;
}
Important
While first two constructors directly initialize underlying vector, the third one will check string size first. If size is equal or greater than 32 bytes then first 32 bytes will be copied to the vector. Otherwise temporary buffer will be zeroed and then the string contents will be copied into it.
Warning
avx::Char256
offers another constructor which accepts explicitly arguments of const char*
.
This constructor is only intended to use when loading binary data into vector!
Altough it cheks for nullptr
it does not check the length of the string.
It will ALWAYS load 32 bytes into vector.
Using basic arithmetic operators¶
avx::Char256
offers two versions of each (non unary) operator.
Let’s assume we have created variables:
avx::Char256 a({1, 2, 3, 4, 5});
avx::Char256 b({7, 8, 9, 10, 11});
char c = 5;
One option is to add vectors a
and b
together:
auto d = a + b; // d will have values 8, 10, 12, 14, 16, 0 ... 0
or
a += b; // This time a will have 8, 10, 12, 14, 16, 0 ... 0
However if we want to just add the same value across the vector we can do it like this:
auto d = a + c; // d will have values 6, 7, 8, 9, 10, 0 ... 0
or
a += c; // same but values will be written to a
List of supported operators¶
Operator |
Supported types |
---|---|
|
|
+, += |
|
-, -= |
|
*, *= |
|
/, /= [3] |
|
%, %= [3] |
|
|, |= |
|
&, &= |
|
^, ^= |
|
<<, <<= |
|
>>, >>= [4] |
|
~ |
Char256 specific metods¶
std::string avx::Char256::toString()
- this method will produce a string representation of vector contents.
Unlike std::string avx::Char256::str()
method it will put the exact vector content into the string.
Below code example shows difference between these two methods:
#include <types/char256.hpp>
#include <iostream>
int main(int argc, char* argv[]) {
avx::Char256 a{std::string("Lorem ipsum")};
std::cout << a.str() << '\n'; // Will print "Char256(76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109, 0, ... 0, 0)"
std::cout << a.toString() << '\n'; // Will print "Lorem ipsum"
return 0;
Note
Even if vector contains binary data using std::string avx::Char256::toString()
will not result in undefined behaviour.
Intermediate array allocates 33 bytes for data and sets 33-rd byte to '\0'
thus resulting string will always be null-terminated.
avx::Char256
supports printing to stream by using std::ostream &avx::operator<<(std::ostream &os, const avx::Char256 &a)
.std::string avx::Char256::toString()
but instead of returning a string it will print data out to the ostream
.