Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <atomic>
 
#include "core/common/common.h"
#include "core/common/inlined_containers.h"
 
// This class is not thread-safe
// TODO: this is a static hash lookup, it's easy to do it better
namespace onnxruntime {
class OrtValueNameIdxMap {
 public:
  using const_iterator = typename InlinedHashMap<std::string, int>::const_iterator;
 
  OrtValueNameIdxMap() = default;
 
  // Add OrtValue name to map and return index associated with it.
  // If entry already existed the existing index value is returned.
  int Add(const std::string& name) {
    const int idx = next_idx_;
    auto p = map_.emplace(name, idx);
    if (p.second) {
      idx_name_map_[idx] = name;
      next_idx_++;
      return idx;
    }
    return p.first->second;
  }
 
  common::Status GetIdx(std::string_view name, int& idx) const {
    idx = -1;
 
    auto it = map_.find(name);
    if (it == map_.end()) {
      return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Could not find OrtValue with name '", name, "'");
    }
 
    idx = it->second;
    return common::Status::OK();
  }
 
  common::Status GetName(int idx, std::string& name) const {
    auto it = idx_name_map_.find(idx);
    if (it == idx_name_map_.end()) {
      return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Could not find OrtValue with idx '", idx, "'");
    }
 
    name = it->second;
    return common::Status::OK();
  }
 
  size_t Size() const { return map_.size(); };
  int MaxIdx() const { return next_idx_ - 1; }
  void Reserve(size_t size) {
    map_.reserve(size);
    idx_name_map_.reserve(size);
  }
 
  const_iterator begin() const noexcept { return map_.cbegin(); }
  const_iterator end() const noexcept { return map_.cend(); }
 
 private:
  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(OrtValueNameIdxMap);
 
  int next_idx_ = 0;
  InlinedHashMap<std::string, int> map_;
  InlinedHashMap<int, std::string> idx_name_map_;
};
}  // namespace onnxruntime