Compare commits

..

5 Commits

Author SHA1 Message Date
bc2ad07dbd arduino/bluetooth 2022-11-17 23:20:57 +00:00
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
7 changed files with 603 additions and 11 deletions

View File

@@ -0,0 +1,47 @@
// Basic Bluetooth sketch HC-06_01
// // Connect the Hc-06 module and communicate using the serial monitor
// //
// // The HC-06 defaults to AT mode when first powered on.
// // The default baud rate is 9600
// // The Hc-06 requires all AT commands to be in uppercase. NL+CR should not be
// added to the command string
// //
//
//
// #include <SoftwareSerial.h>
// SoftwareSerial BTserial(2, 3); // RX | TX
// // Connect the HC-06 TX to the Arduino RX on pin 2.
// // Connect the HC-06 RX to the Arduino TX on pin 3 through a voltage
// divider.
// //
//
//
// void setup()
// {
// Serial.begin(9600);
// Serial.println("Enter AT commands:");
//
// // HC-06 default serial speed is 9600
// BTserial.begin(9600);
// }
//
// void loop()
// {
//
// // Keep reading from HC-06 and send to Arduino
// Serial Monitor
// if (BTserial.available())
// {
// Serial.write(BTserial.read());
// }
//
// // Keep reading from
// Arduino Serial Monitor
// and send to HC-06
// if
// (Serial.available())
// {
// BTserial.write(Serial.read());
// }
//
// }

View File

@@ -0,0 +1,21 @@
#!/usr/bin/python
#
# - Pair bluetooth useing bluetoothctl.
# agent on
# pair 20:16:02:14:58:51
# - Setup serial connection:
# sudo rfcomm bind hci0 20:16:02:14:58:51 1
#
# - For module
# sudo apt-get install python-serial
#
import serial
from time import sleep
bluetoothSerial = serial.Serial( "/dev/rfcomm0", baudrate=9600 )
bluetoothSerial.write( "Test from python." )
print bluetoothSerial.readline()

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

@@ -0,0 +1,102 @@
/*
VIM: let b:lcppflags="-std=c++11 -O2 -pthread"
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
VIM: let b:argv=""
*/
#include <vector>
<<<<<<< HEAD
#include <string>
||||||| merged common ancestors
=======
#include <stack>
>>>>>>> preorder tree construction: much better algorithm. No need to keep root.
#include <iostream>
struct node {
node * left = nullptr;
node * right = nullptr;
double data = 0;
node( double d )
: data(d)
{}
};
node * prefix_to_tree( const std::vector<double>& v ){
if ( !v.size() ){
return nullptr;
}
node * root = new node(v[0]);
std::stack<node*> stack;
stack.push(root);
for ( int i = 1; i < v.size(); ) {
if ( v[i] < stack.top()->data ){
auto n = new node(v[i++]);
stack.top()->left = n;
stack.push(n);
} else {
auto c = stack.top();
stack.pop();
while( !stack.empty() && stack.top()->data < v[i] ){
c = stack.top();
stack.pop();
}
auto n = new node(v[i++]);
c->right = n;
stack.push(n);
}
}
return root;
}
void print( node * root, const std::string& level = "" ){
if ( root ) {
print( root->left, level + " ");
std::cout << level << "- " << root->data << std::endl;
print( root->right, level + " ");
}
}
void traverse( node * root, std::vector<double>& v ){
if ( root ) {
v.push_back(root->data);
traverse(root->left, v);
traverse(root->right, v);
}
}
void test( const std::vector<double>& v ){
std::cout << "-----------------------------------------------------" << std::endl;
auto r = prefix_to_tree( v );
print( r );
std::vector<double> w;
traverse( r, w );
if ( v.size() != w.size() ) {
std::cout << "v.size() != w.size() => " << v.size() << " != " << w.size() << std::endl;
}
for (int i = 0; i < v.size(); ++i ){
if (v[i]!=w[i]){
std::cout << "error at index " << i << " -> "<< v[i] << " != " << w[i] << std::endl;
}
}
}
int main( void ) {
test( {1} );
test( {2, 1, 3} );
test( {4, 2, 1, 3, 6, 5, 7} );
test( {8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15} );
test( {8, 4, 2, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15} );
test( {8, 4, 6, 5, 7, 12, 10, 9, 14, 13, 15} );
test( {8, 12, 10, 9, 11, 14, 13, } );
test( {8, 4, 2, 1, 6, 12, 10, 11, 14, 15} );
return 0;
}

View File

@@ -15,12 +15,55 @@ void print(const std::vector<int>& row)
std::cout << std::endl;
}
void push(std::vector<int> row)
void print(const std::vector<std::vector<int>>& board)
{
print(row);
auto q = row.begin();
auto pe = row.end();
for (auto p = row.begin()+1; p != pe; ++p){
for (const auto& row : board){
for (auto i : row){
std::cout << i << ',';
}
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){
continue;
}
@@ -31,25 +74,154 @@ void push(std::vector<int> row)
else if (*q == *p){
*q+=*p;
*p = 0;
++q;
q = Traits::next(q);
}
else {
*++q = *p;
if (*q != *p){
q = Traits::next(q);
*q = *p;
if (q != p){
*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 )
{try{
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();
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;
}}