Posts

Showing posts with the label programming

C/C++ Programming Tips and Tricks

Every once in a while I found some C/C++ tips and tricks. This page is going to be the repository of those tips and tricks. Constexpr Constexpr specifier declares that it is possible to evaluate the expression at compile time: 1. Constexpr function 2. Constexpr constructor 3. Constexpr variable Initialization of constexpr MUST happen at compile time (const variable can defer at runtime). Constexpr implies const and const implies static. If constexpr variable is in the header, every translation unit will get its own copy. Since C++17, inline keyword can be added to variables (and constexpr variables) that means there should only be one single copy in all translation units (this also allows to declare non-const variable in header file). Fast insertion to std::map/std::unordered_map std::unordered_map > m; auto [iter, succeed] = m.emplace( key, nullptr ); if ( succeed ) iter->second = std::make_unique (); Creating C++ function similar to printf() template inline st...

C# Integer to String Builder

As many of you know, StringBuilder.Append(int) method creates a garbage. This is bad for XNA games that do this conversion every frame. In this article, I provide one implementation to convert int to string without creating garbage. I tried to be as efficient as possible; if you find better way to do this, please let me know. public static class StringBuilderExtension { private static char[] charToInt = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; public static void Swap(this StringBuilder sb, int startIndex, int endIndex) { // Swap the integers Debug.Assert(endIndex >= startIndex); int count = (endIndex - startIndex + 1) / 2; for (int i = 0; i

Generating Alternating 0 and 1

Once upon a time, a friend of mine gave some of us a challenge. It's not exactly the same but quite similar ;) Anyway, think of ways you can generate alternating 0 and 1. If you have an Update() loop, the first time you call update it will generate 0 and the next time will be 1, and then 0,1,0,1,.. you get the idea. I found 3 ways to do this (assuming initial value of i is 0). Math + Bit i = (i+1) & 1; // You might think of this i = (i+1) % 2. Simple Math i = 1 - i; Bits Operations i = i ^ 1; Can you come up with more ways?

Simple XML parsing with Python

There are many ways to read/parse XML with Python.  I found at least 2 methods: DOM and SAX. Document Object Model (DOM) is a cross-language API from W3C for accessing or modifying XML; whereas SAX stands for Simple API for XML. Most of the time, we don't need to understand the whole XML vocabularies; and most of the time we want to parse simple stuff like: <root> <person name="somebody"></person> <person name="otherguy"></person> </root> I think the simplest way to go is to use python minidom implementation that looks like this: from xml.dom import minidom # parse the xml theXml = minidom.parse('data.xml') # iterate through the root rootList = theXml.getElementsByTagName('root') for root in rootList: # you can get the element name by: root.localName # iterate through person personList = root.getElementsByTagName('person') for person in personList: # get the att...

C# BitConverter

Missing the flexibility of C/C++ way in interpreting bits? In C/C++, you can reinterpret the bits by simply casting it. For example if you have an int and you want to reinterpret it as float , you can do the following: int i = 1234; float f = *( (float*) &i ); The question is how can we do this in C#? It turns out there's a class in C# to do this. Meet BitConverter class. int i = 1234; float f = BitConverter.ToSingle( BitConverter.GetBytes(i), 0 ); With this class, you can reinterpret bits from primitive data types. Which should be enough. The only problem is that the class is not suitable to be called every frames. Why? Using Reflector.NET, you can easily tell that BitConverter.GetBytes() is actually allocating memory! public static unsafe byte[] GetBytes(int value) { byte[] buffer = new byte[4]; fixed (byte* numRef = buffer) { *((int*) numRef) = value; } return buffer; } One solution is just to keep in mind not to call this every frame....

BASH: Reading Text File

BASH for C programmers: Read a text file word-by-word in BASH: #!/bin/bash for WORD in `cat filename` do echo $WORD done Read a text file line-by-line in BASH: #!/bin/bash while read LINE do echo $LINE done < filename Looping in BASH: i=0 while [ $i -lt 10 ] do echo $i # next i=$(($i+1)) done Update : another way to loop suggested in the comment.