Posts Tagged iterator

C++ For C# Developers: Part 49 – Ranges and Parallel Algorithms

Tags: , , ,

Generic algorithms have been available in C++ for decades, but the last two versions of the language have really ramped up the functionality. C++17 added support for parallel execution of generic algorithms to easily take advantage of multi-core CPUs. Then C++20 added support for ranges, a composable version of generic algorithms that’s even closer to LINQ in C#. Today we’ll explore both of these!

Read the rest of this article »

No Comments

C++ For C# Developers: Part 48 – Algorithms Library

Tags: , ,

The C++ Standard Library’s algorithms are culmination of a lot of C++ language and library features. They’re like a much more featureful, much faster version of LINQ in C#. This powerful combination makes most “raw” loops unnecessary as they can be replaced by named function calls that are well-tested and often compile to the same machine code as a “raw” loop. Read on to learn about them!

Read the rest of this article »

No Comments

C++ For C# Developers: Part 36 – Coroutines

Tags: , , ,

In today’s final article covering the C++ language, we’ll explore a new C++20 feature: coroutines. These are analogous to both C# iterator functions (i.e. those with yield) and C# async functions. There are a lot of interesting aspects of coroutines, so let’s dive in explore!

Read the rest of this article »

3 Comments

DIY Iterators and Coroutines

Tags: , , ,

Iterators aren’t magic. We’ve seen the IL2CPP output for them and it’s not complex. It turns out we can just as easily implement our own iterators and gain some nice advantages along the way. Read on to learn how!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 8

Tags: , , , , , , ,

NativeArray<T> is great, but very limited in functionality. We can fix this surprisingly easily! Today we revive a two year old series that created the iterator project. Iterators are like a no-GC version of IEnumerable<T> and LINQ which have a lot of power but only support managed arrays (T[]) and List<T>. Today we’ll add support for NativeArray<T> and inherit support for the same functionality. We’ll also spruce up the project with proper unit tests, assembly definitions, and runtime tests to confirm that zero garbage is created. Read on to see how this was done and how to use iterators with NativeArray<T>.

Read the rest of this article »

3 Comments

IL2CPP Output for Iterators, Switch, and Using

Tags: , , ,

Today we’ll look at the C++ code that IL2CPP outputs when we use iterator functions (those that yield), switch statements, and using blocks. What are you really telling the computer to do when you use these C# features? Read on to find out.

(Website Announcement: check out the new tags page to find articles by topic)

Read the rest of this article »

No Comments

A Simpler Finite State Machine (FSM)

Tags: , , ,

In my last article about Finite State Machines (FSM) for Unity, I showed a “pure code” way to create a state machine, states, and transitions between those states. It worked, but I wanted to create a simpler system. I’ll show you it today!

Read the rest of this article »

11 Comments

Catching Exceptions in Coroutines

Tags: , ,

Unity code frequently makes use of the coroutine feature of MonoBehaviour. It can make asynchronous code a lot easier to write, but runs into problems when exceptions are thrown. There’s no avoiding exceptions since they’re built into C# (e.g. NullReferenceException) but we can cope with them, even when they’re in coroutines. Today’s article introduces a helper function or two that you can drop into your projects to help you handle exceptions thrown from your coroutines. Read on to learn how!

Read the rest of this article »

13 Comments

How to Easily Use Callback Functions in Coroutines

Tags: , ,

In asynchronous programming we’re constantly dealing with callback functions. Maybe you have to call some function in a third party library that takes a callback function. Regardless, Unity programmers often want to use coroutines for their asynchronous tasks. Today’s article show you how you can use callback-based code from your coroutines, all while being simple and easy to use. Read on to learn how!

Read the rest of this article »

5 Comments

Iterators vs. Callbacks: Performance and Garbage

Tags: , , , , , , ,

Iterator functions and their ability to yield return values then continue on really come in handy for a variety of situations. Unfortunately, they come with some pretty serious performance and garbage creation drawbacks. So today’s article explores alternatives in various forms of callbacks: delegates, interfaces, and classes. Can they perform better than iterator functions? Can they avoid garbage creation? Read on to find out!

Read the rest of this article »

2 Comments