WiscSort / wiscSort / RSW / header / utility_2.h
utility_2.h
Raw
// Using arrays and handcrafting comparissions
#pragma once
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#define KEY_SIZE 10
#define VALUE_SIZE 90

typedef struct k_t{
    char key[KEY_SIZE];
    void operator=(const k_t &rhs){
        memcpy(&key, &rhs.key, KEY_SIZE);
    }
} k_t;

typedef struct value{
    char value[VALUE_SIZE];
} value_t;

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

struct indexed_key{
    k_t k;
    size_t r_index;

    bool operator<(const indexed_key &rhs) const{
        int n;
        n = memcmp(k.key, rhs.k.key, KEY_SIZE);
        if(n>0)
            return false;
        else if (n<0)
            return true;
        else {
            return r_index < rhs.r_index;
        }
    }
    bool operator>(const indexed_key &rhs) const{
        int n;
        n = memcmp(k.key, rhs.k.key, KEY_SIZE);
        if(n>0)
            return true;
        else if (n<0)
            return false;
        else {
            return r_index > rhs.r_index;
        }
    }

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

    indexed_key() {
        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.r_index = 0;
        v_index = 0;
    }
} in_record_t;