Giter Club home page Giter Club logo

utils's Introduction

utils

c++封装工具类

cpp/map_divider/map_divider.h

#include <functional>
#include <iostream>
#include <sstream>
#include <stdio.h>

/*************************************
 *  (Y * H)
 *        |
 *        |
 *        |
 *        |
 *        |_______.(W, H)
 *        |       |
 *        | block |
 *        |_______|_____________ (X * W)
 *      (0,0) 
 *
 *************************************/

template<
    int Y,
    int X,
    int H, 
    int W> 
class MapDivider {
public:
    MapDivider(void) {
        for (int y = 0; y < Y; y++) {
            for (int h = 0; h < H; h++) {
                for (int x = 0; x < X; x++) {
                    for (int w = 0; w < W; w++) {
                        buffer_[y][h][x][w] = y * X + x;
                    }
                }
            }
        } 

    }

    void Foreach(std::function<void(int i, uint8_t value)> f) {
        for (int i = 0; i < data_size; i++) {
            f(i, data[i]);
        }
    }

    void ForeachReverse(std::function<void(int i, uint8_t value)> f) {
        for (int i = 0; i < data_size; i++) {
            f(i, data[data_size - i - 1]);
        }
    }

    void Print(std::ostream& out) {
        Foreach([&](int i, uint8_t value) {
            if (i % (width) == 0) {
                out << "\n";
            }
            else if (i % W == 0) {
                out << " |";
            }
            if (i > 0 && i % session == 0) {
                out << " ";
                for (int n = 0; n < width; n++) {
                    out << "----";
                }
                for (int n = 1; n < X; n++) {
                    out << "--";
                }
                out << "\n";
            }
            char buf[8];
            snprintf(buf, sizeof(buf), " %2d ", value);
            out << buf;
        });
        out << "\n\n";
    }

 public:
    const uint32_t width     = X * W;
    const uint32_t height    = Y * H;
    const uint32_t session   = H * X * W;
    const uint32_t data_size = Y * X * H * W;
    const uint8_t *data      = (uint8_t*)buffer_;

 private:
    uint8_t buffer_[Y][H][X][W];
};


/*******************************************
    int main(void) {
        MapDivider<2,3,4,5>().Flatten(std::cout);
        MapDivider<5,4,3,2>().Flatten(std::cout);

        return 0;
    }

    output:
    0   0  |  1   1 
    0   0  |  1   1 
    0   0  |  1   1 
    ------------------
    2   2  |  3   3 
    2   2  |  3   3 
    2   2  |  3   3 

    0   0   0  |  1   1   1  |  2   2   2 
    0   0   0  |  1   1   1  |  2   2   2 
    0   0   0  |  1   1   1  |  2   2   2 
    0   0   0  |  1   1   1  |  2   2   2 
    ---------------------------------------
    3   3   3  |  4   4   4  |  5   5   5 
    3   3   3  |  4   4   4  |  5   5   5 
    3   3   3  |  4   4   4  |  5   5   5 
    3   3   3  |  4   4   4  |  5   5   5 
    ---------------------------------------
    6   6   6  |  7   7   7  |  8   8   8 
    6   6   6  |  7   7   7  |  8   8   8 
    6   6   6  |  7   7   7  |  8   8   8 
    6   6   6  |  7   7   7  |  8   8   8 

*******************************************/

utils's People

Contributors

zschong avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.