coding theory – Understanding a CRC32 Implementation

I’m currently trying to understand an implementation of CRC32 about which I have a question.

On this page at section 6, there is the following code:

public uint Compute_CRC32_Simple(byte() bytes)
{
    const uint polynomial = 0x04C11DB7; /* divisor is 32bit */
    uint crc = 0; /* CRC value is 32bit */

    foreach (byte b in bytes)
    {
        crc ^= (uint)(b << 24); /* move byte into MSB of 32bit CRC */

        for (int i = 0; i < 8; i++)
        {
            if ((crc & 0x80000000) != 0) /* test for MSB = bit 31 */
            {
                crc = (uint)((crc << 1) ^ polynomial);
            }
            else
            {
                crc <<= 1;
            }
        }
    }

    return crc;
}

I’m particularly interested in understanding this line: crc ^= (uint)(b << 24); /* move byte into MSB of 32bit CRC */

What are the mathematics that make this line possible, both the shifting of the current byte (turned into an int) by 24 and the following XOR with the current crc? Unfortunately, the author doesn’t go into detail regarding this.

plotting – Question about the implementation of “Filling”

I was working with mathematica and I need to use “Filling”, however I notice that it doesn’t work when you have “small” numbers, for example if I make the following:

Plot(Evaluate({x + 1, x + 2}), {x, -1*10^-15,1*10^-15},PlotRange -> All, Filling -> {1 -> {2}})

The “Filling” doesn’t appears.

Question

There is some way to make that the “Filling” appear?

Extra

Since this question appear in a physical context, I know that I can change units in order to make “x” a larger number.(Actually I do this.)

innodb – MySQL Code implementation of locking and transactions

I am trying to understand the internals of how MySql (using InnoDB) implements locking and transactions in code. I understand the different types and concepts of locks and transactions, but I really want to deep dive into understanding how MySQL internally implements in code. I tried going through the documentation and MySQL open source code, but could not find a good starting point. Google was no help either. All search results were talking about the different types of locks, but not how they are implemented actually. Any help here?

postgresql – Optimizing implementation of custom ULID generation in Postgres

Following the spec github.com/ulid/spec
I’m trying to implement this on Postgres, with some tweaks (without monolitic implementation, no base32):

  • 43 bits allocated to timestamp instead of 48
  • 85 bits allocated to randomness instead of 80

This function is planned to replace uuid generator.
I’m beginner but all theses casts and allocating each byte one by one seems dirty, any way to optimize this ?
Should I switch into C ?

CREATE OR REPLACE FUNCTION gen_custom_ulid() RETURNS bytea AS $$
DECLARE
    unix bit(43);
    --gen_random_bytes from pgcrypto extension
    random_bytes bytea = gen_random_bytes(11);
    ulid bytea = 'x00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00';
BEGIN
    unix := (EXTRACT(EPOCH FROM clock_timestamp())*1000)::bigint::bit(43);

    ulid := SET_BYTE(ulid, 0, (unix)::bit(8)::integer);
    ulid := SET_BYTE(ulid, 1, (unix << 8)::bit(8)::integer);
    ulid := SET_BYTE(ulid, 2, (unix << 16)::bit(8)::integer);
    ulid := SET_BYTE(ulid, 3, (unix << 24)::bit(8)::integer);
    ulid := SET_BYTE(ulid, 4, (unix << 32)::bit(8)::integer);
    ulid := SET_BYTE(ulid, 5, ((unix << 40)::bit(3)||GET_BYTE(random_bytes,0)::bit(5))::bit(8)::integer);
    ulid := SET_BYTE(ulid, 6, GET_BYTE(random_bytes, 1));
    ulid := SET_BYTE(ulid, 7, GET_BYTE(random_bytes, 2));
    ulid := SET_BYTE(ulid, 8, GET_BYTE(random_bytes, 3));
    ulid := SET_BYTE(ulid, 9, GET_BYTE(random_bytes, 4));
    ulid := SET_BYTE(ulid, 10, GET_BYTE(random_bytes, 5));
    ulid := SET_BYTE(ulid, 11, GET_BYTE(random_bytes, 6));
    ulid := SET_BYTE(ulid, 12, GET_BYTE(random_bytes, 7));
    ulid := SET_BYTE(ulid, 13, GET_BYTE(random_bytes, 8));
    ulid := SET_BYTE(ulid, 14, GET_BYTE(random_bytes, 9));
    ulid := SET_BYTE(ulid, 15, GET_BYTE(random_bytes, 10));

    RETURN ulid;
