Rust – Sometimes you don’t need to use HashMap or HashSet

Rust – Sometimes you don’t need to use HashMap or HashSet

Seven playing cardsI wrote a Poker Hand evaluator. It read in a file of a 1,000 randomly generated sets of seven cards like this AC 8H JC …and figured out what was the best hand. It’s in Rust and on GitHub. That takes you to a projects folder and the whole rust project complete with two test sets of cards are included. Look for the file Rust_pokerhand.zip.

Now in the Hand evaluation I used both a HashMap and HashSet.  Here for example is the code to count how many of each Rank there are.

    let mut rank_counts = HashMap::new();
    for card in cards.iter() {
        *rank_counts.entry(card.rank).or_insert(0) += 1;
    }

The program reads in the cards into a Vec<Vec<Card>> and then calculate the average time to evaluate a hand.

Now there’s nothing wrong with the program. One of the card sets is called test_card_hands.txt and has an instance of each hand type along with a comment.

5H AS 2D 6C 3S KD 7C - High card
7H 3D 8S 7C 4H 9H JC - One Pair
AD 7H 8C AC 5S 8D KS - Two Pairs

You can run that with this command:

cargo run --release --features "show_cards" test_card_hands.txt

Or if you run the other test set with this, it doesn’t show the cards but does calculate the average time.

cargo run --release 1000_card_hands.txt

If I run this on Windows it evaluates a hand in 780 ns on average and on Ubuntu 24.04 on the same PC, (running under Hyper-V), it’s faster typically 550 ns.

So, I was looking for ways to speed it up and thought.  The HasmMap and HashSets are running on small numbers of Cards. 13 for Ranks and 4 for suits so what if I use them as arrays. Would it be faster?

It was, roughly four times faster.

As an example, the code above I replaced with this:

    let mut rank_counts = [0,0,0,0,0,0,0,0,0,0,0,0,0];
    for card in cards.iter() {
       rank_counts[&card.rank.value()-2] += 1;
    }

That needed a value() method that mapped all the Ranks starting at 2, onto 2-14.

I also redid the code for working out a straight.

The original is this, plus a bit more to check for A2345:

    let mut values: HashSet<u8> = cards.iter().map(|card| card.rank.value()).collect();
    let mut unique_values: Vec<u8> = values.drain().collect();
    unique_values.sort_unstable();

    let mut straight_length = 1;
    for i in 1..unique_values.len() {
        if unique_values[i] == unique_values[i - 1] + 1 {
            straight_length += 1;
            if straight_length == 5 {
                is_straight = true;
                break;
            }
        } else {
            straight_length = 1;
        }
    }
The improved version uses this constant

static STRAIGHTS: &'static [i32]=&[7936, 3968, 1984, 992, 496, 248, 124, 62, 31, 7681];

Plus a binvalue function which maps the card Ranks onto 1,2,4,8 etc. Just or the Cards binValues together and ‘ands’ (&) it with the individual STRAIGHTS values.

      let mut bivalue=0;
      for card in cards.iter() {
        bivalue |= card.rank.binvalue();
      }   

      for i in 0..STRAIGHTS.len(){
        if &binvalue & STRAIGHTS[i]== STRAIGHTS[i] {
            is_straight= true;
            break;
        }
    }
SDL3 is officially out

SDL3 is officially out

SDL3 on a monitor created by Claude.asi

SDL releases are few and far between so this is significant news. What’s new you ask? Well lots of things- best to read this page.

It’s highly cross-platform, SDL officially supports Windows, macOS, Linux, iOS, and Android, and several other platforms.  It’s written in C, works natively with C++, and has bindings available for several other languages, including C#, Python, and Rust.

One of the projects I’m working on is rewriting the Asteroids game into Rust with SDL3. I’ll publish more here as it proceeds.

A new year, new language

A new year, new language

Rusty computer by Copilot

I’ve switched to learning Rust hence the corny image.

It is an interesting programming language where the Rust compiler can stop you introducing bugs at compile time. It’s very strong on ownership of variables.  However my interest lies in the area of games and to that end you can

        • get  bindings for libraries like SDL
        • generate WASM for browser games.

