Compare commits

..

4 Commits

Author SHA1 Message Date
7fa822fc42 negabinary.cpp 2021-11-22 11:12:39 +00:00
bcafc4bdfb stl_rb_tree.cpp 2021-11-21 14:21:42 +00:00
fa13178e66 tree_iterator.cpp 2021-11-21 14:10:00 +00:00
a1835ad116 2048.cpp 2021-11-20 22:47:05 +00:00
4 changed files with 433 additions and 11 deletions

43
cpp/stl_rb_tree.cpp Normal file
View File

@@ -0,0 +1,43 @@
/*
VIM: let b:cf5build="clang -std=c++20 -O2 -pthread -lstdc++ -I. {SRC} -o {OUT}"
VIM: let b:cf5run="{OUT}"
*/
#include <iostream>
#include <exception>
#include <chrono>
#include <set>
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
std::set<int> s({1, 2, 3, 4, 5});
//......
auto it = s.begin();
for (int i = 0; i < 10; i++){
std::cout << *it++ << ", " << std::endl;
}
std::cout << "-------" << std::endl;
it = s.end();
for (int i = 0; i < 10; i++){
std::cout << *it-- << ", " << std::endl;
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -0,0 +1,61 @@
/*
VIM: let b:lcppflags="-std=c++2a -O2 -pthread -I."
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM-: let b:argv=""
*/
#include <iostream>
#include <exception>
#include <chrono>
#include <type_traits>
#include <iomanip>
template <typename T>
constexpr T negabinary(T v){
static_assert(std::is_integral_v<T>, "Only integral types expected.");
const T base = -2;
const T abs_base = abs(base);
std::make_unsigned_t<T> n = 0;
int i = 0;
while ( v ){
T r = v % base;
v /= base;
if ( r < 0){
v +=1;
r = abs_base - r;
}
n |= std::make_unsigned_t<T>(r) << i;
++i;
}
return n;
}
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
for (int i = -16; i < 17; ++i){
std::cout << std::setbase(10) << i << " == " << std::setbase(16) << std::showbase << negabinary(i) << std::endl;
}
//std::cout << negabinary(2.) << std::endl;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}

View File

@@ -15,12 +15,55 @@ void print(const std::vector<int>& row)
std::cout << std::endl; std::cout << std::endl;
} }
void push(std::vector<int> row) void print(const std::vector<std::vector<int>>& board)
{ {
print(row); for (const auto& row : board){
auto q = row.begin(); for (auto i : row){
auto pe = row.end(); std::cout << i << ',';
for (auto p = row.begin()+1; p != pe; ++p){ }
std::cout << std::endl;
}
}
struct OneDimVec
{
using board_t = std::vector<int>;
using iterator_t = int *;
enum {dimentions=1};
static iterator_t begin(board_t& b, int n){
return b.data();
}
static iterator_t end(board_t& b, int n){
return b.data()+b.size();
}
static iterator_t next(iterator_t it){
return it+1;
}
};
struct OneDimVecReverse
{
using board_t = std::vector<int>;
using iterator_t = board_t::reverse_iterator;
enum {dimentions=1};
static iterator_t begin(board_t& b, int n){
return b.rbegin();
}
static iterator_t end(board_t& b, int n){
return b.rend();
}
static iterator_t next(iterator_t it){
return it+1;
}
};
template <typename Traits>
void push(typename Traits::board_t& board, int n)
{
auto q = Traits::begin(board, n);
auto pe = Traits::end(board, n);
for (auto p = Traits::next(q); p != pe; p=Traits::next(p)){
if (*p==0){ if (*p==0){
continue; continue;
} }
@@ -31,25 +74,154 @@ void push(std::vector<int> row)
else if (*q == *p){ else if (*q == *p){
*q+=*p; *q+=*p;
*p = 0; *p = 0;
++q; q = Traits::next(q);
} }
else { else {
*++q = *p; q = Traits::next(q);
if (*q != *p){ *q = *p;
if (q != p){
*p = 0; *p = 0;
} }
} }
} }
print(row);
} }
template <typename T>
void test(typename T::board_t v){
std::cout << "--------------" << std::endl;
print(v);
if constexpr (T::dimentions == 1){
push<T>(v,0);
} else {
for (int i = 0; i < T::dim_size(v); ++i){
push<T>(v,i);
}
}
std::cout << "-" << std::endl;
print(v);
}
struct BoardTraits
{
using board_t=std::vector<std::vector<int>>;
enum {dimentions=2};
struct iterator_t {
board_t& b;
int i;
int j;
int& operator*(){
return b[i][j];
}
bool operator != (const iterator_t& it){
return i != it.i;
}
iterator_t& operator = (const iterator_t& it){
i=it.i;
j=it.j;
return *this;
}
};
};
struct Up : public BoardTraits
{
static int dim_size(const board_t& b){
return b[0].size();
}
static iterator_t begin(board_t& b, int n){
return {b, 0, n};
}
static iterator_t end(board_t& b, int n){
return {b, int(b.size()), n};
}
static iterator_t next(const iterator_t& it){
return {it.b, it.i+1, it.j};
}
};
struct Down : public BoardTraits
{
static int dim_size(const board_t& b){
return b[0].size();
}
static iterator_t begin(board_t& b, int n){
return {b, int(b.size())-1, n};
}
static iterator_t end(board_t& b, int n){
return {b, -1, n};
}
static iterator_t next(const iterator_t& it){
return {it.b, it.i-1, it.j};
}
};
struct Left : public BoardTraits
{
using iterator_t = std::vector<int>::iterator;
static int dim_size(const board_t& b){
return b.size();
}
static iterator_t begin(board_t& b, int n){
return b[n].begin();
}
static iterator_t end(board_t& b, int n){
return b[n].end();
}
static iterator_t next(const iterator_t& it){
return it+1;
}
};
struct Right : public BoardTraits
{
using iterator_t = std::vector<int>::reverse_iterator;
static int dim_size(const board_t& b){
return b.size();
}
static iterator_t begin(board_t& b, int n){
return b[n].rbegin();
}
static iterator_t end(board_t& b, int n){
return b[n].rend();
}
static iterator_t next(const iterator_t& it){
return it+1;
}
};
int main ( void ) int main ( void )
{try{ {try{
auto begin = std::chrono::high_resolution_clock::now(); auto begin = std::chrono::high_resolution_clock::now();
push({4, 0, 4, 8}); test<OneDimVec>({4, 0, 4, 8});
test<OneDimVecReverse>({4, 0, 4, 8});
std::cout << "test" << std::endl;
//...... //......
test<Left>({
{4, 0, 4, 8},
{2, 0, 2, 8},
{1, 1, 0, 0},
{4, 0, 4, 4}
});
test<Right>({
{4, 0, 4, 8},
{2, 0, 2, 8},
{1, 1, 0, 0},
{4, 0, 4, 4}
});
test<Up>({
{4, 0, 4, 8},
{2, 0, 2, 8},
{1, 1, 0, 0},
{4, 0, 4, 4}
});
test<Down>({
{4, 0, 4, 8},
{2, 0, 2, 8},
{1, 1, 0, 0},
{4, 0, 4, 4}
});
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin; std::chrono::duration<double> seconds = end - begin;

View File

@@ -0,0 +1,146 @@
/*
VIM: let b:cf5build="clang -std=c++20 -O2 -pthread -lstdc++ -I. {SRC} -o {OUT}"
VIM: let b:cf5run="{OUT}"
*/
#include <iostream>
#include <exception>
#include <chrono>
#include <vector>
struct node {
node * parent;
node * left;
node * right;
int val;
node(int v, node * p)
: parent(p)
, left(nullptr)
, right(nullptr)
, val(v)
{}
};
node * build_tree(const std::vector<int>& t){
node * root = nullptr;
bool left = true;
for (int v : t){
if (v!=-1){
node * n = new node(v, root);
if ( root ){
if ( left ){
root->left = n;
} else {
root->right = n;
}
}
root = n;
left = true;
} else {
if (!root){
break;
}
if (!left){
while ( root->parent && !left){
left = root->parent->left == root;
root = root->parent;
}
if (!left){
break;
}
}
left = false;
}
}
return root;
}
void print(const node * root){
if ( !root ){
std::cout << "-1";
} else {
std::cout << '(';
print(root->left);
std::cout << ',' << root->val << ',';
print(root->right);
std::cout << ')';
}
}
class tree_itor {
node * n;
public:
tree_itor()
: n(nullptr)
{}
tree_itor(node * _n)
: n(leftmost(_n))
{}
tree_itor& operator ++ () {
if (n->right){
n = leftmost(n->right);
} else {
while (n->parent && n->parent->right==n){
n = n->parent;
}
if (!n->parent){
n = nullptr;
} else {
n = n->parent;
}
}
return *this;
}
bool operator != (const tree_itor& it) const {
return n != it.n;
}
int& operator * (){
return n->val;
}
private:
node * leftmost(node * n){
while ( n->left ){
n = n->left;
}
return n;
}
};
int main ( void )
{try{
auto begin = std::chrono::high_resolution_clock::now();
std::cout << "------------------------" << std::endl;
node * tree = build_tree({1, 2, 3, 4, -1, 5, -1, -1, 6, -1, 7, -1, -1, 8, -1, -1, 4, 10, -1, -1, -1 });
print(tree);
std::cout << std::endl;
tree_itor it(tree);
tree_itor itEnd;
for ( ; it != itEnd; ++it ){
std::cout << *it << ", ";
}
std::cout << std::endl;
//......
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> seconds = end - begin;
std::cout << "Time: " << seconds.count() << std::endl;
return 0;
}
catch ( const std::exception& e )
{
std::cerr << std::endl
<< "std::exception(\"" << e.what() << "\")." << std::endl;
return 2;
}
catch ( ... )
{
std::cerr << std::endl
<< "unknown exception." << std::endl;
return 1;
}}