KP56's blog

By KP56, history, 21 month(s) ago, In English

I'm trying to get a custom hash function working for my structure but I get a lot of warnings and compilation fails.

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using lli = long long int;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define endl "\n"
#define in(x) cin >> x
#define in_lli(x) lli (x); in(x)
#define in_i(x) int (x); in(x)
#define in_str(x) string (x); in(x)

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int current_id = 0;

struct Rectangle {
    int id;

    lli width;
    lli height;
    lli area;

    Rectangle(lli width, lli height) {
        this->width = width;
        this->height = height;
        this->area = width * height;

        id = current_id;
        current_id++;
    }

    inline bool operator == (const Rectangle& rectangle) {
        return rectangle.id == id;
    }

    struct HashFunction {
        size_t operator()(const Rectangle& rectangle) const {
            return hash<int>()(rectangle.id);
        }
    };
};

void readCaseData() {
    //in_lli(rectangles);
    lli rectangles = 100000;
    //in_lli(queries);

    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_higher_than(1000);
    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_wider_than(1000);

    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_shorter_than(1000);
    vector<unordered_set<Rectangle,Rectangle::HashFunction>> rect_narrower_than(1000);
    for (int i = 0; i < rectangles; i++) {
        //in_lli(height);
        //in_lli(width);
        lli height = 0;
        lli width = 0;

        for (int i = width; i < 1000; i++) {
            rect_narrower_than[i].emplace(width, height);
        }

        for (int i = height; i < 1000; i++) {
            rect_shorter_than[i].emplace(width, height);
        }

        for (int i = 999; i >= width; i--) {
            rect_wider_than[i].emplace(width, height);
        }

        for (int i = 999; i >= height; i--) {
            rect_higher_than[i].emplace(width, height);
        }
    }
    
    
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    in_i(cases);

    while (cases--) {
        readCaseData();
    }
}

Does anyone know what the issue may be?

  • Vote: I like it
  • -1
  • Vote: I do not like it

| Write comment?
»
21 month(s) ago, # |
  Vote: I like it +3 Vote: I do not like it

If you add const in line 60 it compiles on my machine.

    inline bool operator==(const Rectangle& rectangle) const {

Hope this helps you. Have a nice day!