It’s cross-platform and you can setup VS Code with a couple of extensions on Windows and Linux (Mac as well but I haven’t got oneto test that) though I noticed a curious thing.  The same program, built and run on Ubuntu in a Hyper-V is quicker than running on Windows. Both were optiomized builds – the Hyper-V Ubuntu took 266 ns, the Windows 388 ns.

It’s been a while

It’s been a while

Official Rust logo
Official Rust logo.

I’ve decided that I’m going to learn Rust and as the SDL2 bindings are available for Rust, I’ll be doing a bit of rewriting C into Rust. I’ve always found that working on some code is a good way to learn a programming language.

Debugging C# on Ubuntu and you get

Debugging C# on Ubuntu and you get

vs-debugger-popup

So you’re trying to debug C# code on Linux, maybe Ubuntu. You’ve installed .NET 6 or 7, you’ve built your solution in Visual Studio on your Windows PC, published it then copied the files over to the Linux box. But when you run it, you get an error “Unable to find debugger script at”. It’s time to debug your Linux app from your Windows Visual Studio.

Now there is a Microsoft support issue for this and it’s less than helpful. Read below to see how I fixed it.

Only, as soon as it tries to attach over SSH (with username, password, IP etc), you get this popup on the left. “Computer says no”.

Looking in the VS Output folder you see the text below or something similar. For whatever reason, it failed to create a hidden folder in your home folder. To fix this, all you have to do is create that folder. It’s .vs-debugger under your home folder.

If you’re not familiar with .folders on Linux, note that the . at the front of the name means it’s a hidden folder. A simple ls command will not reveal it. If you use the File Manager, you can click the settings in the top right and tick the Show Hidden Files. Then you can do a cd ~ followed by a mkdir .vs-debugger. And View in files to confirm that it’s there.

Now the only pain with debugging is that the application must be running for Visual Studio to attach to it. I was debugging a utility that is run, does its business and closes. So to debug it, I added this as the first line in main() in program.cs

Console.ReadKey();

So run it, let it sit waiting for a key then attach visual studio’s debugger to your app over SSH, add your breakpoints and hit a key. Simples!

Compiling C with Visual C++ 2022

Compiling C with Visual C++ 2022

monitor displaying programming languageIt’s been a few years since I compiled the code for the first eBook and I needed to create an SDL application on Windows. I copied a project, as it was the fastest way to setup the include and lib paths, and the lib files needed to compile.

Compared to clang/gcc on Linux, setting up visual studio projects on Windows can be a bit of a pita. You’ve got to be careful not to mix up the x64 and x86 files, add everything twice (once for Win 32 and once for x64).

So I copied .sln and .vcxproj files, started compiling and kept getting this error:

Error LNK2019 unresolved external symbol main referenced in function “int __cdecl invoke_main(void)” (?invoke_main@@YAHXZ) maze D:\development\LinuxFormat\maze\MSVCRTD.lib(exe_main.obj) 

I messed around with settings, still no joy. did a bit of searching around and found that some people fixed it by setting the Link System Subsystem but that didn’t fix it. Also one person had forgotten to add SDL2_main.lib to the list of lib files to be linked in.

Then I noticed the project was defaulting to 64-bit. I checked my includes and it was only setup for 32-bit. D’oh. So copying the settings from 32-bit to 64-bit fixed it.

Single key compile with Visual Studio Code

Single key compile with Visual Studio Code

Set Keyboard ShortcutsOn Linux (Ubuntu and Raspberry Pi OS) I use Visual Studio Code,  but compiling or more specifically on the Terminal menu,  Run Build Task defaults to Ctrl-Shift-B. Being lazy and a long time user of Visual Studio with it’s F6 key, I decided to change this.

First get the Keyboard Shortcuts form up. This is done by Ctrl K, Ctrl S. Jusr hold the control key down then press k then s. You should see this form appear.

Then type in Run B to have it filter and just show the Command we want to remap. Double click that first line and a popup will appear. Press the Scroll Lock key or some other key (warning, most seem to be used!)  and press enter and voila, it now compiles when you press the Scroll Lock key. Another possibility is the Pause key which is next to it.

 

