Skip to content

Instantly share code, notes, and snippets.

View roachsinai's full-sized avatar
🌴
On vacation

RoachZhao roachsinai

🌴
On vacation
View GitHub Profile
#include <cstddef>
#include <cstdlib>
template <typename R, auto getter, auto setter, size_t (*offset)()>
struct property {
inline R *self() {
return reinterpret_cast<R *>(reinterpret_cast<size_t>(this) - offset());
}
inline operator auto() { return (self()->*getter)(); }
inline void operator=(auto t) { (self()->*setter)(t); }
@roachsinai
roachsinai / .cpp
Created May 6, 2024 12:45 — forked from DongHeZheng/.cpp
One method of scaling, rotating and cropping image by using OpenCV
void imageScaleAndRotate(const cv::Mat &src, cv::Mat &out, double scale, double roll) {
// scaling
cv::Mat imSmall;
cv::resize(src, imSmall, cv::Size(), scale, scale);
// prepare for rotating
int width = imSmall.cols, height = imSmall.rows;
int diagonal = int(sqrt(height * height + width * width));
int offsetX = (diagonal - width) / 2, offsetY = (diagonal - height) / 2;
@roachsinai
roachsinai / bpe_tokenizer_from_scratch.py
Created April 13, 2025 04:41 — forked from xiabingquan/bpe_tokenizer_from_scratch.py
Building a BPE (Bpte-Pair Encoding) tokenizer from scratch.
# A minimal example of how to implement byte-pair encoding (BPE) tokenizer from scratch in Python.
# Reference: https://github.com/karpathy/minbpe
# Contact: [email protected]
def get_stats(byte_arr):
# get the frequency of each byte pair in the text
count = {}
for pair in zip(byte_arr[:-1], byte_arr[1:]): # e.g. pair: (b'a', b' ')
count[pair] = count.get(pair, 0) + 1