WiscSort / wiscSort / RSW / header / utility_1.h
utility_1.h
Raw
// Using C++ constructs to represent and compare
#pragma once
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <array>
#include <tuple>

#define KEY_SIZE 10
#define VALUE_SIZE 90

struct k_t{
    std::array<char, KEY_SIZE> key;
//    void operator=(const k_t &rhs){
//        memcpy(&key, &rhs.key, KEY_SIZE);
//    }
};
struct value_t{
    std::array<char, VALUE_SIZE> value;
};

struct record_t{
    k_t k;
    value_t v;
    record_t(k_t _k, value_t _v){
        k = _k;
        v = _v;
    }
    record_t(){}
};

struct indexed_key{
    k_t k;
    size_t r_index;
    int n;
    bool operator<(const indexed_key &rhs) const{
        return std::tie(k.key, r_index) < std::tie(rhs.k.key, rhs.r_index);
        //bool isKeyLess = std::lexicographical_compare(std::begin(k.key), std::end(k.key), std::begin(rhs.k.key), std::end(rhs.k.key));
        //return std::tuple(isKeyLess, r_index) < std::tuple(!isKeyLess, rhs.r_index);
    }
    bool operator>(const indexed_key &rhs) const{
        return std::tie(k.key, r_index) > std::tie(rhs.k.key, rhs.r_index);
        //bool isKeyLess = std::lexicographical_compare(std::begin(k.key), std::end(k.key), std::begin(rhs.k.key), std::end(rhs.k.key));
        //return std::tuple(isKeyLess, r_index) > std::tuple(!isKeyLess, rhs.r_index);
    }

    indexed_key(k_t _k, size_t _r_index){
        k = _k;
        r_index = _r_index;
    }

    indexed_key() {
        k.key = {0};
        r_index = 0;
    }
};

typedef struct in_record {
    indexed_key ki;
    size_t v_index;

    bool operator<(const in_record &rhs) const {
        return ki < rhs.ki;
    }

    bool operator>(const in_record &rhs) const {
        return ki > rhs.ki;
    }

    void operator=(const in_record &rhs) {
        ki.k = rhs.ki.k;
        ki.r_index = rhs.ki.r_index;
        v_index = rhs.v_index;
    }

    in_record(k_t _k, size_t _v_index, size_t _r_index) {
        ki.k = _k;
        ki.r_index = _r_index;
        v_index = _v_index;
    }

    in_record() {
        ki.k.key = {0};
        ki.r_index = 0;
        v_index = 0;
    }
} in_record_t;