END $$ LANGUAGE plpgsql VOLATILE PARALLEL SAFE;

Is this implementation of factory design pattern in C#?

Here are my class library code which create credit card.

public abstract class CreditCard
{
    public CardType CardType { get; protected set; }
    public decimal CreditLimit { get; protected set; }
    public decimal AnnualCharge { get; protected set; }
}

public class GoldCreditCard : CreditCard
{
    private GoldCreditCard(decimal creditLimit, decimal annualCharge)
    {
        CardType = CardType.Gold;
        CreditLimit = creditLimit;
        AnnualCharge = annualCharge;
    }

    /// <summary>
    /// Factory pattern
    /// </summary>
    /// <returns></returns>
    public static CreditCard Create()
    {
        return new GoldCreditCard(creditLimit: 15000, annualCharge: 100);
    }
}

public class TitaniumCreditCard : CreditCard
{
    private TitaniumCreditCard(decimal creditLimit, decimal annualCharge)
    {
        CardType = CardType.Titanium;
        CreditLimit = creditLimit;
        AnnualCharge = annualCharge;
    }

    /// <summary>
    /// Factory pattern
    /// </summary>
    /// <returns></returns>
    public static CreditCard Create()
    {
        return new TitaniumCreditCard(creditLimit: 30000, annualCharge: 250);
    }
}

and the ui will be:

    static void Main(string() args)
    {
        CreditCard card;

        System.Console.WriteLine("Select your card type");
        System.Console.WriteLine("1. Gold Credit Card");
        System.Console.WriteLine("2. Titanium Credit Card");
        var option = System.Console.ReadLine();
        switch (option)
        {

            case "1":
                card = GoldCreditCard.Create();
                PrintCard(card);
                break;
            case "2":
                card = TitaniumCreditCard.Create();
                PrintCard(card);
                break;
            default:
                break;
        }

    }

    static void PrintCard(CreditCard card)
    {
        System.Console.WriteLine($"Your credit card has been successfully created.");
        System.Console.WriteLine($"The credit card type is {card.CardType}.");
        System.Console.WriteLine($"The credit card limit is {card.CreditLimit:C}.");
        System.Console.WriteLine($"The credit card annual fee is {card.AnnualCharge:C}.");
    }

Is this implementation of factory design pattern in C#?

Simple-ish Rust implementation of ‘cat’

I’ve seen some other implementations of cat (even in Rust) on this site, but none attempted to completely implement the GNU cat CLI as far as I can tell. This version that I am developing is complete (minus the -u parameter that is ignored by GNU cat anyway) and, as far as I can tell, works.

Some problems:

  • It requires proper Unicode unless -A, -v, or -e are passed.
  • The code is a bit of a mess and could use cleaning up. Mainly this involves the way I have divided it into functions as well as my implementation of error handling.
  • I repeat myself a couple of times here and there.
  • It’s all in one file. (This one is easily fixed.)

After all, the reason I am doing this is to learn. Could someone show me where my code could be best improved? Be as pedantic as you’d like. Thanks in advance.

(Anyway, here’s the code.)

use common::Arg;
use std::default::Default;
use std::env;
use std::fmt::Debug;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::io::BufReader;

