unordered_multimap<字符串, 字符串>;映射<字符串,字符串>软搜索,如何做这样的事情以及如何预处理地图以加快搜索速度?

发布于 2024-11-19 12:03:48 字数 1142 浏览 2 评论 0 原文

我们有一个map、rules>; service_map 其中规则是

struct rules
{
boost::unordered_multimap<string, string> set_of_rules_1;
boost::unordered_multimap<string, string> set_of_rules_2;
}

在我的例子中,规则是来自http请求headersarguments的对,例如在一个这样的unordered_multimap中我们可以找到Accept -语言:FR接受语言:US

每个boost::shared_ptr都是继承自service class的类的实例。

我填写这张服务地图<->规则与服务和规则一起运行(来自一些共享库和一些带有规则的文本文件)。

现在,我得到了 data 的实例,

struct data
{
map<string, string> headers;
map<string, string> arguments;
}

对于每个给定的 data 对象,我需要从 service_map 中找到最相关的 service 并调用其 service->inherited_method();

这里所说的“相关”是指其规则最适合给定数据的规则。例如,如果我们在规则 Accept-Language : FRAccept-Language : US 中,则如果数据包含对 Accept-Language : fr-FR,ru; q=0.8,en-US;q=0.6,en;q=0.4 我们认为这是相关的。

预处理我的 service_map 以实现更快的软搜索的最佳方法是什么,以及如何实现此类搜索?

We have a map<boost::shared_ptr<service>, rules> service_map where rules is

struct rules
{
boost::unordered_multimap<string, string> set_of_rules_1;
boost::unordered_multimap<string, string> set_of_rules_2;
}

In my case of rules are pairs from of http request headers and arguments, for example in one such unordered_multimap we could find Accept-Language : FR and Accept-Language : US.

Each boost::shared_ptr<service> is some instance of class that inherits from service class.

I fill this map of servise <-> rules on fly with services and rules (from some shared libs and some text files with rules).

Now I am given instances of data

struct data
{
map<string, string> headers;
map<string, string> arguments;
}

For each given data object I need to find most relevant service from service_map and call its service->inherited_method();

By relevant here we mean one whose rules fit to given data mostly. For example if we have in rules Accept-Language : FR and Accept-Language : US than if data contains pair Accept-Language : fr-FR,ru;q=0.8,en-US;q=0.6,en;q=0.4 we think it is relevant.

What would be best way to preprocess my service_map for faster soft search, and how to implement such search?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽梦忆笙歌 2024-11-26 12:03:49

这是一项艰巨的任务,您必须自己开发一些逻辑。不过,这里有一个框架解决方案:

1) 编写一个函数,根据规则与给定数据集的相关性对规则进行排名:

int relevance(const rules & r, const data & d); // write this

2) 对于每条数据,创建规则的排序排名。例如,您可以保留一堆迭代器。然后找到与最相关的规则集匹配的服务。

typedef RuleCollection::const_iterator rit;

boost::shared_ptr<service> find_service(cosnt data & d, ...)
{
  std::multimap<int, rit> relevant_rules;

  for (rit it = rc.begin(), end = rc.end(); it != end; ++it)
  {
    // relevant_rules[relevance(*it, d)] = it; // sorry, that was nonsense
    relevant_rules.insert(std::make_pair(relevance(*it, d), it));
  }

  for (auto it = relevant_rules.rbegin(), rend = relevant_rules.rend(); it != rend; ++it)
  {
    for (auto st = service_map.begin(), end = service_map.end(); st != end; ++st)
    {
      if (*st->second == *it->second) return st->first;
    }
  }
  throw std::exception("Could not find suitable service.");
}

我假设您的所有规则都保存在 RuleCollection rc; 中,这是值类型 rules 的一些容器。

编辑:修复了 multimap 元素插入 - 由于显而易见的原因,multimap 没有 [] 访问运算符。

This is a tall order, and you'll have to develop some of the logic yourself. However, here's a skeleton solution:

1) Write a function that ranks rules according to their relevance for a given set of data:

int relevance(const rules & r, const data & d); // write this

2) For each piece of data, create a sorted ranking of the rules. For instance, you could keep a bunch of iterators around. Then find the service that matches the most relevant rule set.

typedef RuleCollection::const_iterator rit;

boost::shared_ptr<service> find_service(cosnt data & d, ...)
{
  std::multimap<int, rit> relevant_rules;

  for (rit it = rc.begin(), end = rc.end(); it != end; ++it)
  {
    // relevant_rules[relevance(*it, d)] = it; // sorry, that was nonsense
    relevant_rules.insert(std::make_pair(relevance(*it, d), it));
  }

  for (auto it = relevant_rules.rbegin(), rend = relevant_rules.rend(); it != rend; ++it)
  {
    for (auto st = service_map.begin(), end = service_map.end(); st != end; ++st)
    {
      if (*st->second == *it->second) return st->first;
    }
  }
  throw std::exception("Could not find suitable service.");
}

I'm supposing that all your rules are kept in RuleCollection rc;, some container of value type rules.

Edit: Fixed multimap element insertion -- multimap does not have a [] access operator for obvious reasons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文