Skip to content

5. Utils

Saptak Bhoumik edited this page Jul 3, 2024 · 1 revision

Iterator

This is used commonly when we use the reduce function

Example usage

#include <ouroboros/ouroboros.hpp>
#include <iostream>
int main(){
    double data[10]={1,2,3,4,5,6,7,8,9,10};
    // Note the iterator does not copy the data
    // It just stores the pointer to the data and never modifies or deletes it
    Ouroboros::Utils::Iterator<double> it1(data,10);//10 is the number of times we can iterate.
    for(auto x:it1){//U cant use auto& x:it because it does not return a reference
        std::cout<<x<<" ";
    }
    std::cout<<"\n-------------------"<<std::endl;
    Ouroboros::Utils::Iterator<double> it2(data,5,2);//5 is the number of times we can iterate. 2 is the step size
    for(auto x:it2){
        std::cout<<x<<" ";
    }
    std::cout<<"\n-------------------"<<std::endl;
    it2=it1;
    for(auto x:it2){
        std::cout<<x<<" ";
    }
    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10 
-------------------
1 3 5 7 9 
-------------------
1 2 3 4 5 6 7 8 9 10

File utils

The other function under the namespace Ouroboros::Utils are

void write_bin_float(std::fstream& file,float value);//Writes a float to file in binary
void write_bin_double(std::fstream& file,double value);//Writes a double to file in binary

void write_bin_uint8(std::fstream& file,uint8_t value);//Writes a uint8_t to file in binary
void write_bin_uint16(std::fstream& file,uint16_t value);//Writes a uint16_t to file in binary
void write_bin_uint32(std::fstream& file,uint32_t value);//Writes a uint32_t to file in binary
void write_bin_uint64(std::fstream& file,uint64_t value);//Writes a uint64_t to file in binary

void write_bin_int8(std::fstream& file,int8_t value);//Writes a int8_t to file in binary
void write_bin_int16(std::fstream& file,int16_t value);//Writes a int16_t to file in binary
void write_bin_int32(std::fstream& file,int32_t value);//Writes a int32_t to file in binary
void write_bin_int64(std::fstream& file,int64_t value);//Writes a int64_t to file in binary

void write_bin_str(std::fstream& file,std::string value);//Writes a string to file in binary

void write_bin_shape(std::fstream& file,const Shape& shape);//Writes a Shape to file
void write_bin_tensor(std::fstream& file,const Tensor& tensor,TensorType type=TensorType::DOUBLE);//Writes a Tensor to file in the data type format specfied
void write_bin_bool_tensor(std::fstream& file,const BoolTensor& tensor);//Writes a BoolTensor to file

float read_bin_float(std::fstream& file);//Reads a float from file
double read_bin_double(std::fstream& file);//Reads a double from file

uint8_t read_bin_uint8(std::fstream& file);//Reads a uint8_t from file
uint16_t read_bin_uint16(std::fstream& file);//Reads a uint16_t from file
uint32_t read_bin_uint32(std::fstream& file);//Reads a uint32_t from file
uint64_t read_bin_uint64(std::fstream& file);//Reads a uint64_t from file

int8_t read_bin_int8(std::fstream& file);//Reads a int8_t from file
int16_t read_bin_int16(std::fstream& file);//Reads a int16_t from file
int32_t read_bin_int32(std::fstream& file);//Reads a int32_t from file
int64_t read_bin_int64(std::fstream& file);//Reads a int64_t from file

std::string read_bin_str(std::fstream& file);//Reads a string from file

Shape read_bin_shape(std::fstream& file);//Reads a Shape from file
Tensor read_bin_tensor(std::fstream& file);//Reads a Tensor from file
BoolTensor read_bin_bool_tensor(std::fstream& file);//Reads a BoolTensor from file

Example usage

#include <ouroboros/ouroboros.hpp>
#include <iostream>
int main(){
    {
        std::cout<<"Writing to a binary file\n";
        std::fstream file("example.bin",std::ios::out|std::ios::binary);
        Ouroboros::Utils::write_bin_double(file,3.14);
        Ouroboros::Tensor t=Ouroboros::CreateTensor::ones({2,2});
        std::cout<<"t:\n"<<t<<"\n";
        Ouroboros::Utils::write_bin_tensor(file,t);//By default it writes the tensor as double i.e every element take 8 bytes
        Ouroboros::Utils::write_bin_tensor(file,t,Ouroboros::Utils::TensorType::FLOAT);//Now every element takes 4 bytes
        file.close();
        /*
        enum class TensorType{
            //Done to reduce the size of the tensor in the file
            INT8,//Save the tensor as int8_t
            INT16,//Save the tensor as int16_t
            INT32,//Save the tensor as int32_t
            INT64,//Save the tensor as int64_t
            UINT8,//Save the tensor as uint8_t
            UINT16,//Save the tensor as uint16_t
            UINT32,//Save the tensor as uint32_t
            UINT64,//Save the tensor as uint64_t
            FLOAT,//Save the tensor as float
            DOUBLE,//Save the tensor as double
        };
        */
    }
    {
        std::cout<<"Reading from a binary file\n";
        std::fstream file("example.bin",std::ios::in|std::ios::binary);
        std::cout<<"Double value: "<<Ouroboros::Utils::read_bin_double(file)<<"\n";
        std::cout<<"Tensor as double:\n"<<Ouroboros::Utils::read_bin_tensor(file)<<"\n";
        std::cout<<"Tensor as float:\n"<<Ouroboros::Utils::read_bin_tensor(file)<<"\n";
        file.close();
    }
}

Output

Writing to a binary file
t:
[[1, 1], [1, 1]]

Reading from a binary file
Double value: 3.14
Tensor as double:
[[1, 1], [1, 1]]

Tensor as float:
[[1, 1], [1, 1]]
Clone this wiki locally