You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
142 lines
4.1 KiB
C
142 lines
4.1 KiB
C
|
1 month ago
|
/*------------------------------------------------------------------------------
|
||
|
|
* Copyright (c) 2023 by Bai Bing (seread@163.com)
|
||
|
|
* See COPYING file for copying and redistribution conditions.
|
||
|
|
*
|
||
|
|
* Alians IT Studio.
|
||
|
|
*----------------------------------------------------------------------------*/
|
||
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <stdexcept>
|
||
|
|
#include <iterator>
|
||
|
|
#include <iostream>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
#include "_Define.h"
|
||
|
|
|
||
|
|
namespace ais
|
||
|
|
{
|
||
|
|
//================================================================================
|
||
|
|
/// A Shape Class
|
||
|
|
class AIS_EXPORT Shape
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
//====================================Attributes==============================
|
||
|
|
size_t rows{0};
|
||
|
|
size_t cols{0};
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Constructor
|
||
|
|
///
|
||
|
|
constexpr Shape() = default;
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Constructor
|
||
|
|
///
|
||
|
|
/// @param squareSize
|
||
|
|
///
|
||
|
|
constexpr explicit Shape(size_t squareSize) noexcept : rows(squareSize),
|
||
|
|
cols(squareSize)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Constructor
|
||
|
|
///
|
||
|
|
/// @param rows
|
||
|
|
/// @param columns
|
||
|
|
///
|
||
|
|
constexpr Shape(size_t rows, size_t columns) noexcept : rows(rows),
|
||
|
|
cols(columns)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Equality operator
|
||
|
|
///
|
||
|
|
/// @param otherShape
|
||
|
|
///
|
||
|
|
/// @return bool
|
||
|
|
///
|
||
|
|
bool operator==(const Shape &otherShape) const noexcept
|
||
|
|
{
|
||
|
|
return rows == otherShape.rows && cols == otherShape.cols;
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Not equality operator
|
||
|
|
///
|
||
|
|
/// @param otherShape
|
||
|
|
///
|
||
|
|
/// @return bool
|
||
|
|
///
|
||
|
|
bool operator!=(const Shape &otherShape) const noexcept
|
||
|
|
{
|
||
|
|
return !(*this == otherShape);
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Returns the size of the shape
|
||
|
|
///
|
||
|
|
/// @return size
|
||
|
|
///
|
||
|
|
size_t size() const noexcept
|
||
|
|
{
|
||
|
|
return rows * cols;
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Returns whether the shape is null (constructed with the
|
||
|
|
/// default constructor).
|
||
|
|
///
|
||
|
|
/// @return bool
|
||
|
|
///
|
||
|
|
bool is_null() const noexcept
|
||
|
|
{
|
||
|
|
return rows == 0 && cols == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Returns whether the shape is square or not.
|
||
|
|
///
|
||
|
|
/// @return bool
|
||
|
|
///
|
||
|
|
bool is_square() const noexcept
|
||
|
|
{
|
||
|
|
return rows == cols;
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Returns the shape as a string representation
|
||
|
|
///
|
||
|
|
/// @return std::string
|
||
|
|
///
|
||
|
|
std::string str() const
|
||
|
|
{
|
||
|
|
std::string out = "[" + std::to_string(rows) + ", " + std::to_string(cols) + "]\n";
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// Prints the shape to the console
|
||
|
|
///
|
||
|
|
void print() const
|
||
|
|
{
|
||
|
|
std::cout << *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
//============================================================================
|
||
|
|
/// IO operator for the Shape class
|
||
|
|
///
|
||
|
|
/// @param stream
|
||
|
|
/// @param shape
|
||
|
|
///
|
||
|
|
/// @return std::ostream
|
||
|
|
///
|
||
|
|
friend std::ostream &operator<<(std::ostream &stream, const Shape &shape)
|
||
|
|
{
|
||
|
|
stream << shape.str();
|
||
|
|
return stream;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace ais
|