Skip to content

Instantly share code, notes, and snippets.

View BewareMyPower's full-sized avatar
🏠
Working from home

Yunze Xu BewareMyPower

🏠
Working from home
View GitHub Profile
@BewareMyPower
BewareMyPower / quicksort.go
Last active July 4, 2019 13:07
quick sort implementation in Golang
package mysort
type Sorter interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
func Sort(data Sorter) {
quicksort(0, data.Len()-1, data)
@BewareMyPower
BewareMyPower / readlines.go
Last active July 6, 2019 13:26
read every line of a file and format line number in proper leading spaces
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
)
@BewareMyPower
BewareMyPower / batch_format.sh
Created July 20, 2019 07:22
use clang-format format c/c++ files
#!/bin/bash
Usage() {
echo "Usage: "$0" directory [maxdepth=1]"
exit 1
}
SHELL_DIR=$(dirname $0)
if [[ $# -lt 1 || $1 = "-h" || $1 = "--help" ]]; then
Usage
fi
@BewareMyPower
BewareMyPower / CumulativeAckForPartitionedTopic.java
Last active April 23, 2020 03:17
Pulsar Java client send cumulative ack on a partitioned topic
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.*;
public class CumulativeAck {
public static void main(String[] args) {
final String SERVICE_URL = "pulsar://localhost:6650";
final String ADMIN_URL = "http://localhost:8080";
final String TOPIC_NAME = "public/default/FooTest";
final int NUM_PARTITIONS = 3;
@BewareMyPower
BewareMyPower / SendAsyncThenClose.cpp
Created April 26, 2020 04:54
send async messages and close immediately
#include <pulsar/Client.h>
#include <assert.h>
#include <sstream>
using namespace pulsar;
int main(int argc, char* argv[]) {
Client client("pulsar://localhost:6650");
ProducerConfiguration producerConfig;
producerConfig.setSendTimeout(1);
@BewareMyPower
BewareMyPower / jsoncpp_demo.cc
Created June 1, 2020 17:46
Demo of JsonCpp
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
// https://github.com/open-source-parsers/jsoncpp
#include <json/json.h>
static std::string json_data = R"(// Configuration options
{
@BewareMyPower
BewareMyPower / list_ops.h
Last active July 14, 2020 16:00
ListOperationTemplate
#pragma once
#include <functional>
#include <iostream>
#include <vector>
struct ListNode {
int val;
ListNode* next = nullptr;
ListNode(int val_ = 0) : val(val_) {}
@BewareMyPower
BewareMyPower / matrix.h
Last active July 22, 2020 15:46
matrix for dp
#pragma once
#if __cplusplus < 201402L
#error "C++ standard must be at least 14"
#endif
#include <iostream>
#include <vector>
template <typename T>
@BewareMyPower
BewareMyPower / CustomLoggerTest.cc
Created August 31, 2020 04:08
Unit test for custom logger of pulsar cpp client
#include <gtest/gtest.h>
#include <pulsar/Client.h>
#include <fstream>
using namespace pulsar;
static std::string kLogPath = "custom_logger.txt";
static std::string kPrefix = "hello world";
class MyLogger : public Logger {
// g++ ObjectPool.cc -std=c++14 -pthread -O2
#include "ObjectPool.h" // https://github.com/apache/pulsar/tree/master/pulsar-client-cpp/lib/ObjectPool.h
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
inline auto now() { return std::chrono::high_resolution_clock::now(); }