1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "io/proxy_directory.h"
17
18 #include <unordered_set>
19 #include <utility>
20
21 #include <base/util/compile_time_hashes.h>
22 #include <core/namespace.h>
23
BASE_BEGIN_NAMESPACE()24 BASE_BEGIN_NAMESPACE()
25 template<>
26 uint64_t hash(const CORE_NS::IDirectory::Entry::Type& val)
27 {
28 return static_cast<uint64_t>(val);
29 }
30 BASE_END_NAMESPACE()
31
32 template<>
33 struct std::hash<CORE_NS::IDirectory::Entry> {
34 using argument_type = CORE_NS::IDirectory::Entry;
35 using result_type = uint64_t;
operator ()std::hash36 result_type operator()(argument_type const& s) const noexcept
37 {
38 return BASE_NS::Hash(s.type, s.name);
39 }
40 };
41
42 CORE_BEGIN_NAMESPACE()
43 using BASE_NS::move;
44 using BASE_NS::vector;
45
operator ==(const IDirectory::Entry & lhs,const IDirectory::Entry & rhs)46 bool operator==(const IDirectory::Entry& lhs, const IDirectory::Entry& rhs)
47 {
48 return lhs.type == rhs.type && lhs.name == rhs.name;
49 }
50
ProxyDirectory(vector<IDirectory::Ptr> && directories)51 ProxyDirectory::ProxyDirectory(vector<IDirectory::Ptr>&& directories) : directories_(move(directories)) {}
52
Close()53 void ProxyDirectory::Close() {}
54
GetEntries() const55 vector<IDirectory::Entry> ProxyDirectory::GetEntries() const
56 {
57 vector<IDirectory::Entry> result;
58 std::unordered_set<IDirectory::Entry> entries;
59 for (auto const& dir : directories_) {
60 for (auto&& entry : dir->GetEntries()) {
61 entries.insert(entry);
62 }
63 }
64 result.reserve(entries.size());
65 // std::unordered_set cannot really move unless the hash type and value type are the same. Should playaround with
66 // BASE_NS::unordered_map to see if it would suite better.
67 std::move(entries.begin(), entries.end(), std::back_inserter(result));
68 return result;
69 }
70 CORE_END_NAMESPACE()
71