Skip to content

Instantly share code, notes, and snippets.

View imranjavaid's full-sized avatar

Imran Javaid imranjavaid

  • Chicago, IL
View GitHub Profile
@imranjavaid
imranjavaid / pairAsVoidParameter
Created August 24, 2021 15:19
How to pass std::pair as void to a function
#include <iostream>
#include <string>
void pairtest(void *p)
{
std::pair<int**, std::string> *pr =
static_cast<std::pair<int**, std::string>* >(p);
int **i = pr->first;
std::cout << **i << "\n";
@imranjavaid
imranjavaid / helper.cpp
Last active November 1, 2022 17:17
call C++ function from C passing back char* array
#include "helper.h"
#include <cstring>
#include <string>
#include <iostream>
extern "C" {
void get_names(char names[][LENGTH], int *nums, int count) {
std::string snames[] = { "abc", "def", "ghi", "xxx", "zzz", "aa", "BBBBB", "cc", "AAAA", "FFFFFFF" };
for (int i=0; i<count; i++) {
@imranjavaid
imranjavaid / zip_tmp.cpp
Last active November 2, 2022 19:24
C++ Metaprogramming - zip two vectors
// https://godbolt.org/z/5Yj9hv1dv
#include <tuple>
template <auto...>
struct Vector;
template <typename...>
struct zip;
@imranjavaid
imranjavaid / vec_map_set_test.cpp
Last active November 9, 2022 13:42
performance test of STL vec vs map vs set
#include <iostream>
#include <chrono>
#include <vector>
#include <random>
#include <algorithm>
#include <map>
#include <set>
auto vecTest(const int size, std::mt19937 &rng, std::uniform_int_distribution<std::mt19937::result_type> &dist) {
auto begin= std::chrono::system_clock::now();
@imranjavaid
imranjavaid / CopyStableWithoutDuplicates.cpp
Last active November 20, 2022 22:02
Copy Stable Without Duplicates
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <iostream>
// https://godbolt.org/z/Ehd85Ws18
// https://quick-bench.com/q/69J3sKtK9ScVWESLIrZDn7chyzY
template <typename T>
void copyStableWithoutDuplicates(const std::vector<T>& from, std::vector<T>& to) {
@imranjavaid
imranjavaid / copyStableOnlyDuplicates.cpp
Last active November 20, 2022 22:11
Copy Stable Only Duplicates
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <iostream>
// https://godbolt.org/z/Toc3T95oW
// https://quick-bench.com/q/tIZ8WWzcixs_ktnadmqa1T3t4Bw
template <typename T>
void copyStableOnlyDuplicates(const std::vector<T>& from, std::vector<T>& to) {
@imranjavaid
imranjavaid / VecTuple.cpp
Last active November 24, 2022 00:41
TMP: get vector of tuples of elements from a list of containers with elements (AKA zip)
#include <tuple>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <iostream>
#include <ranges>
#include <deque>
#include <concepts>
#include <cassert>
@imranjavaid
imranjavaid / fib.cpp
Created December 14, 2022 18:17
Fibonacci
#include <unordered_map>
#include <math.h>
#include <cassert>
class Fib {
public:
Fib() : last(1), memo{{0,0},{1,1}} { }
unsigned long operator()(unsigned int n) {
assert(n < 94); // overflow after this