Skip to content

Instantly share code, notes, and snippets.

View char-1ee's full-sized avatar

Li Xingjian char-1ee

  • Nanyang Technological University
  • Singapore
  • X @lix1ngjian
View GitHub Profile
@char-1ee
char-1ee / EFCoreApp.cs
Last active June 23, 2022 06:37
A trial .NET Core console app that performs data access against a SQLite database using Entity Framework Core.
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public string DbPath { get; }
@char-1ee
char-1ee / if-else-optimizations.md
Last active June 23, 2022 18:24
How to optimize if-else writing

If-else optimizations

With the growth of logics, long nested if-else writing could be a burden for code extensibility and maintainability. There are a series of optimization schemes. In the language syntax level, we have approaches like

  • Switch-case syntax, ternary conditional operator
  • Optional (JDK 8+) for nullable checks
  • Enum methods

In the logic/code structure level, we can do

  • Early return in if-else blocks / remove unnecessary else
  • Merges of condition expressions
@char-1ee
char-1ee / setup_dev_env.sh
Last active June 23, 2022 01:18
Script to set up C/C++/Networking development environment on Linux from Stanford.
#!/bin/sh
if [ -z "$SUDO_USER" ]; then
# if the user didn't call us with sudo, re-execute
exec sudo $0 "$@"
fi
### update sources and get add-apt-repository
apt-get update
apt-get -y install software-properties-common

Concurrent Database Access @Depricated @see link

This short article describes how to make access to your android database thread safe. Sample project is available [here][7].

Assuming you have your own [SQLiteOpenHelper][1].

public class DatabaseHelper extends SQLiteOpenHelper { ... }

Now you want to write data to database in separate threads.

Raw Database Model

I want to demonstrate Android "Raw database model". The basic concept is simple - use only raw queries which are stored inside res/values/queries.xml.

Base package classes

    com.sample.db.model

In C++, variable length arrays are not legal. g++ allows this as an "extension" (because C allows it), so in g++ (without being
-pedantic about following the C++ standard), you can do:

int n = 10;
double a[n]; // Legal in g++ (with extensions), illegal in proper C++

If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself:

int n = 10;

Clockwise/Spiral Rule

  • int* - pointer to int
  • int const * - pointer to const int
  • int * const - const pointer to int
  • int const * const - const pointer to const int

Now the first const can be on either side of the type so:

  • const int * == int const *
  • const int * const == int const * const

Initialize vector with hardcoded elements

In C++ 11:

#include <vector>
using std::vector;
vector<int> v1 {10, 20, 30};
vector<int> v2 = {10, 20, 30};

Using Boost list_of:

What is Rule of Three in C++?

If you need to explicitly declare either

  • the destructor,
  • copy constructor,
  • copy assignment operator

yourself, you probably need to explicitly declare all three of them.

What is Rule of Five in C++11?

From C++11 on, an object has 2 extra special member functions: the move constructor and move assignment.

To check whether givn x is an integer,

  • In Java, take the decimal part using % 1 and then check if is 0:

    return (x + EPSILON) % 1 <= 2 * EPSILON; // caution on double type
  • In cpp, check by using x - int(x) == 0.

  • In Javascript, check by Number.isInteger().