fn show_help(program_name: &str) {
    print!("Usage: {0} (options) <file>...
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
  Options:
    -A, --show-all          Equivalent to -vET
    -b, --number-nonblank   Number all non-empty output lines, starting with 1
    -e                      Equivalent to -vE
    -E, --show-ends         Display a '$' after the end of each line
    -n, --number            Number all output lines, starting with 1; ignored if -b was passed
    -s, --squeeze-blank     Suppress repeated adjacent blank lines, output one empty line instead of many
    -t                      Equivalent to -vT
    -T, --show-tabs         Display TAB characters as '^I'
    -v, --show-nonprinting  Display control characters (except for LFD and TAB) using '^' notation and precede characters that have the high bit set with 'M-'
  {0} normally reads and writes in binary mode, unless one of -bensAE is passed or standard output is a terminal. An exit status of zero indicates success, and a nonzero value indicates failure.
  ", program_name);
}

#(derive(Debug))
struct Configuration {
    show_nonprinting: bool,
    show_tabs: bool,
    squeeze_blanks: bool,
    number_lines: bool,
    show_line_ends: bool,
    number_nonblanks: bool,
}

impl Default for Configuration {
    fn default() -> Self {
        Configuration {
            show_nonprinting: false,
            show_tabs: false,
            show_line_ends: false,
            squeeze_blanks: false,
            number_lines: false,
            number_nonblanks: false,
        }
    }
}

fn main() -> Result<(), ()> {
    let raw_args: Vec<String> = env::args().collect();
    let args = common::parse_args(&raw_args(1..));

    let mut config = Configuration::default();
    let mut files: Vec<String> = vec!();

    for arg in args {
        match arg {
            Arg::Flag(text) => match text.as_str() {
                "h" | "help" => {
                    show_help(&raw_args(0));
                    return Ok(());
                }
                "A" | "show-all" => {
                    config.show_nonprinting = true;
                    config.show_line_ends = true;
                    config.show_tabs = true;
                }
                "b" | "number-nonblank" => {
                    config.number_nonblanks = true;
                }
                "e" => {
                    config.show_nonprinting = true;
                    config.show_line_ends = true;
                }
                "E" | "show-ends" => {
                    config.show_line_ends = true;
                }
                "n" | "number" => {
                    if !config.number_nonblanks {
                        config.number_lines = true;
                    }
                }
                "s" | "squeeze-blank" => {
                    config.squeeze_blanks = true;
                }
                "t" => {
                    config.show_nonprinting = true;
                    config.show_tabs = true;
                }
                "T" | "show-tabs" => {
                    config.show_tabs = true;
                }
                "v" | "show-nonprinting" => {
                    config.show_nonprinting = true;
                }
                _ => {
                    show_help(&raw_args(0));
                    return Err(());
                }
            },
            Arg::Positional(text) => {
                files.push(text);
            }
            Arg::FlagEquals(_, _) => {
                // no x=y flags in cat
                show_help(&raw_args(0));
                return Err(());
            }
        }
    }
    if files.len() == 0 {
        files.push("-".to_owned());
    }

    let config = config;
    let files = files;

    for file in files {
        if file == "-" {
            let stdin_handle = io::stdin();
            let stdin_reader = BufReader::new(stdin_handle);
            if let Err(e) = cat(stdin_reader, &config) {
                eprintln!("ERROR: reading stdin: {}", e);
                return Err(());
            }
        } else {
            let file_handle = File::open(&file).map_err(|e| {
                eprintln!("ERROR: opening {}: {}", file, e);
                ()
            })?;
            let file_reader = BufReader::new(file_handle);
            if let Err(e) = cat(file_reader, &config) {
                eprintln!("ERROR: reading {}: {}", file, e);
                return Err(());
            }
        }
    }

    Ok(())
}

fn cat<F>(mut reader: BufReader<F>, config: &Configuration) -> io::Result<()>
where
    F: io::Read,
{
    let mut line = 1u64;
    let mut prev_line_was_blank = false;
    // builds a UTF-8 char when necessary
    let mut utf8_char: Vec<u8> = vec!();
    let mut utf8_bytes_rem = 0;
    loop {
        let buf = reader.fill_buf()?;
        let len = buf.len();
        if len == 0 {
            break;
        }
        let mut line_buf = String::new();
        for c in buf {
            let mut cs = if config.show_nonprinting {
                print_char_escaped(*c)
            } else {
                String::new()
            };
            if config.show_tabs && (*c == 9 || *c == 0b10001001) {
                if *c & 0b10000000 != 0 {
                    cs = String::from("M-");
                }
                cs += &format!("^I");
            }
            if cs.is_empty() {
                // non-special
                if *c == b'n' {
                    let current_line_is_blank =
                        (config.number_nonblanks || config.squeeze_blanks) && line_buf.is_empty();
                    if config.show_line_ends {
                        line_buf.push('$');
                    }
                    if !(config.squeeze_blanks && current_line_is_blank && prev_line_was_blank) {
                        // line should be printed
                        if config.number_lines
                            || (config.number_nonblanks && !current_line_is_blank)
                        {
                            // number line
                            print!("{:6}  ", line);
                            line += 1;
                        }
                        // print line
                        println!("{}", line_buf);
                        line_buf = String::new();
                    }
                    prev_line_was_blank = current_line_is_blank;
                } else {
                    if utf8_bytes_rem == 0 && *c & 0xc0 != 0xc0 {
                        line_buf.push(*c as char);
                    } else if *c & 0xc0 == 0xc0 {
                        // start of Unicode char
                        let mut mask = 0x20;
                        utf8_bytes_rem = 1;
                        while *c & mask != 0 {
                            if mask == 0x02 {
                                // bad Unicode!
                                return Err(io::Error::new(
                                    io::ErrorKind::InvalidData,
                                    "Invalid UTF-8",
                                ));
                            }
                            utf8_bytes_rem += 1;
                            mask >>= 1;
                        }
                        utf8_char.push(*c);
                    } else {
                        // continued Unicode char
                        if *c & 0xc0 != 0x80 {
                            // bad continuation!
                            return Err(io::Error::new(
                                io::ErrorKind::InvalidData,
                                "Invalid UTF-8",
                            ));
                        }
                        utf8_bytes_rem -= 1;
                        utf8_char.push(*c);
                        if utf8_bytes_rem == 0 {
                            // should not panic; validity was checked!
                            let uc = std::str::from_utf8(&utf8_char).unwrap();
                            line_buf.push_str(uc);
                        }
                        utf8_char = vec!();
                    }
                }
            } else {
                line_buf.push_str(&format!("{}", cs));
            }
        }
        reader.consume(len);
    }
    Ok(())
}

fn print_char_escaped(c: u8) -> String {
    let mut c = c;
    // do non-printing checks
    let (meta, high_bit) = if c & 0b10000000 != 0 {
        // high bit set, unset
        c &= !0b10000000;
        (format!("M-"), true)
    } else {
        (String::new(), false)
    };
    let c = c;
    // now check for control chars
    let cs = match c {
        // null char
        0 => format!("^@"),
        // control char is represented as letter
        1..=26 => {
            if high_bit {
                format!("^{}", (c + 64) as char)
            } else {
                match c {
                    // TAB, ignore; it is handled by config.show_tabs
                    9 => String::new(),
                    // LF, ignore; it is handled by config.show_line_ends
                    10 => String::new(),
                    // other control chars are treated the same
                    _ => format!("^{}", (c + 64) as char),
                }
            }
        }
        // ESC
        27 => format!("^("),
        // FS
        28 => format!("^\"),
        // GS
        29 => format!("^)"),
        // RS
        30 => format!("^^"),
        // US
        31 => format!("^_"),
        // DEL
        127 => format!("^?"),
        // printable char
        _ => format!("{}", c as char),
    };
    meta + cs.as_str()
}

common is my own crate which implements the basic argument parsing used here. I can supply it if someone wants it.

algorithm – Trie implementation for strings in Swift

A trie for handling strings for an autocomplete dictionary. This passes my fairly casual tests, though it’s always possible that there are broken edge cases, but I’m mainly concerned about design and efficiency: is this a sensible way to implement this data structure? (In particular, is it sensible to do the insert method recursively?). It feels a little gnarly to me.

struct Trie {
    var root: Node
    init() {
        root = Node()
    }
    func search(_ word: String) -> Bool {
        let letters = Array(word)
        var curnode = root
        for letter in letters {
            guard let match = curnode.children.first(where: {(key, _) in
                key == letter
            })
                else {
                    return false
            }
            curnode = match.value
        }
        if curnode.contained {
            return true
        }
        return false
    }
    
    func remove(_ word: String) {
        let letters = Array(word)
        var curnode = root
        for letter in letters {
            if !curnode.children.contains(where: {(key, _) in
                key == letter
            }) {
                break
            } else {
                curnode = curnode.children(letter)!
            }
        }
        curnode.contained = false
    }
    
    func insert(_ letters: (Character), parent: Node) -> Node {
        if letters.count == 1 {
            let letter = letters(0)
            if parent.children.contains(where: {(key, _) in
                key == letter
            }) {
                let newNode = parent
                newNode.children(letter)!.contained = true
                return newNode
            } else {
                let newNode = Node(letter, final: true)
                return newNode
            }
        } else {
            let first = letters(0)
            let rest = Array(letters.dropFirst())
            if let subtree = parent.children.first(where: {(key, _) in
                           key == first
            }) {
                let newNode = Node(first, final: subtree.value.contained, kids: subtree.value.children)
                newNode.children(rest(0)) = insert(rest, parent: newNode)
                return newNode

            } else {
            let newNode = Node(first, final: false)
            newNode.children(rest(0)) = insert(rest, parent: newNode)
            return newNode
            }
        }
    }
    mutating func insert(_ word: String) {
        let new_subtree = insert(Array(word), parent: root)
        root.children(new_subtree.char!) = new_subtree
    }
}

class Node {
    var char: Character?
    var children: (Character:Node)
    var contained: Bool
    init() {
        char = nil
        children = (:)
        contained = false
    }
    init(_ c: Character, final: Bool) {
        children = (:)
        contained = final
        char = c
    }
    init(_ c: Character, final: Bool, kids: (Character:Node)) {
        children = kids
        contained = final
        char = c
    }
}

algorithm – Hamming distance implementation in Java

I’m exploring String relativity and comparison techniques, and I found Hamming distance algorithm in my search so I give a try to implement that approach in Java and below I’m sharing my implementation.

Please let me know if this approach is correct or I need to improve it more.

HammingDistance.java

public interface HammingDistance {

 public int distance(String first,String second);
    
}

HammingDistanceImpl.java

public class HammingDistanceImpl implements HammingDistance {

    @Override
    public int distance(String first, String second) {

        first = first.trim().toLowerCase();
        first = flipFyString(first);

        second = second.trim().toLowerCase();
        second = flipFyString(second);

        // System.out.println(first + " " + second);

        if (first.length() - second.length() == 1 || first.length() - second.length() == 2
                || first.length() - second.length() == 3) {
            // System.out.println("Second String bit shorter in length to First String");
            return calculatedDistance(second, first);
        }

        if (second.length() - first.length() == 1 || second.length() - first.length() == 2
                || second.length() - first.length() == 3) {
            // System.out.println("First String bit shorter in length to Second String");
            return calculatedDistance(first, second);
        }

        if (first.length() == second.length()) {
            // System.out.println("Both String Are Equals");
            return calculatedDistance(first, second);
        }

        if (first.length() == second.length() / 2) {
            // System.out.println("First String is Half of Second String");
            int result = calculatedDistance(first, second.substring(0, second.length() / 2));

            return result;
        }

        if (first.length() == second.length() / 2) {

            int result2 = calculatedDistance(first, second.substring(second.length() / 2, second.length()));

            return result2;
        }

        if (second.length() == first.length() / 2) {
            // System.out.println("Second String is Half of First String");
            int result = calculatedDistance(second, first.substring(0, first.length() / 2));

            return result;
        }

        if (second.length() == first.length() / 2) {

            int result2 = calculatedDistance(second, first.substring(first.length() / 2, first.length()));

            return result2;
        }

        return 0;
    }

    private int calculatedDistance(String first, String second) {
        int messasureDistance = 0;
        char() firstStrCharArray = first.toCharArray();
        char() secondStrCharArray = second.toCharArray();

        // System.out.println(first + " " + second);

        for (int i = 0; i < firstStrCharArray.length; i++) {

            if (firstStrCharArray(i) != secondStrCharArray(i)) {
                messasureDistance++;
            }

        }

        return messasureDistance;
    }

    private String flipFyString(String str) {

        StringBuffer word = new StringBuffer();
        char() charArray = str.toCharArray();

        for (int i = 0; i <= charArray.length; i++) {
            int j = i + 1;
            if (j <= charArray.length - 1 && charArray(i) != charArray(j)) {
                word.append(charArray(i));
            }
        }

        word.append(charArray(charArray.length - 1));
        return word.toString();
    }

}

HammingImplTest.java

import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;

public class HammingImplTest {

    public static void main(String... strings) {

        List<Boolean> resultlist = new ArrayList<>();

        resultlist.add(testHammingAlgo("India is near to pakistan.", "India is neighbour of pakistan."));
        resultlist.add(testHammingAlgo("India is near to pakistan.", "India is neighbour of nepal."));
        resultlist.add(testHammingAlgo("Simmant Yadav", "Seemant Yadav"));
        resultlist.add(testHammingAlgo("I love code in java", "I love coding in java"));
        /*
         * Specific result count
         * 
         * System.out.println(resultlist.stream().filter(data->!data).count());
         * if(resultlist.stream().filter(data->data).count()==3 &&
         * resultlist.stream().filter(data->!data).count()==1) {
         * System.out.println("Test cases satisfied"); }
         */
        resultlist.forEach(data -> System.out.println(data));

    }

    private static boolean testHammingAlgo(String first, String second) {

        boolean result = false;

        HammingDistanceImpl hamingImpl = new HammingDistanceImpl();

        String() sentenceFirst = first.split(" ");
        String() sentenceTwo = second.split(" ");

        ArrayList<Integer> distance = new ArrayList<>();

        for (int i = 0; i <= sentenceFirst.length - 1; i++) {
            distance.add(hamingImpl.distance(sentenceFirst(i), sentenceTwo(i)));
        }

        OptionalInt maxD = distance.stream().mapToInt(v -> v).max();

        if (maxD.getAsInt() <= 3) {
            result = true;
        }
        return result;
    }

}

Test Results :

true
false
true
true

angular 2+ – Is this a valid implementation of the state pattern?

The Article model supports a bunch of transitions. Only two are shown below: star()/unstar().

interface ArticleState {
  star(): ArticleState;
  unstar(): ArticleState;
  // other transitions ...
}

export class InitialArticleState implements ArticleState {
  star   = () => new StarredArticleState();
  unstar = () => this;
  // other transitions ...
}

export class StarredArticleState implements ArticleState {
  star   = () => this;
  unstar = () => new InitialArticleState();
  // other transitions ...
}

export class Article {  
  state: ArticleState = new InitialArticleState();  
  star() { this.state = this.state.star(); }
  unstar() { this.state = this.state.unstar(); }
}

The Article state operations are wrapped by the service which updates the feed:

@Injectable()
export class ArticlesFeedService {

  private feed$: Subject<Article()> = new BehaviorSubject<Article()>(());
  private store: Article() = ();

  private publishChanges() {
    this.feed$.next(this.store);
  }

  starArticle(article: Article): void {
    article.star();
    // update the store then publish changes...
  }
}

There’s a component subscribed to get starred articles:

getStarredArticles(): Observable<Article()> {
  return this.getArticles().pipe(
    map((articles: Article()) => 
      articles.filter((article: Article) => 
        article.state instanceof StarredArticleState
      )
    )
  );
}

My motivation behind using the state pattern is that it helps reduce branching (decision making) in the feed service methods (e.g. starred article can’t be blacklisted, only unstarred / initial).

  1. Is it okay to use state as public property of the Article?
  2. How can I improve the design?
  3. Are there any TypeScript features that could be used instead?