1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __MEM_RUBY_COMMON_NETDEST_HH__
30#define __MEM_RUBY_COMMON_NETDEST_HH__
31
32#include <iostream>
33#include <vector>
34
35#include "mem/ruby/common/Set.hh"
36#include "mem/ruby/common/MachineID.hh"
37
38// NetDest specifies the network destination of a Message
39class NetDest
40{
41  public:
42    // Constructors
43    // creates and empty set
44    NetDest();
45    explicit NetDest(int bit_size);
46
47    NetDest& operator=(const Set& obj);
48
49    ~NetDest()
50    { }
51
52    void add(MachineID newElement);
53    void addNetDest(const NetDest& netDest);
54    void setNetDest(MachineType machine, const Set& set);
55    void remove(MachineID oldElement);
56    void removeNetDest(const NetDest& netDest);
57    void clear();
58    void broadcast();
59    void broadcast(MachineType machine);
60    int count() const;
61    bool isEqual(const NetDest& netDest) const;
62
63    // return the logical OR of this netDest and orNetDest
64    NetDest OR(const NetDest& orNetDest) const;
65
66    // return the logical AND of this netDest and andNetDest
67    NetDest AND(const NetDest& andNetDest) const;
68
69    // Returns true if the intersection of the two netDests is non-empty
70    bool intersectionIsNotEmpty(const NetDest& other_netDest) const;
71
72    // Returns true if the intersection of the two netDests is empty
73    bool intersectionIsEmpty(const NetDest& other_netDest) const;
74
75    bool isSuperset(const NetDest& test) const;
76    bool isSubset(const NetDest& test) const { return test.isSuperset(*this); }
77    bool isElement(MachineID element) const;
78    bool isBroadcast() const;
79    bool isEmpty() const;
80
81    // For Princeton Network
82    std::vector<NodeID> getAllDest();
83
84    MachineID smallestElement() const;
85    MachineID smallestElement(MachineType machine) const;
86
87    void resize();
88    int getSize() const { return m_bits.size(); }
89
90    // get element for a index
91    NodeID elementAt(MachineID index);
92
93    void print(std::ostream& out) const;
94
95  private:
96    // returns a value >= MachineType_base_level("this machine")
97    // and < MachineType_base_level("next highest machine")
98    int
99    vecIndex(MachineID m) const
100    {
101        int vec_index = MachineType_base_level(m.type);
102        assert(vec_index < m_bits.size());
103        return vec_index;
104    }
105
106    NodeID bitIndex(NodeID index) const { return index; }
107
108    std::vector<Set> m_bits;  // a vector of bit vectors - i.e. Sets
109};
110
111inline std::ostream&
112operator<<(std::ostream& out, const NetDest& obj)
113{
114    obj.print(out);
115    out << std::flush;
116    return out;
117}
118
119#endif // __MEM_RUBY_COMMON_NETDEST_HH__
120