Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:23:21+00:00 2026-05-31T19:23:21+00:00

My main data object is a array of doubles of a length that depends

  • 0

My main data object is a array of doubles of a length that depends on a specific instantiation of my class. I would like to construct a very simple hash table to store/retrieve these objects, and we can assume that the numbers are generated in a way that is free of numerical error.

int main() {
  std::tr1::unordered_map<double*, double*> cache;

  double x1[] = { 1.0, 3.14 };
  double x2[] = { 1.0, 3.14 };

  cache[x1] = x1;

  std::cout << "x1: " << cache.count(x1) << std::endl;
  std::cout << "x2: " << cache.count(x2) << std::endl;

  return 0;
}

The above obviously only compares the pointers, giving the output:

> ./tmp
x1: 1
x2: 0

When I really want to see:

> ./tmp
x1: 1
x2: 1

It’s pretty clear how to create custom hashing and equality functions when the size of the arrays are fixed at compile time but I do not know how to make custom functions that depend on a specific instantiation… I created a class below, but I’m not sure if it’s useful, or how it could be used.

class Hash_double_vec {

public:
  int dim;
  Hash_double_vec(int d) { dim = d; }

  size_t operator()(const double *x) const{
    std::tr1::hash<double> hash_fn;
    size_t r = hash_fn(x[0]);
    for(int i=1;i<dim;i++) r ^= hash_fn(x[i]);
    return r;
  }

  bool operator()(const double *x, const double *y) const{
    for(int i=1;i<dim;i++) if (fabs(x[i]-y[i]) > 1e-10) return false;
    return true;
  }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:23:22+00:00Added an answer on May 31, 2026 at 7:23 pm

    One way would be to create struct to hold the pointer to the sequence of doubles:

    struct DoubleRegion
    {
        double* p;
        size_t size;
    };
    
    bool operator==(DoubleRegion a, DoubleRegion b)
    {
        return a.size == b.size && memcmp(a.p, b.p, a.size) == 0;
    }
    
    size_t hash(DoubleRegion dr) 
    {
        size_t h = 0;
        for (double* p = dr.p; p != dr.p + dr.size; ++p)
            h ^= hash(*p);
        return h;
    }
    

    And then use it:

    unordered_map<DoubleRegion, DoubleRegion> cache;
    

    Of course it is your problem to make sure the lifetime of the backing memory is a superset of the lifetime of the DoubleRegion.

    Old Answer:

    If you don’t know until runtime how big the key and value is going to be, use a std::vector:

    unordered_map<vector<double>, vector<double>> cache;
    

    If you know at compile-time how big you can use an std::array:

    unordered_map<array<double, N>, array<double, N>> cache;
    

    In both cases the default hashing function will work by value as you want, and you do not need to define a custom one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one 'main' thread that creates an array of objects of class SlowData
I'm developing a REST service, so a request could be something like this: /Data/Main/Table=Customers/
Our main program is in java but the code that extracts our data from
I have a main table that I must get data from. I have a
The main concept goes like this. I have four listviews with its own data
In a WinForms UserControl, I would pass data to the main GUI thread by
I have a BankAccount class that models bank account information and a main method
I've created an array of objects in the main app delegate that is loading
For Example if I Create array of object And assing data... short version of
import org.apache.tools.ant.Project object HelloWorld { def main(args: Array[String]) { println(Hello, world!) } } I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.