Skip to content

Instantly share code, notes, and snippets.

View ZJUGuoShuai's full-sized avatar
🇨🇳

Guo Shuai ZJUGuoShuai

🇨🇳
  • ByteDance
  • Hangzhou, China
  • 02:42 (UTC +08:00)
View GitHub Profile
@ZJUGuoShuai
ZJUGuoShuai / showOpenCVMat.mm
Created August 20, 2023 09:02
Display a OpenCV Mat using a UIImageView.
void showOpenCVMat(cv::Mat img, UIImageView* imageView) {
cv::Mat converted;
img.convertTo(converted, CV_8U);
// convert the cv::Mat to UIImage
NSData *data = [NSData dataWithBytes:converted.data length:converted.elemSize() * converted.total()];
CGColorSpaceRef colorSpace =
CGColorSpaceCreateDeviceGray(); // CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider =
CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGImageRef imageRef = CGImageCreate(
@ZJUGuoShuai
ZJUGuoShuai / thread-safe-singleton.md
Last active August 19, 2023 04:03
C++ 如何实现线程安全的单例模式

基本版本

class Singleton {
  Singleton() = default;

 public:
  Singleton(const Singleton&)            = delete;
  Singleton& operator=(const Singleton&) = delete;
@ZJUGuoShuai
ZJUGuoShuai / STL-Swap.md
Last active August 18, 2023 11:45
C++ STL 中的 std::swap 通常是如何实现的?

C++ STL 中的 std::swap 通常是如何实现的?

C++11 之前,只能使用拷贝构造和拷贝赋值:

template <typename T>
void swap(T& a, T& b) {
  T t = a;
  b = a;
 b = t;
@ZJUGuoShuai
ZJUGuoShuai / myVector.cpp
Last active August 19, 2023 16:00
My implementation of std::vector
template <typename T>
class vector {
size_t size_ = 0;
size_t capacity_ = 0;
T* data_ = nullptr;
public:
// Default constructor
vector() = default;
@ZJUGuoShuai
ZJUGuoShuai / bilinear_resize.py
Last active August 23, 2023 16:53
Bilinear Resize Implementation (Python)
def sample(img, x: float, y: float):
h, w = img.shape
left = math.floor(x)
top = math.floor(y)
right = left + 1
bottom = top + 1
# top left corner
if (0 <= left < w) and (0 <= top < h):
@ZJUGuoShuai
ZJUGuoShuai / epsilon.cc
Created August 8, 2023 08:57
Machine Epsilon 的作用
#include <limits>
#include <iostream>
int main() {
float eps = std::numeric_limits<float>::epsilon();
float a = 1.0f;
float b = a + eps;
float c = a + eps / 2.0f;
std::cout << "a == b: " << std::boolalpha << (a == b) << "\n"; // false
std::cout << "a == c: " << std::boolalpha << (a == c) << "\n"; // true

ComputeShader class 增加以下三个方法:

// 从 OpenCV Mat 创建(只读的) Metal Texture
- (id<MTLTexture>) textureFromCVMat:(const void*)cv_data Width:(int) w Height:(int)h {
  // Create a Metal texture descriptor
  MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR8Unorm
                                                                                               width:w
                                                                                              height:h
                                                                                           mipmapped:NO];
@ZJUGuoShuai
ZJUGuoShuai / dft.swift
Created July 28, 2023 15:04
Swift 实现 DFT(原始算法)
import Cocoa
let input: [Float] = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
var output_real = Array<Float>(repeating: 0, count: 8)
var output_imag = Array<Float>(repeating: 0, count: 8)
for i in 0..<8 {
var real_sum: Float = 0.0
var imag_sum: Float = 0.0
for k in 0..<8 {
@ZJUGuoShuai
ZJUGuoShuai / Timer.hpp
Last active October 24, 2023 11:44
用于方便地测量代码运行时间的 C++ class Timer
//
// Timer.h
// NLE
//
// Created by Guo Shuai on 2023/7/27.
//
#ifndef Timer_h
#define Timer_h
@ZJUGuoShuai
ZJUGuoShuai / fft.c
Created July 26, 2023 07:29
C 语言实现 DFT 和 FFT
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
// O(n^2)
void dft(double *input, double *real, double *imag, int n) {
for (int k = 0; k < n; k++) {
real[k] = 0;
imag[k] = 0;