Looking up private instances in constant time

I am currently using Sophia to parse a file type. I need to match
against a Sophia Term object.

or more specificially against the IriData variant.

Currently I am doing this with a very clunky use of a
match. Something like this:

match t {
  Term::Iri(iri) if iri == "http://www.w3.org/2002/07/owl#AllValuesFrom" => {todo!()},
  Term::Iri(iri) if iri == "http://www.w3.org/2002/07/owl#AnnotatedSource" => {todo!()},
}

As far as I can tell, I cannot do anything better than this with a
match statement, because of equality check has to be done in a
pattern guard.

So, I thought, I need a hash-table look up instead -- I could use the
IriData as a key and return a Unit enum instance that will be easier
to handle.

But this fails for me, because IriData has only crate private
methods for creating instances. So I cannot see a way to look a hash
map with IriData keys.

Am I missing a trick here?

Since Term implements Hash, you could create Term values and store them in a hash table.

IndexMap uses an Equivalent trait. I think you could use an IndexMap<String, Unit> and then make a lookup type that wraps &IriData and implements Equivalent.

You can compare to a set of fixed strings really quickly with PHF:

You may need to wrap IriData in a newtype to implement required traits.

Thank you all for your solutions, all very good. I think I will go for the Term lookup in this instance.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.