multi_bloom_filter.cc revision 14262:991410960fdb
1/*
2 * Copyright (c) 2019 Inria
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Daniel Carvalho
30 */
31
32#include "base/filters/multi_bloom_filter.hh"
33
34#include "base/logging.hh"
35#include "params/BloomFilterMulti.hh"
36
37namespace BloomFilter {
38
39Multi::Multi(const BloomFilterMultiParams* p)
40    : Base(p), filters(p->filters)
41{
42}
43
44Multi::~Multi()
45{
46}
47
48void
49Multi::clear()
50{
51    for (auto& sub_filter : filters) {
52        sub_filter->clear();
53    }
54}
55
56void
57Multi::merge(const Base* other)
58{
59    auto* cast_other = static_cast<const Multi*>(other);
60    assert(filters.size() == cast_other->filters.size());
61    for (int i = 0; i < filters.size(); ++i){
62        filters[i]->merge(cast_other->filters[i]);
63    }
64}
65
66void
67Multi::set(Addr addr)
68{
69    for (auto& sub_filter : filters) {
70        sub_filter->set(addr);
71    }
72}
73
74void
75Multi::unset(Addr addr)
76{
77    for (auto& sub_filter : filters) {
78        sub_filter->unset(addr);
79    }
80}
81
82bool
83Multi::isSet(Addr addr) const
84{
85    int count = 0;
86    for (const auto& sub_filter : filters) {
87        if (sub_filter->isSet(addr)) {
88            count++;
89        }
90    }
91    return count >= setThreshold;
92}
93
94int
95Multi::getCount(Addr addr) const
96{
97    int count = 0;
98    for (const auto& sub_filter : filters) {
99        count += sub_filter->getCount(addr);
100    }
101    return count;
102}
103
104int
105Multi::getTotalCount() const
106{
107    int count = 0;
108    for (const auto& sub_filter : filters) {
109        count += sub_filter->getTotalCount();
110    }
111    return count;
112}
113
114} // namespace BloomFilter
115
116BloomFilter::Multi*
117BloomFilterMultiParams::create()
118{
119    return new BloomFilter::Multi(this);
120}
121
122