1

I noticed today that I could give a C++ Vector or Array a Float value as index. (e.g. tab[0.5f]) This Float value will be converted into an Int value and then gives me the same result as tab[0].

This behavior is not interesting to me, as I'm searching for a method to access in the fastest way possible to an Object, depending on a Float key. Is it possible to keep the access speed of an array/vector, with a Float index ?

I understand that my keys will have an inaccuracy problem, but I expect my Float values to keep a maximum of 3 digits of precision.

Would a Map<Float, Object> do the job ? I've read on the C++ reference documentation that the Map access was "logarithmic in size", which is way less appealing to me.

Thank you :).

Edit :

I need to transform a mesh M containing X numbers of shared vertices into a mesh M' containing X' number of NON shared vertices. Indexes of vertices are set in M, and I know it's in TRIANGLE mode.

My current algorithm is :

for i in M.indexes, i+3

  • take 3indexes, and deducing the vertices they are pointing to (get 3vertices of a triangle)

  • calculate normal on these vertices

  • check, for each couple {Vertex_i, Normal} (i between 1 and 3, my 3vertices) if I already have this couple stored, and act accordingly

  • ... Next steps

To check the couple {Vertex,Normal}, i use an Array[x][y][z] based on position of the vertice, which IS a Float, though i know it won't be more than 3digits precision.

9
  • 1
    can you give us a convincing scenario of why you want a float index? or are you simply trying to map ranges into keys/indices? Commented Mar 25, 2014 at 8:56
  • 5
    if you expecting floats to keep a maximum of 3 digits - then just multiply it by 1000 and use an integer indexing. Because even with one digit after point you will have an inaccuracy problems. Commented Mar 25, 2014 at 9:00
  • What is the range and precision of your keys? Commented Mar 25, 2014 at 9:05
  • I need to transform a mesh M containing X numbers of shared vertices into a mesh M' containing X' number of NON shared vertices. Indexes of vertices are set in M, and I know it's in TRIANGLE mode. My current algorithm is : for i in M.indexes, i+3 - take 3indexes, and deducing the vertices they are pointing to (triangle) - calculate normal on these vertices - check, for each couple {Vertex_i, Normal} (i between 1 and 3, my 3vertices) if I already have this couple - ... next steps To check the couple {Vertex,Normal}, i use an t[x][y][z] based on position of the vertice, which CAN be a float Commented Mar 25, 2014 at 9:08
  • Wow, didn't expect my commentary to be this badly formated. Sorry about this. Commented Mar 25, 2014 at 9:08

1 Answer 1

1

Use an unordered_map. The find method has a complexity in average case: constant and in worst case: linear in container size.

Note : Since you were willing to use an array, I'm assuming you're not interested in having an ordered container

That been said, in any case, the performance depends on the input (mesh size) and its characteristics, and the only way to choose an optimal solution would be to implement any reasonable ones and benchmark against each other. In many cases theoretical complexity is irrelevant due to implementation specifics/intrinsics. I mean even if one told that a std::vector<std::pair<float, mapped_value>> would perform better in your case, I'd have to actually do some tests to prove him right/wrong

Sign up to request clarification or add additional context in comments.

1 Comment

The Mesh size should not exceed a thousands of vertices. But since the access to this container should be done once per Mesh Index, and there will be a few meshes (3~) needing this calculus every second or so, I need the fastest container possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.