Skip to content

Instantly share code, notes, and snippets.

View minjang's full-sized avatar

Minjang Kim minjang

  • Facebook
  • Menlo Park, CA
View GitHub Profile
@minjang
minjang / python-like-test1.cpp
Last active December 23, 2015 16:36
Python's enumerate implementation in C++
// 1. Forward iterator를 얻을 수 있는 STL 컨테이너
cout << "[TEST 1] vector<string>\n";
vector<string> A = {"foo", "bar", "baz"};
// 참조자로 직접 A 내용 수정.
for (pair<size_t, string&> p : enumerate(A))
cout << p.first << ": " << (p.second += p.second) << '\n';
// 수정 내역 확인: 벡터 원소를 상수 참조자로 받음.
for (pair<size_t, const string&> p : enumerate(A))
cout << p.first << ": " << p.second << '\n';
// 3. const 예제
cout << "[TEST 3] const\n";
const string E[] = {"foo", "bar", "baz"};
// decltype(p) == pair<size_t, string const&>&&
// p 자체는 상수가 아니므로 인덱스 값은 변경 가능, 배열 값은 수정 불가.
for (auto &&p : enumerate(E))
cout << (p.first += 1) << ": " << p.second << '\n';
// 5. 변수를 거치지 않고 직접 사용
cout << "[TEST 5] in-place through rvalue reference\n";
for (auto &&p : enumerate(range(100, 103)))
cout << p.first << ": " << p.second << '\n';
// decltype(p) == pair<size_t, string&>&&
for (auto &&p : enumerate(vector<string>{"foo", "bar", "baz"}))
cout << p.first << ": " << (p.second += p.second) << '\n';
// 함수 반환값 바로 사용
@minjang
minjang / python_like_range.cpp
Last active December 8, 2015 06:16
Python-like range implementation with C++11
#include <iostream>
template<typename T>
class range_iterator {
T cur_;
const T step_ = 1;
public:
range_iterator(T init) : cur_{init} {}
@minjang
minjang / python-like-test2.cpp
Created December 23, 2015 16:36
Python's enumerate implementation in C++
// 2. 일반 배열 예
cout << "[TEST 2] array\n";
string C[] = {"foo", "bar", "baz"};
// auto&&로 받는 것이 범위 기반 for 문에서 일반적이고 효율적인 방법
// p 타입: pair<size_t, string&>&&, 원소 타입이 string&로 추론
for (auto &&p : enumerate(C, 100))
cout << p.first << ": " << (p.second += p.second) << '\n';
for (auto &&p : enumerate(C, 100))
cout << p.first << ": " << p.second << '\n';
// 4. 앞서 구현한 range 사용 예
cout << "[TEST 4] range\n";
auto &&D = range(100, 103);
// decltype(p) == pair<size_t, int>&&
for (auto &&p : enumerate(D))
cout << p.first << ": " << p.second << '\n';
// 6. 초기화 리스트
cout << "[TEST 6] initializer list\n";
for (auto &&p : enumerate({"foo", "bar", "baz"}))
cout << p.first << ": " << p.second << '\n';
bool NStrideDetector::IsMergable(int64_t ea, int32_t /*size*/, const TRAINING& t) const
{
//
// Note: be careful of the sign for % operation
//
int64_t abs_stride = (t.distance > 0 ? t.distance : -t.distance);
int64_t lb = (t.low - abs_stride < t.low ? t.low - abs_stride : t.low/*0*/);
int64_t ub = (t.high + abs_stride > t.high ? t.high + abs_stride : t.high/*-1*/);
if (/*(t.size == size) &&*/ (((t.low - ea) % abs_stride) == 0) &&
@minjang
minjang / swap-unoptimized.ll
Last active November 28, 2016 19:36
Unoptimized LLVM bitcode of the swap example
; ModuleID = 'swap.bc'
source_filename = "swap.cpp"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"
; Function Attrs: ssp uwtable
define i32 @_Z4testv() #0 {
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
@minjang
minjang / swap-only-mem2reg.ll
Last active November 28, 2016 19:42
Applied only mem2reg pass
define linkonce_odr void @void temp_swap<int>(int&, int&)(i32* %a, i32* %b) #2 {
entry: ; %0
%0 = load i32, i32* %a, align 4 ; W %1
%1 = load i32, i32* %b, align 4 ; | W
store i32 %1, i32* %a, align 4 ; R |
store i32 %0, i32* %b, align 4 ; R
ret void
}
define linkonce_odr void @void xor_swap<int>(int&, int&)(i32* %a, i32* %b) #2 {