Skip to content

[QST] Is there any plan to support insert empty_key_sentinel in static_map? #433

Answered by PointKernel
OliverLPH asked this question in Q&A
Discussion options

You must be logged in to vote

Please do not insert sentinels.

Sentinel values are reserved values and they must be never present in your input data. Attempting to insert them is an undefined behavior.

Whenever one needs to insert sentinels, it's time to take a step back and revise the algorithm. A common workaround is to use the hash map as an indirection mapping table to the original input, e.g. https://godbolt.org/z/s96e5z7bn:

  • the actual keys inserted in the map are indices of your original key array
  • -1 is a valid sentinel value since you will never access to keys[-1] in your problem
  • any original keys can be inserted/queried without worrying about conflicts against sentinels
  • custom hasher and key_equal are needed t…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@PointKernel
Comment options

Answer selected by OliverLPH
@OliverLPH
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants