Skip to content

Instantly share code, notes, and snippets.

View ashwin's full-sized avatar

Ashwin Nanjappa ashwin

View GitHub Profile
@ashwin
ashwin / trie.cpp
Created November 2, 2015 02:54
Simple trie implemented in C++
// Adapted from Trie code in Algorithms (4 Ed) by Sedgewick
#include <iostream>
#include <string>
// Size of alphabet
constexpr int R = 256;
struct Node
{
@ashwin
ashwin / print_buckets.cpp
Created October 30, 2015 04:10
Print buckets and their linked list of keys in C++ unordered container
#include <unordered_set>
#include <iostream>
using IntHashTable = std::unordered_set<int>;
void PrintBuckets(const IntHashTable& htab)
{
for (auto bi = 0; bi < htab.bucket_count(); ++bi)
{
std::cout << "B[" << bi << "]:";
@ashwin
ashwin / math_defines.h
Created October 26, 2015 08:40
Math constants for C and C++. Copied from math.h of Visual Studio 2012.
/* Define _USE_MATH_DEFINES before including math.h to expose these macro
* definitions for common math constants. These are placed under an #ifdef
* since these commonly-defined names are not part of the C/C++ standards.
*/
/* Definitions of useful mathematical constants
* M_E - e
* M_LOG2E - log2(e)
* M_LOG10E - log10(e)
* M_LN2 - ln(2)
@ashwin
ashwin / fixed_width_ints.cpp
Created October 12, 2015 10:20
Size of fixed width integer types of cstdint
#include <cstdint>
#include <iostream>
int main()
{
std::cout << "Precise" << std::endl;
std::cout << sizeof(int8_t ) << std::endl;
std::cout << sizeof(int16_t ) << std::endl;
std::cout << sizeof(int32_t ) << std::endl;
@ashwin
ashwin / gl_error_check.cpp
Created September 14, 2015 05:56
Check error in OpenGL
const char * GetGLErrorStr(GLenum err)
{
switch (err)
{
case GL_NO_ERROR: return "No error";
case GL_INVALID_ENUM: return "Invalid enum";
case GL_INVALID_VALUE: return "Invalid value";
case GL_INVALID_OPERATION: return "Invalid operation";
case GL_STACK_OVERFLOW: return "Stack overflow";
case GL_STACK_UNDERFLOW: return "Stack underflow";
@ashwin
ashwin / depth_type_cvmat.cpp
Created August 27, 2015 04:46
Useful functions to get the depth or type of a cv::Mat
std::string GetMatDepth(const cv::Mat& mat)
{
const int depth = mat.depth();
switch (depth)
{
case CV_8U: return "CV_8U";
case CV_8S: return "CV_8S";
case CV_16U: return "CV_16U";
case CV_16S: return "CV_16S";
@ashwin
ashwin / list_ppa.sh
Created August 4, 2015 02:21
List PPA repositories on an Ubuntu system
#! /bin/sh
# listppa Script to get all the PPA installed on a system ready to share for reininstall
# From: http://askubuntu.com/questions/148932/how-can-i-get-a-list-of-all-repositories-and-ppas-from-the-command-line
for APT in `find /etc/apt/ -name \*.list`; do
grep -o "^deb http://ppa.launchpad.net/[a-z0-9\-]\+/[a-z0-9\-]\+" $APT | while read ENTRY ; do
USER=`echo $ENTRY | cut -d/ -f4`
PPA=`echo $ENTRY | cut -d/ -f5`
echo sudo apt-add-repository ppa:$USER/$PPA
done
@ashwin
ashwin / crtp_example.cpp
Created July 9, 2015 09:21
Simple C++ code to understand CRTP
#include <iostream>
using namespace std;
template <typename Derived>
struct Creature
{
int eye_num;
friend bool operator == (const Derived& lhs, const Derived& rhs)
{
@ashwin
ashwin / limits.cpp
Created July 2, 2015 03:00
Max, min and lowest in C++
#include <limits>
std::numeric_limits<int>::max() // INT_MAX
std::numeric_limits<int>::min() // INT_MIN
std::numeric_limits<int>::lowest() // INT_MIN
std::numeric_limits<float>::max() // FLT_MAX
std::numeric_limits<float>::min() // FLT_MIN
std::numeric_limits<float>::lowest() // -FLT_MAX