Yet another curious bug

Yet another curious bug

MatchThree latest version with a bugI’m aware that C is notorious for unexpected behaviour (UB). Let me describe this bug:

My second game in C + SDL for my newest eBook is Match Three and compiled with Clang-11 on my Raspberry Pi 4B. I’m getting a weird bug that doesn’t occur when I compile it on Windows under MSVC; the only differences between the source code on the PI and Windows are the paths to SDL and the sn._s calls but there aren’t many of those. Also I compiled it with clang-14 on Ubuntu 22.04 LTS on a Hyper-VM and that behaves properly.

 

Each piece is held a struct in a 10 x 10 array. One field is  called size. It’s 0 normally but if it is set to a value, typically  64 then it counts down, one per frame and the piece is drawn rotated by 8 degrees each frame. When it reaches 0 the piece is removed. This is my “down the plughole” way of removing pieces. It takes exactly 64/60 seconds to vanish. I use the size field to draw it when size is non-zero so it shrinks as it rotates It’s a nice effect.

 

The bug though is that when it starts up, several pieces start rotating and keep rotating. The size field stays at 63. There’s no code that explains this behaviour and it only happens on the Raspberry Pi, not on Windows or on Ubuntu. Is it a compiler bug or some weird UB on the PI version. It’s an interesting bug and I’ve only wasted a week of evenings on it so far!

New Linux Timing Code

New Linux Timing Code

silver and white round analog watch

For some time, I had a problem with my old Linux timing code. The units hr_time.h/c had to have the includes arranged in the right order and some flags set for it to compile. Plus it never seemed right having to do this in hr_time.h

#include <linux/time.h>
#define __timespec_defined 1
#define __itimerspec_defined 1
#define __timeval_defined 1

But today I found some better code on this SolarianProgrammer website and I’m always happy to give credit where it’s due. I’ve witched to using C17 which clang on Ubuntu (clang 14) and Raspberry Pi (clang 11) both seem to accept.

So now my hr_time.h looks like this:

hr_time.h

#pragma once
#include <stdlib.h>
#include <time.h>

void startTimer();
void stopTimer();
double diff();

hr_time.c

#include "hr_time.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

struct timespec start;
struct timespec end;

void startTimer() {
	if (timespec_get(&start, TIME_UTC) != TIME_UTC) {
		printf("Error in calling timespec_get\n");
		exit(EXIT_FAILURE);
	}
};

void stopTimer() {
	if (timespec_get(&end, TIME_UTC) != TIME_UTC) {
		printf("Error in calling timespec_get\n");
		exit(EXIT_FAILURE);
	}
};

double diff()
{
	return  (double)(end.tv_sec - start.tv_sec) + 
			((double)(end.tv_nsec - start.tv_nsec) / 1000000000L);
}

I’m only ever using one timer, if you want to modify this to pass in a timer in the startTimer and stopTimer then it’s easy enough.

A free eBook about the Game of Life

A free eBook about the Game of Life

Front cover of Conway's Game of Life eBook.Remember the cellular automation Life? I covered it in previous blog entries John Conway’s Game of Life and also Portable Life in C. Well, Associate Professor Nathaniel Johnston (in the Department of Mathematics and Computer Science at Mount Allison University in New Brunswick, Canada) and Dave Greene wrote a book/eBook about Life. It’s N. Johnston and D. Greene. Conway’s Game of Life: Mathematics and Construction.

The 474 page eBook is free on this page or you can buy the coloured hardback book. On Amazon that’s a modest £59.99! The PDF is 93 MB in size.

Ever since Life was publicized in Scientific American in the 1970s, it’s held a fascination for many including myself. I wrote a program (in Basic) 45 years ago to run it, but it was quite slow. One of the fastest I ever saw was written in 6502 assembly language running on an Acorn Atom. A very comprehensive and fast open source Life is Golly, written in C++ and is scriptable in Python or Lua.

But back to the book. If you are a real life afficionado and know the difference between a Glider and an Oscillator then this book/eBook is definitely for you. It’s a detailed look at Life from an academic point of view.