{"@attributes":{"version":"2.0"},"channel":{"title":"Algorithms on Personal Site of Andrei N. Ciobanu","link":"https:\/\/andreinc.net\/tags\/algorithms\/","description":"Recent content in Algorithms on Personal Site of Andrei N. Ciobanu","generator":"Hugo","language":"en-us","lastBuildDate":"Tue, 15 Mar 2022 00:00:00 +0000","item":[{"title":"Hash, displace, and compress: Perfect hashing with Java","link":"https:\/\/andreinc.net\/2022\/03\/15\/hash-displace-and-compress-perfect-hashing-with-java\/","pubDate":"Tue, 15 Mar 2022 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2022\/03\/15\/hash-displace-and-compress-perfect-hashing-with-java\/","description":"<blockquote>\n<p>This article explains a straightforward approach for generating <em>Perfect Hash Functions<\/em>, and using them in tandem with a <code>Map&lt;K,V&gt;<\/code> implementation called <code>ReadOnlyMap&lt;K,V&gt;<\/code>. It assumes the reader is already familiar with concepts like hash functions and hash tables. If you want to refresh your knowledge on these two topics, I recommend you read some of my previous articles: <a href=\"..\/..\/2021\/10\/02\/implementing-hash-tables-in-c-part-1\">Implementing Hash Tables in C<\/a> and <a href=\"..\/..\/2021\/11\/08\/a-tale-of-java-hash-tables\">A tale of Java Hash Tables<\/a>.<\/p>\n<\/blockquote>\n<p>A <em>Perfect Hash Function<\/em> (PHF), $H$, is a <em>hash function<\/em> that maps distinct elements from a set $S$ to a range of integer values $[0, 1, &hellip;.]$ so that there are absolutely no collisions. In mathematical terms, $H$ is injective. This means that for any $x_{1}, x_{2} \\in S$, if $H(x_{1}) = H(x_{2})$ we can confidently say $x_{1} = x_{2}$. The contrapositive argument is also true: if $x_{1} \\neq x_{2}$, then $H(x_{1}) \\neq H(x_{2})$.<\/p>"},{"title":"On implementing Bloom Filters in C","link":"https:\/\/andreinc.net\/2022\/03\/15\/on-implementing-bloom-filters-in-c\/","pubDate":"Tue, 15 Mar 2022 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2022\/03\/15\/on-implementing-bloom-filters-in-c\/","description":"<p>This article assumes the reader is already familiar with the C programming language and some basic concepts concerning hash functions. The target audience (as is often the case on my blog) is undergrad CS students or seasoned developers who, just like me, haven&rsquo;t learned about Bloom Filters during their University years.<\/p>\n<h1 id=\"introduction\">Introduction<\/h1>\n<blockquote>\n<p>If you just want to jump directly into the code, check out <a href=\"https:\/\/github.com\/nomemory\/bloomfilters-c\">this repository<\/a>.<\/p>\n<\/blockquote>\n<p>As <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bloom_filter\">Wikipedia states<\/a>, <em>Bloom Filters<\/em> are space-efficient, probabilistic data structures, conceived by Burton Howard Bloom in 1970, used to test whether an element is a member of a set or not. What I find peculiar is that the real Mr. Burton Howard Bloom doesn&rsquo;t have a wiki page, while the imaginary <a href=\"https:\/\/en.wikipedia.org\/wiki\/Leopold_Bloom\">Mr. Leopold Bloom<\/a> has one.<\/p>"},{"title":"A tale of Java Hash Tables","link":"https:\/\/andreinc.net\/2021\/11\/08\/a-tale-of-java-hash-tables\/","pubDate":"Mon, 08 Nov 2021 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2021\/11\/08\/a-tale-of-java-hash-tables\/","description":"<p>The intended audience for this article is undergrad students who already have a good grasp of Java, or seasoned Java developers who would like to explore an in-depth analysis of various <em>hash table<\/em> implementations that use <em>Open Addressing<\/em>.<\/p>\n<p>The reader should be familiar with Java generics, collections, basic data structures, hash functions, and bitwise operations.<\/p>\n<nav class=\"toc\">\n  <h2>Contents<\/h2>\n  <nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#introduction\">Introduction<\/a>\n      <ul>\n        <li><a href=\"#standard-implementations-by-language\">Standard Implementations by Language<\/a><\/li>\n        <li><a href=\"#objectives-and-scope\">Objectives and Scope<\/a>\n          <ul>\n            <li><a href=\"#the-contenders\">The Contenders<\/a><\/li>\n            <li><a href=\"#getting-started\">Getting Started<\/a><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#separate-chaining-or-how-hashmapkv-works-internally\">Separate Chaining, or how <code>HashMap&lt;K,V&gt;<\/code> works internally<\/a>\n      <ul>\n        <li>\n          <ul>\n            <li><a href=\"#the-table-and-the-nodes\">The Table and the Nodes<\/a><\/li>\n            <li><a href=\"#mapping-cities-an-example\">Mapping Cities: An Example<\/a><\/li>\n            <li><a href=\"#the-magic-of-bucket-indexing\">The &ldquo;Magic&rdquo; of Bucket Indexing<\/a><\/li>\n            <li><a href=\"#retrieval-following-the-trail\">Retrieval: Following the Trail<\/a><\/li>\n            <li><a href=\"#performance-optimizations\">Performance Optimizations<\/a><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#open-addressing\">Open Addressing<\/a>\n      <ul>\n        <li><a href=\"#tombstones\">Tombstones<\/a><\/li>\n        <li><a href=\"#lprobmapk-v\"><code>LProbMap&lt;K, V&gt;<\/code><\/a>\n          <ul>\n            <li><a href=\"#the-hash-function\">The Hash Function<\/a><\/li>\n            <li><a href=\"#inserting-an-entry\">Inserting an Entry<\/a><\/li>\n            <li><a href=\"#the-bitwise-modulo-trick\">The Bitwise Modulo Trick<\/a><\/li>\n            <li><a href=\"#retrieving-an-entry\">Retrieving an Entry<\/a><\/li>\n            <li><a href=\"#deleting-an-entry\">Deleting an Entry<\/a><\/li>\n            <li><a href=\"#resizing-and-rehashing\">Resizing and Rehashing<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#lprobsoamapkv\"><code>LProbSOAMap&lt;K,V&gt;<\/code><\/a><\/li>\n        <li><a href=\"#perturbmapk-v\"><code>PerturbMap&lt;K, V&gt;<\/code><\/a>\n          <ul>\n            <li><a href=\"#scrambling-the-search-path\">Scrambling the Search Path<\/a><\/li>\n            <li><a href=\"#a-practical-example\">A Practical Example<\/a><\/li>\n            <li><a href=\"#implementation-of-getobject-key\">Implementation of <code>get(Object key)<\/code><\/a><\/li>\n            <li><a href=\"#performance-trade-offs\">Performance Trade-offs<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#lprobbinsmapkv\"><code>LProbBinsMap&lt;K,V&gt;<\/code><\/a>\n          <ul>\n            <li><a href=\"#separating-metadata-from-data\">Separating Metadata from Data<\/a><\/li>\n            <li><a href=\"#implementation-of-getobject-key-1\">Implementation of <code>get(Object key)<\/code><\/a><\/li>\n            <li><a href=\"#advantages-of-the-bins-approach\">Advantages of the &ldquo;Bins&rdquo; Approach<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#lprobradarmapk-v\"><code>LProbRadarMap&lt;K, V&gt;<\/code><\/a>\n          <ul>\n            <li><a href=\"#how-the-radar-works\">How the Radar Works<\/a><\/li>\n            <li><a href=\"#insertion-logic\">Insertion Logic<\/a><\/li>\n            <li><a href=\"#retrieval-logic\">Retrieval Logic<\/a><\/li>\n            <li><a href=\"#analysis-and-risks\">Analysis and Risks<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#robinhoodmapk-v\"><code>RobinHoodMap&lt;K, V&gt;<\/code><\/a>\n          <ul>\n            <li><a href=\"#the-rich-and-the-poor\">The &ldquo;Rich&rdquo; and the &ldquo;Poor&rdquo;<\/a><\/li>\n            <li><a href=\"#implementation-of-put\">Implementation of <code>put<\/code><\/a><\/li>\n            <li><a href=\"#why-use-robin-hood-hashing\">Why use Robin Hood Hashing?<\/a><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#benchmarks\">Benchmarks<\/a>\n      <ul>\n        <li>\n          <ul>\n            <li><a href=\"#benchmark-configuration\">Benchmark Configuration<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#data-generation-strategies\">Data Generation Strategies<\/a>\n          <ul>\n            <li><a href=\"#1-randomstringsreads\">1. <code>RandomStringsReads<\/code><\/a><\/li>\n            <li><a href=\"#2-sequencedstringsreads\">2. <code>SequencedStringsReads<\/code><\/a><\/li>\n            <li><a href=\"#3-alphanumericcodesreads\">3. <code>AlphaNumericCodesReads<\/code><\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#the-results\">The Results<\/a>\n          <ul>\n            <li><a href=\"#including-lprobsoamapkv\">Including <code>LProbSOAMap&lt;K,V&gt;<\/code><\/a><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#wrapping-up\">Wrapping Up<\/a>\n      <ul>\n        <li>\n          <ul>\n            <li><a href=\"#discussion\">Discussion<\/a><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n  <\/ul>\n<\/nav>\n<\/nav>\n<h1 id=\"introduction\">Introduction<\/h1>\n<p>In Java, the primary hash table implementation, <code>HashMap&lt;K,V&gt;<\/code>, employs the classical <strong>Separate Chaining<\/strong> technique with its critical optimizations (such as treeifying bins) to reduce read times during high-collision scenarios.<\/p>"},{"title":"Implementing Hash Tables in C","link":"https:\/\/andreinc.net\/2021\/10\/02\/implementing-hash-tables-in-c\/","pubDate":"Sat, 02 Oct 2021 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2021\/10\/02\/implementing-hash-tables-in-c\/","description":"<h1 id=\"table-of-contents\">Table of contents<\/h1>\n<nav class=\"toc\">\n  <h2>Contents<\/h2>\n  <nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#table-of-contents\">Table of contents<\/a><\/li>\n    <li><a href=\"#code\">Code<\/a><\/li>\n    <li><a href=\"#introduction\">Introduction<\/a>\n      <ul>\n        <li><a href=\"#c-unordered_map\">C++ (<code>unordered_map<\/code>)<\/a><\/li>\n        <li><a href=\"#java-hashmap\">Java (<code>HashMap<\/code>)<\/a><\/li>\n        <li><a href=\"#python-dict\">Python (<code>dict<\/code>)<\/a><\/li>\n        <li><a href=\"#performance-why-size-doesnt-matter\">Performance: Why Size Doesn&rsquo;t Matter<\/a><\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#hash-functions\">Hash Functions<\/a>\n      <ul>\n        <li>\n          <ul>\n            <li><a href=\"#hash-collisions\">Hash Collisions<\/a><\/li>\n            <li><a href=\"#what-makes-a-good-hash-function\">What makes a &ldquo;Good&rdquo; Hash Function?<\/a><\/li>\n            <li><a href=\"#families-of-hashing-functions\">Families of Hashing Functions<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#division-hashing\">Division hashing<\/a><\/li>\n        <li><a href=\"#multiplicative-hashing\">Multiplicative hashing<\/a><\/li>\n        <li><a href=\"#hashing-strings\">Hashing strings<\/a>\n          <ul>\n            <li><a href=\"#the-rolling-hash-template\">The Rolling Hash Template<\/a><\/li>\n            <li><a href=\"#djb2\">djb2<\/a><\/li>\n            <li><a href=\"#sdbm\">sdbm<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#reducing-collisions-finalizers\">Reducing collisions: Finalizers<\/a><\/li>\n        <li><a href=\"#more-exploration\">More exploration<\/a><\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#implementing-hash-tables\">Implementing Hash Tables<\/a>\n      <ul>\n        <li>\n          <ul>\n            <li><a href=\"#1-separate-chaining\">1. Separate Chaining<\/a><\/li>\n            <li><a href=\"#2-open-addressing\">2. Open Addressing<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#separate-chaining-using-linked-lists\">Separate Chaining (using Linked Lists)<\/a>\n          <ul>\n            <li><a href=\"#the-multi-step-hashing-process\">The Multi-Step Hashing Process<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#a-generic-implementation\">A Generic Implementation<\/a>\n          <ul>\n            <li><a href=\"#the-model\">The Model<\/a>\n              <ul>\n                <li><a href=\"#data-specific-operations-the-contract\">Data-Specific Operations: The Contract<\/a><\/li>\n                <li><a href=\"#implementation-example-strings\">Implementation Example: Strings<\/a><\/li>\n              <\/ul>\n            <\/li>\n            <li><a href=\"#the-interface\">The Interface<\/a>\n              <ul>\n                <li><a href=\"#dynamic-resizing-and-rehashing\">Dynamic Resizing and Rehashing<\/a><\/li>\n                <li><a href=\"#internal-node-retrieval\">Internal Node Retrieval<\/a><\/li>\n                <li><a href=\"#creatingdestroying-a-hash-table\">Creating\/Destroying a <strong>hash table<\/strong><\/a><\/li>\n                <li><a href=\"#retrieving-a-value-from-the-hash-table\">Retrieving a value from the <strong>hash table<\/strong><\/a><\/li>\n                <li><a href=\"#adding-an-entry-to-the-hash-table\">Adding an entry to the <strong>hash table<\/strong><\/a><\/li>\n                <li><a href=\"#scaling-the-ch_hash_grow-method\">Scaling: The <code>ch_hash_grow<\/code> Method<\/a><\/li>\n                <li><a href=\"#printing-the-contents-of-a-hash-table\">Printing the contents of a <strong>hash table<\/strong><\/a><\/li>\n                <li><a href=\"#calculating-collisions\">Calculating collisions<\/a><\/li>\n              <\/ul>\n            <\/li>\n            <li><a href=\"#using-the-hash-table\">Using the <strong>hash table<\/strong><\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#separate-chaining-dynamically-growing-array-buckets\">Separate Chaining (Dynamically growing array buckets)<\/a>\n          <ul>\n            <li><a href=\"#writing-a-vector-like-structure-for-our-buckets-ch_vect\">Writing a <code>vector<\/code>-like structure for our buckets: <code>ch_vect<\/code><\/a>\n              <ul>\n                <li><a href=\"#the-model-1\">The model<\/a><\/li>\n                <li><a href=\"#the-interface-1\">The interface<\/a><\/li>\n                <li><a href=\"#memory-management\">Memory Management<\/a><\/li>\n                <li><a href=\"#appending-and-growing\">Appending and Growing<\/a><\/li>\n              <\/ul>\n            <\/li>\n            <li><a href=\"#updating-ch_hash-to-ch_hashv\">Updating <code>ch_hash<\/code> to <code>ch_hashv<\/code><\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#separate-chaining-red-black-trees-optimization\">Separate Chaining (Red-Black Trees Optimization)<\/a><\/li>\n        <li><a href=\"#open-addressing\">Open Addressing<\/a>\n          <ul>\n            <li><a href=\"#linear-probing\">Linear Probing<\/a><\/li>\n            <li><a href=\"#the-problem-of-clustering\">The Problem of Clustering<\/a><\/li>\n            <li><a href=\"#beyond-linear-probing\">Beyond Linear Probing<\/a><\/li>\n            <li><a href=\"#why-use-open-addressing\">Why use Open Addressing?<\/a><\/li>\n            <li><a href=\"#tombstones\">Tombstones<\/a>\n              <ul>\n                <li><a href=\"#the-sentinel-solution\">The Sentinel Solution<\/a><\/li>\n                <li><a href=\"#managing-tombstone-density\">Managing Tombstone Density<\/a><\/li>\n              <\/ul>\n            <\/li>\n            <li><a href=\"#a-generic-implementation-1\">A Generic Implementation<\/a>\n              <ul>\n                <li><a href=\"#the-model-2\">The Model<\/a><\/li>\n                <li><a href=\"#probing-strategies\">Probing Strategies<\/a><\/li>\n                <li><a href=\"#configuration-constants\">Configuration Constants<\/a><\/li>\n                <li><a href=\"#the-interface-2\">The interface<\/a><\/li>\n                <li><a href=\"#creatingdestroying-a-hash-table-1\">Creating\/Destroying a <strong>hash table<\/strong><\/a><\/li>\n                <li><a href=\"#modeling-tombstones\">Modeling Tombstones<\/a><\/li>\n                <li><a href=\"#putting-it-all-together\">Putting it all together<\/a><\/li>\n              <\/ul>\n            <\/li>\n            <li><a href=\"#references\">References<\/a><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n  <\/ul>\n<\/nav>\n<\/nav>\n<h1 id=\"code\">Code<\/h1>\n<p>If you want to skip the theory and dive straight into the implementation, you can find the source code here:<\/p>"},{"title":"Implementing a generic Priority Queue in C","link":"https:\/\/andreinc.net\/2011\/06\/01\/implementing-a-generic-priority-queue-in-c\/","pubDate":"Wed, 01 Jun 2011 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2011\/06\/01\/implementing-a-generic-priority-queue-in-c\/","description":"<nav class=\"toc\">\n  <h2>Contents<\/h2>\n  <nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#introduction\">Introduction<\/a><\/li>\n    <li><a href=\"#the-binary-heap\">The Binary Heap<\/a><\/li>\n    <li><a href=\"#why-use-a-heap\">Why use a Heap?<\/a><\/li>\n    <li><a href=\"#the-code\">The code<\/a><\/li>\n    <li><a href=\"#defining-the-data\">Defining the data<\/a><\/li>\n    <li><a href=\"#implementation\">Implementation<\/a><\/li>\n    <li><a href=\"#testing\">Testing<\/a><\/li>\n  <\/ul>\n<\/nav>\n<\/nav>\n<h1 id=\"introduction\">Introduction<\/h1>\n<p>In Computer Science, a <strong>Priority Queue<\/strong> is an abstract data type similar to a standard Queue, but with a critical distinction: <em>every element has an associated priority<\/em>. In this structure, elements with higher priorities are dequeued and served before those with lower priorities, regardless of their insertion order.<\/p>\n<h1 id=\"the-binary-heap\">The Binary Heap<\/h1>\n<p>Our implementation utilizes a <strong>Binary Heap<\/strong>. While the name sounds complex, it is simply a binary tree that satisfies two specific properties:<\/p>"},{"title":"Evaluate RPN expressions using Haskell, Scala, and python","link":"https:\/\/andreinc.net\/2011\/01\/03\/evaluate-rpn-expressions-using-haskell-scala-and-python\/","pubDate":"Mon, 03 Jan 2011 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2011\/01\/03\/evaluate-rpn-expressions-using-haskell-scala-and-python\/","description":"<nav class=\"toc\">\n  <h2>Contents<\/h2>\n  <nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#introduction\">Introduction<\/a><\/li>\n    <li><a href=\"#the-algorithm\">The Algorithm<\/a><\/li>\n    <li><a href=\"#implementations\">Implementations<\/a>\n      <ul>\n        <li><a href=\"#scala\">Scala<\/a><\/li>\n        <li><a href=\"#python\">Python<\/a><\/li>\n        <li><a href=\"#haskell\">Haskell<\/a><\/li>\n      <\/ul>\n    <\/li>\n  <\/ul>\n<\/nav>\n<\/nav>\n<h1 id=\"introduction\">Introduction<\/h1>\n<p>One of the earlier challenges from Programming Praxis was the RPN Calculator. The goal is to create a module that evaluates Reverse Polish Notation (RPN) expressions. RPN is a mathematical notation where operators follow their operands, eliminating the need for parentheses. This makes expressions easier for computers to parse and execute.<\/p>\n<p>The specific requirement for this challenge was:<\/p>\n<blockquote>\n<p>Implement an RPN calculator that takes an expression like <code>19 2.14 + 4.5 2 4.3 \/ - *<\/code> (which represents <code>(19 + 2.14) * (4.5 - 2 \/ 4.3)<\/code>) and returns 85.2974. The program should read from standard input, print the result when a newline is encountered, and maintain the state of the operand stack between expressions.<\/p>"},{"title":"Converting infix to RPN (shunting-yard algorithm)","link":"https:\/\/andreinc.net\/2010\/10\/05\/converting-infix-to-rpn-shunting-yard-algorithm\/","pubDate":"Tue, 05 Oct 2010 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2010\/10\/05\/converting-infix-to-rpn-shunting-yard-algorithm\/","description":"<hr>\n<h1 id=\"introduction\">Introduction<\/h1>\n<p>If you&rsquo;ve ever tried to write your own calculator, you&rsquo;ve probably needed a way to convert mathematical expressions written in the usual <em>infix<\/em> notation into <a href=\"http:\/\/en.wikipedia.org\/wiki\/Postfix_notation\">Reverse Polish Notation (RPN)<\/a>. This post walks through that conversion using the classic <strong>shunting-yard algorithm<\/strong>, and shows a (hopefully) compact Java implementation.<\/p>\n<p>Before we jump into the algorithm, let&rsquo;s make sure we&rsquo;re on the same page about the terminology: <em>infix notation<\/em> and <em>rpn<\/em>.<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Infix_notation\">Infix notation<\/a>: this is the \u201cnormal\u201d notation you use every day: operators are written <em>between<\/em> operands (e.g. <code>A + B<\/code>, <code>3 * (4 + 5)<\/code>). It&rsquo;s natural for humans, but surprisingly annoying to parse for computers because you need to consider parentheses and operator precedence.<\/p>"},{"title":"Generic data structures in C","link":"https:\/\/andreinc.net\/2010\/09\/30\/generic-data-structures-in-c\/","pubDate":"Thu, 30 Sep 2010 00:00:00 +0000","guid":"https:\/\/andreinc.net\/2010\/09\/30\/generic-data-structures-in-c\/","description":"<div class=\"mp\">\nUpdate (2026): This was the first 'real article' I ever wrote. It actually dates back to before 2010, though I didn't publish it until later. I\u2019ve made a few tweaks here and there, but the original 'old code' remains. Please judge it kindly!\n<\/div>\n<nav class=\"toc\">\n  <h2>Contents<\/h2>\n  <nav id=\"TableOfContents\">\n  <ul>\n    <li><a href=\"#introduction\">Introduction<\/a><\/li>\n    <li><a href=\"#generic-data-structures-using-the-c-preprocessor\">Generic Data Structures Using the C Preprocessor<\/a>\n      <ul>\n        <li><a href=\"#what-is-a-macro\">What is a Macro?<\/a>\n          <ul>\n            <li><a href=\"#example-object-like-macros\">Example: Object-like Macros<\/a><\/li>\n            <li><a href=\"#example-function-like-macros\">Example: Function-like Macros<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#token-concatenation-\">Token Concatenation (<code>##<\/code>)<\/a><\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#writing-a-generic-stack-using-macros\">Writing a Generic Stack Using Macros<\/a>\n      <ul>\n        <li><a href=\"#the-declaration-macro\">The Declaration Macro<\/a><\/li>\n        <li><a href=\"#the-definition-macro\">The Definition Macro<\/a>\n          <ul>\n            <li><a href=\"#visualizing-the-expansion\">Visualizing the Expansion<\/a><\/li>\n          <\/ul>\n        <\/li>\n        <li><a href=\"#putting-it-all-together\">Putting It All Together<\/a><\/li>\n        <li><a href=\"#important-constraint-type-naming\">Important Constraint: Type Naming<\/a><\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#writing-a-generic-stack-using-void\">Writing a Generic Stack Using <code>void*<\/code><\/a>\n      <ul>\n        <li><a href=\"#defining-the-typeless-struct\">Defining the Typeless Struct<\/a><\/li>\n        <li><a href=\"#implementation-push-and-pop\">Implementation: Push and Pop<\/a><\/li>\n        <li><a href=\"#using-the-generic-stack\">Using the Generic Stack<\/a><\/li>\n      <\/ul>\n    <\/li>\n    <li><a href=\"#key-differences-from-the-macro-approach\">Key Differences from the Macro Approach<\/a><\/li>\n  <\/ul>\n<\/nav>\n<\/nav>\n<hr\/>\n<h1 id=\"introduction\">Introduction<\/h1>\n<p>This tutorial assumes basic familiarity with <strong>C macros<\/strong>, <strong>pointers<\/strong>, and simple <strong>data structures<\/strong>. While the C language does not provide built-in generics or templates (like C++, C#, or Java), we can emulate them using a few specific techniques. In this post, we explore two primary approaches:<\/p>"}]}}