PackedList.C 6.52 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
    Copyright (C) 2018-2019 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "PackedList.H"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "labelRange.H"

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

template<unsigned Width>
Foam::PackedList<Width>::PackedList
(
    const PackedList<Width>& list,
    const labelUList& addr
)
:
    PackedList<Width>(addr.size())
{
    const label len = addr.size();

    for (label i = 0; i < len; ++i)
    {
        set(i, list.get(addr[i]));
    }
}


template<unsigned Width>
52
template<class Addr>
53 54 55
Foam::PackedList<Width>::PackedList
(
    const PackedList<Width>& list,
56
    const IndirectListBase<label, Addr>& addr
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
)
:
    PackedList<Width>(addr.size())
{
    const label len = addr.size();

    for (label i = 0; i < len; ++i)
    {
        set(i, list.get(addr[i]));
    }
}


template<unsigned Width>
Foam::PackedList<Width>::PackedList
(
    const PackedList<Width>& list,
    const labelRange& range
)
:
    PackedList<Width>(range.size())
{
    label pos = range.start();
    const label len = range.size();

    for (label i = 0; i < len; ++i)
    {
        set(i, list.get(pos));
        ++pos;
    }
}

89 90 91 92 93 94

// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<unsigned Width>
bool Foam::PackedList<Width>::uniform() const
{
95 96
    // Trivial cases
    if (empty())
97
    {
98
        return false;
99
    }
100 101 102 103 104
    else if (size() == 1)
    {
        return true;
    }

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    // The value of the first element for testing
    const unsigned int val = get(0);

    const label nblocks = num_blocks(size());

    bool identical = true;

    if (!val)
    {
        // Zero value: can just check block content directly

        for (label blocki = 0; identical && blocki < nblocks; ++blocki)
        {
            identical = !blocks_[blocki];
        }

        return identical;
    }
    else if (nblocks > 1)
    {
        // Check all blocks that are completely occupied: (nblocks-1)
        const unsigned int blockval =
            BitOps::repeat_value<block_type,Width>(val);

        for (label blocki = 0; identical && blocki < (nblocks-1); ++blocki)
        {
            identical = (blocks_[blocki] == blockval);
        }
    }

    // Partial block: check manually
    for
    (
        label elemi = elem_per_block*(nblocks-1);
        identical && elemi < size();
        ++elemi
    )
    {
        identical = (val == get(elemi));
    }

    return identical;
}


151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
template<unsigned Width>
bool Foam::PackedList<Width>::equal(const PackedList<Width>& other) const
{
    if (size() != other.size())
    {
        return false;
    }

    const label nblocks = num_blocks(size());
    const auto& rhs = other.blocks_;

    for (label blocki = 0; blocki < nblocks; ++blocki)
    {
        if (blocks_[blocki] != rhs[blocki])
        {
            return false;
        }
    }

    return true;
}


174 175 176
template<unsigned Width>
Foam::labelList Foam::PackedList<Width>::values() const
{
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    return this->unpack<label>();
}


template<unsigned Width>
template<class IntType>
Foam::List<IntType>
Foam::PackedList<Width>::unpack() const
{
    static_assert
    (
        std::is_integral<IntType>::value,
        "Integral required for output."
    );
    static_assert
    (
        std::numeric_limits<IntType>::digits >= Width,
        "Width of IntType is too small to hold result"
    );

197
    if (empty())
198
    {
199 200 201 202 203
        return List<IntType>(0);
    }
    else if (uniform())
    {
        return List<IntType>(size(), static_cast<IntType>(get(0)));
204 205
    }

206
    List<IntType> output(size());
207 208 209 210 211 212 213 214 215 216 217
    label outi = 0;

    // Process n-1 complete blocks
    const label nblocks = num_blocks(size());

    for (label blocki=0; blocki < nblocks-1; ++blocki)
    {
        unsigned int blockval = blocks_[blocki];

        for (unsigned nget = elem_per_block; nget; --nget, ++outi)
        {
218
            output[outi] = IntType(blockval & max_value);
219 220 221 222 223 224 225 226 227 228 229 230 231 232
            blockval >>= Width;
        }
    }

    // Any partial blocks
    for (/*nil*/; outi < size(); ++outi)
    {
        output[outi] = get(outi);
    }

    return output;
}


233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
template<unsigned Width>
template<class IntType>
Foam::List<IntType>
Foam::PackedList<Width>::unpack(const labelRange& range) const
{
    static_assert
    (
        std::is_integral<IntType>::value,
        "Integral required for unpack output."
    );
    static_assert
    (
        std::numeric_limits<IntType>::digits >= Width,
        "Width of IntType is too small to hold unpack output."
    );


    // Could be more efficient but messier with block-wise access.
    // - automatically handles any invalid positions

    auto pos = range.start();

    List<IntType> output(range.size());

    for (IntType& out : output)
    {
        out = IntType(get(pos));
        ++pos;
    }

    return output;
}


template<unsigned Width>
template<class IntType>
Foam::List<IntType>
Foam::PackedList<Width>::unpack(const labelUList& locations) const
{
    static_assert
    (
        std::is_integral<IntType>::value,
        "Integral required for unpack output."
    );
    static_assert
    (
        std::numeric_limits<IntType>::digits >= Width,
        "Width of IntType is too small to hold unpack output."
    );


    label pos = 0;

    List<IntType> output(locations.size());

    for (IntType& out : output)
    {
        out = IntType(get(locations[pos]));
        ++pos;
    }

    return output;
}


298
// ************************************************************************* //