<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Journal</title>
    <description>Ekin's Notes</description>
    <link>http://ekins.space/</link>
    <atom:link href="http://ekins.space/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Thu, 12 Aug 2021 23:56:07 +0000</pubDate>
    <lastBuildDate>Thu, 12 Aug 2021 23:56:07 +0000</lastBuildDate>
    <generator>Jekyll v3.9.0</generator>
    
      <item>
        <title>Linux Processes</title>
        <description>&lt;p&gt;The instance of a program that is being executed by one or many threads is called a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process&lt;/code&gt;. Any command that you give to your machine starts a new process. It is basically the running instance of your program, made up of instructions and sometimes with some form of input such as data from a file or user input.&lt;/p&gt;

&lt;p&gt;While a program is a passive collection of instructions, a process is the actual execution of those instructions. A program may be run on multiple processes.&lt;/p&gt;

&lt;p&gt;Fundamentally, we have two kinds of processes; the ones running in the background and the ones that run interactively. Background processes run automatically without expecting a user input while foreground processes are usually started and controlled through a terminal session.&lt;/p&gt;

&lt;p&gt;We also have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;daemons&lt;/code&gt; - these are background processes that start when you boot up your machine and keep running forever, unless they are stopped.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;properties-of-a-process&quot;&gt;Properties of a process&lt;/h4&gt;

&lt;p&gt;You can see the status of currently running processes by running the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps&lt;/code&gt; command. This command will also show you certain properties of these processes, should you ask about them by passing arguments like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps -o pid,ppid,tty,uid,args&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/process_ps.png&quot; alt=&quot;Running ps command with arguments&quot; style=&quot;margin:0 auto&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PID&lt;/code&gt;: Every process has a unique identifier associated to it. You can reference a specific process using their PID.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PPID&lt;/code&gt;: The PID of the parent process. Parent of a process is the process that was responsible for its creation. In the above example, the process for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ps&lt;/code&gt; command was initiated by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/bin/bash&lt;/code&gt;, as you can see the PPID of it is same as bash PID.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TTY&lt;/code&gt;: The identifier of the terminal session that triggered this process. Almost every process will be attached to a terminal (except daemons).  In the example above you can see that I have a terminal sessions running (pts/1). You can check your current tty by running the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tty&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UID&lt;/code&gt;: The identifier for the user who is the owner of this process, which will define the permissions available for this process. Run the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id -u yourUsername&lt;/code&gt; to check your user id.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ARGS&lt;/code&gt;: The command (followed by its arguments) that’s running in this process.&lt;/p&gt;

&lt;p&gt;There are tons of properties besides the ones I’ve mentioned above, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fname, start, cgname, class, machine, nwchan, c, %mem, pidns, pgid&lt;/code&gt;. Check the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man ps&lt;/code&gt; for a long list of keywords available.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;the-init-process&quot;&gt;The init process&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; is the first program that’s executed when the linux system boots up, so it doesn’t really have a parent process.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;the-kernel-user-space-and-kernel-space&quot;&gt;The Kernel, User Space and Kernel Space&lt;/h3&gt;

&lt;p&gt;The kernel is the program that controls all other programs on the computer, talking both to the hardware and software, managing resources.&lt;/p&gt;

&lt;p&gt;A modern OS will usually isolate virtual memory into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel space&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user space&lt;/code&gt;, to provide memory and hardware protection from malicious behaviour.&lt;/p&gt;

&lt;p&gt;Kernel space is reserved for running a privileged OS kernel, its extensions and most device drivers, while user space is the memory area where normal user processes (anything other than the kernel) can execute.&lt;/p&gt;

&lt;p&gt;Processes that are running in the user space will only have access to this limited part of memory, while kernel can access all of the memory.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;the-shell&quot;&gt;The Shell&lt;/h3&gt;

&lt;p&gt;A shell program is a command-line interface on a computer that allows the user to interact with the file system (a.k.a. the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user space&lt;/code&gt;). 
In most operating systems, you access the shell (eg. bash) through a terminal emulator (eg. Konsole).&lt;/p&gt;

&lt;p&gt;To run programs and start processes, the shell needs to pass it to the kernel. Basically, the shell is your interface to communicate with the kernel.&lt;/p&gt;

&lt;p&gt;When you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l&lt;/code&gt;, The shell reads the command from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getline()&lt;/code&gt; function’s STDIN, parses the command into arguments that will be then passed to the program it is executing.
The shell checks if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; is an alias. If it is, the alias replaces &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; with its value. If it isn’t an alias, the shell checks if this is a built-in command.&lt;/p&gt;

&lt;p&gt;Then the shell will try to find a program file for this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; command. How would it know where to look for it?&lt;/p&gt;

&lt;p&gt;The shell environment holds a bunch of environment specific variables, including a well known one that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; is a list of directories that shell can search in upon receiving a command.
You can see your environmental variables by running the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printenv&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;This is how a sample&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; would look like: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; will be parsed using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; as the delimiter, then all the directories within the path will be tokenized and parsed further with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; as the delimiter.&lt;/p&gt;

&lt;p&gt;Most of the built-in executable programs live within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/bin&lt;/code&gt;, so the shell will find the program for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/bin/ls&lt;/code&gt; location.&lt;/p&gt;

&lt;p&gt;Most programs can take &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;options&lt;/code&gt; and/or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arguments&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; lists the contents found in the current working directory. 
It can take a specific directory as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;argument&lt;/code&gt; and it would then list files under that directory.&lt;/p&gt;

&lt;p&gt;Most &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;options&lt;/code&gt; actually stand for something that is longer to type. Each program defines its options (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-l&lt;/code&gt; for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;) and arguments (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/foo&lt;/code&gt; for listing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /foo&lt;/code&gt;) that it will expose to users.&lt;/p&gt;

&lt;p&gt;Most of the programs differentiate options from arguments by requiring a dash (-) or two dashes (–) before them. For some options you can define a value, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--dir=/home/username/foo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The order of options and arguments might matter, and options come before arguments do in most cases. So if you were to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /foo -l&lt;/code&gt; in a system without GNU C library, you would make &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; treat &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-l&lt;/code&gt; as another directory. 
In GNU &amp;amp; GNU/Linux many programs will automatically treat the options as if they were typed before the arguments, so running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls /foo -l&lt;/code&gt; still works.&lt;/p&gt;

&lt;p&gt;Let’s summarize what’s happening when you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Shell uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getline()&lt;/code&gt; to receive the user given command and it stores this into a buffer.&lt;/li&gt;
  &lt;li&gt;Shell uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strtok()&lt;/code&gt; to tokenize each word found in the command using space as the delimiter.&lt;/li&gt;
  &lt;li&gt;Checks for aliases. For example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ll&lt;/code&gt; is an alias for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l&lt;/code&gt;. When you run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ll&lt;/code&gt;, the shell will replace this with the full command.&lt;/li&gt;
  &lt;li&gt;Checks if the received command is a built-in command. This is sort of like looking a function’s definition first within the current namespace in PHP. If the program is contained within the shell itself, it’ll just run it directly.&lt;/li&gt;
  &lt;li&gt;Search for the command within the directories found in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt;, if it was not a built-in program.&lt;/li&gt;
  &lt;li&gt;The shell will use a system call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork()&lt;/code&gt; to create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;child process&lt;/code&gt; that the program can run on, and when it’s done executing we get back to the shell. It uses the system call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait()&lt;/code&gt; to wait until the program operation is finalized before exiting back to the command prompt.&lt;/li&gt;
  &lt;li&gt;If the found program is executable, the shell will use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;execve()&lt;/code&gt; system call to execute the program in the child process.&lt;/li&gt;
  &lt;li&gt;Once the execution is complete, any returns from the program will be returned, anything the program wants to print out, like the list of directories in the current working directory, will be displayed on the command prompt.&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;exit-codes&quot;&gt;Exit codes&lt;/h4&gt;

&lt;p&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit code&lt;/code&gt; is the code returned to a parent process (caller) by a child process (callee) that has finished executing. The child process exits by calling the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit&lt;/code&gt; syscall when it finishes executing. This syscall passes the exit status code back to the parent, which can retrieve it using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait&lt;/code&gt; syscall.&lt;/p&gt;

&lt;p&gt;On POSIX systems the standard exit code is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; for success and any number from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;255&lt;/code&gt; for anything else. You can find some of the well accepted exit codes listed at the related section of the &lt;a href=&quot;http://tldp.org/LDP/abs/html/exitcodes.html&quot;&gt;Advanced Bash-Scripting Guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If the child process exits and the parent somehow fails to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait&lt;/code&gt;, you get a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zombie process&lt;/code&gt;. These zombie processes don’t really exist anymore, yet they will still show up in the process table with a status &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Z&lt;/code&gt;. The kernel cannot right away get rid of a process when it exits, since this would make it impossible to read its exit code, so it just waits until the parent calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait&lt;/code&gt; and then it gets fully removed.&lt;/p&gt;

&lt;p&gt;If a child process is still running, but it’s parent exits (because it crashed etc), then you get an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;orphan process&lt;/code&gt;. Orphan processes get ‘adopted’ by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init process&lt;/code&gt; (or launchd on Mac OS, basically the very first process executed) and their PPID will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A process can become a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;daemon&lt;/code&gt; by being made an orphan process on purpose (eg. by forking a child and immediately exiting). This is useful when you’d like to detach a long running process from your session. Usually one makes use of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;signals&lt;/code&gt; to terminate daemon processes using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt; syscall. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt; actually just send a message (any message available, see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill -l&lt;/code&gt;), it doesn’t terminate a process by default.&lt;/p&gt;

&lt;p&gt;With the exception of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGKILL&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGSTOP&lt;/code&gt;, processes can &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trap&lt;/code&gt; a signal to ignore it completely or to take some custom action. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-c&lt;/code&gt; doesn’t kill the process, your target process probably traps it to do something before exiting.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; process can ignore even a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGKILL&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SIGSTOP&lt;/code&gt; signal, because the kernel forces a system crash if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; process terminates, so it will ignore any such signal.&lt;/p&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;system-calls-interrupts-and-processor-modes&quot;&gt;System calls, interrupts and processor modes&lt;/h3&gt;

&lt;p&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;system call&lt;/code&gt; lets a program to requests a service from the kernel. They act as an interface between a process and the OS it is running on.
This could be a hardware-related request like accessing the file system, creating, executing or scheduling a new process.
In most systems, system calls can only be made from user space processes.&lt;/p&gt;

&lt;p&gt;The architecture of most modern processors (except some embedded systems) involves a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;security model&lt;/code&gt;.
Ie. the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rings model&lt;/code&gt; specifies multiple privilege levels under which software may be executed.&lt;/p&gt;

&lt;p&gt;Usually all programs run in their own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;address space&lt;/code&gt; so that they aren’t allowed to access or modify other programs, devices or the OS itself.&lt;/p&gt;

&lt;p&gt;However, many applications do need access to these components, so the OS exposes the system calls to provide safe implementations for such operations.
The OS is executing at the highest level of privilege possible and it allows applications to use system calls to request services. These are often initiated via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interrupts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;interrupt&lt;/code&gt; puts the CPU into some elevated privilege level automatically, then passes control to the kernel. The kernel determines whether the calling program shall be granted the requested service.
If the service is granted access, the kernel will execute a set of instructions over which the calling program has no direct control.
It will then return to the privilege level of the calling program, and return the control back to the calling program.&lt;/p&gt;

&lt;p&gt;Usually a library or API sits between normal programs and the OS. On Unix-like systems, this API is generally part of an implementation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libc&lt;/code&gt; (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;glibc&lt;/code&gt;), providing wrapper functions for system calls.&lt;/p&gt;

&lt;p&gt;On Windows NT, this comes in the Native API (ntdll.dll) and it is used by implementations of the regular Windows API and by some system programs on Windows.
These wrapper functions expose a function calling convention (a subroutine call on the assembly level) for using the system call.
They place the arguments to be passed to the syscall in the appropriate registers and set a unique system call number for the kernel to call.&lt;/p&gt;

&lt;p&gt;The call to the library function is usually a subroutine call, it doesn’t cause a switch to kernel mode (if the execution was not already in kernel mode).
The actual system call transfers control to kernel and it’s more implementation and platform dependent than the library call abstracting it.
E.g. in Unix-like systems, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;execve&lt;/code&gt; are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libc&lt;/code&gt; functions that invoke the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exec&lt;/code&gt; system calls.&lt;/p&gt;

&lt;p&gt;The library functions are meant to abstract away the implementation to avoid making the system call directly in the application code.
The low-level interface for the syscall operation can change over time and thus it should not be a part of the ABI.&lt;/p&gt;

&lt;p&gt;Some known system calls (in Unix-like and other POSIX-compliant OSes) are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;write&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;close&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exec&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exit&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Many modern operating systems have hundreds of system calls. Check out &lt;a href=&quot;http://man7.org/linux/man-pages/man2/syscalls.2.html&quot;&gt;syscalls&lt;/a&gt; page for a long list of syscalls available in Linux.&lt;/p&gt;

&lt;p&gt;Tools such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ftrace&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;truss&lt;/code&gt; allow a process to execute from start and report all system calls the process invokes. They can also be attached to a running process and intercept any system calls it is making.
This functionality is usually also implemented with a system call, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt; is implemented with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ptrace&lt;/code&gt; or system calls on files in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;procfs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are different categories of syscalls: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process control&lt;/code&gt; (eg. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fork&lt;/code&gt; to create a process, get/set process attributes, allocate and free memory), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file management&lt;/code&gt; (eg. create/delete/open/close/read/write files), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;device management&lt;/code&gt; (eg. logically attach/detach devices, get/set device attributes), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;information maintenance&lt;/code&gt; (eg. get/set time or date, system data, process, file or device attributes), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;communication&lt;/code&gt; (eg. send/receive messages, transfer status info), &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;protection&lt;/code&gt; (eg. get/set file permissions).&lt;/p&gt;

&lt;h4 id=&quot;processor-modes&quot;&gt;Processor Modes&lt;/h4&gt;

&lt;p&gt;Processor modes (a.k.a CPU modes, CPU states or CPU privilege levels) are operating modes for the CPU of certain architectures that place restrictions on the type and scope of operations that can be performed by processes being run by the CPU.
This allows the OS to run at a higher privilege level than any other software.&lt;/p&gt;

&lt;p&gt;Only the kernel code runs in unrestricted mode while everything else runs in a restricted mode and needs to use system calls via interrupts to ask the kernel to run operations on its behalf. 
However, syscalls might hurt performance or take some time, so it’s not that uncommon for system designers to allow some time-critical software (eg. device drivers) to run with full kernel privileges.&lt;/p&gt;

&lt;p&gt;The unrestricted mode is often called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel mode&lt;/code&gt; (or master mode, supervisor mode, privileged mode, etc.). 
Restricted modes are usually referred to as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user modes&lt;/code&gt; (or slave mode, problem state, etc.).&lt;/p&gt;

&lt;p&gt;In &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kernel mode&lt;/code&gt;, the CPU can perform any operation allowed by its architecture, letting any instruction that may be executed, any I/O operation initiated, any area of memory accessed, and so on. 
In other modes, certain restrictions on CPU operations are enforced by the hardware. Typically, altering the global state of the machine is not allowed and some memory areas cannot be accessed etc.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;must-reads-on-this-topic&quot;&gt;Must-reads on this topic&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://wikipedia.com/wiki/System_call&quot;&gt;System Calls, Wikipedia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Process_(computing)&quot;&gt;Process, Wikipedia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://wikipedia.com/wiki/Context_switch&quot;&gt;Context Switch, Wikipedia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://jvns.ca/blog/2016/10/04/exec-will-eat-your-brain/&quot;&gt;What happens when you start a process on Linux?, Julia Evans&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://tldp.org/LDP/abs/html/index.html&quot;&gt;Advanced Bash-Scripting Guide,Mendel Cooper&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sat, 02 Nov 2019 15:26:54 +0000</pubDate>
        <link>http://ekins.space/2019/11/02/linux-processes/</link>
        <guid isPermaLink="true">http://ekins.space/2019/11/02/linux-processes/</guid>
        
        
      </item>
    
      <item>
        <title>Notes on Analyzing Database Queries</title>
        <description>&lt;p&gt;This is an interesting topic to dive into. While I personally prefer PostgreSQL most of the time, this post will mostly have my notes on MySQL side of things.&lt;/p&gt;

&lt;h4 id=&quot;whats-index-cardinality&quot;&gt;What’s index cardinality?&lt;/h4&gt;

&lt;p&gt;Cardinality literally means number of elements of a given set. For the very interested, the relation of having the same cardinality is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equinumerosity&lt;/code&gt;, and this is an equivalence relation on the class of all sets.&lt;/p&gt;

&lt;h5 id=&quot;what-does-it-have-to-do-with-the-database-or-queries-then&quot;&gt;What does it have to do with the database or queries then?&lt;/h5&gt;

&lt;p&gt;Well, considering a table holds a set of rows and columns, the number of records in the table can be referred as it’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cardinality&lt;/code&gt;. For an index, cardinality is considered to be the number of unique values in the index.&lt;/p&gt;

&lt;p&gt;A unique index would have cardinality equal to the number of rows in the table. But a non-unique index can have a cardinality of anywhere from 1 to the number of rows in the table, depending on how many times each index key appears in the table.&lt;/p&gt;

&lt;p&gt;Indexes with relatively few unique values are considered to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;low cardinality indexes&lt;/code&gt;. If a column can only take a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yes&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;no&lt;/code&gt; would have a cardinality of 2. Columns like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;status&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;color&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;currency&lt;/code&gt; would have a small set of unique values, hence they are good examples to low cardinality indexes.&lt;/p&gt;

&lt;p&gt;The lower the cardinality, the more duplicated elements you would have in a column. Thus, a column with the lowest possible cardinality would have the same value for every row.&lt;/p&gt;

&lt;p&gt;Indexes work best for columns that have a high cardinality relative to the number of rows in the table (that is, columns that have many unique values and few duplicates).&lt;/p&gt;

&lt;p&gt;If a column contains many different age values, an index will differentiate rows readily. An index will not help for a column that is used to record sex and contains only the two values ‘M’ and ‘F’.&lt;/p&gt;

&lt;p&gt;If the values occur about equally, you’ll get about half of the rows whichever value you search for. Under these circumstances, the index might never be used at all, because the query optimizer generally skips an index in favor of a full table scan if it determines that a value occurs in a large percentage of a table’s rows.&lt;/p&gt;

&lt;h5 id=&quot;low-cardinality-isnt-always-bad&quot;&gt;Low cardinality isn’t always bad.&lt;/h5&gt;

&lt;p&gt;Say your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;col1&lt;/code&gt; can only take two distinct values and your FOO:BAR ratio looks like below.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mysql&amp;gt; SELECT col1, COUNT(*) FROM test GROUP BY col1;&lt;/code&gt;&lt;/p&gt;
&lt;table style=&quot;font-size: 14px;margin-bottom: 2%;width: 24%;padding: 0;margin: 0 2% 2% 0;float: left;&quot; class=&quot;mbtablestyle&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;col1&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;COUNT(*)&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;FOO&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;5909&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;BAR&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;10&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p style=&quot;width: 74%;float: left;margin: 2% auto;&quot;&gt;We've got low cardinality (with just 2 possible values) yet if we are just looking for &quot;BAR&quot; rows then they should be easy to find with the index.&lt;/p&gt;

&lt;h5 id=&quot;i-haz-an-index-why-is-it-not-used&quot; style=&quot;clear: both;&quot;&gt;I haz an index, why is it not used!?&lt;/h5&gt;

&lt;p&gt;Just because you have the index there it doesn’t mean that MySQL will decide to use it. MySQL can be smart enough to know about the distribution of the data and can decide that it would be much faster to just go through all of the data rather than the n% that it could return from the index lookup. When the query is restrictive enough however, it  would know it will be better to use the index.&lt;/p&gt;

&lt;h4 id=&quot;notes-on-indexes&quot;&gt;Notes on indexes&lt;/h4&gt;

&lt;h5 id=&quot;index-short-values&quot;&gt;Index short values&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;Use smaller data types when possible. Don’t use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BIGINT&lt;/code&gt; column if a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MEDIUMINT&lt;/code&gt; is large enough to hold the values you need to store. Don’t use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CHAR(100)&lt;/code&gt; if none of your values are longer than 25 characters.&lt;/li&gt;
  &lt;li&gt;Shorter values can be compared more quickly, so index lookups are faster.&lt;/li&gt;
  &lt;li&gt;Smaller values result in smaller indexes that require less disk I/O.&lt;/li&gt;
  &lt;li&gt;With shorter key values, index blocks in the key cache hold more key values. MySQL can hold more keys in memory at once, which improves the likelihood of locating key values without reading additional index blocks from disk.&lt;/li&gt;
  &lt;li&gt;If you’re indexing a string column, specify a prefix length whenever it makes sense.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;leftmost-prefixes&quot;&gt;Leftmost prefixes&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;When you create an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n-column composite index&lt;/code&gt;, you actually create &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n indexes&lt;/code&gt; that MySQL can use. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;composite index&lt;/code&gt; serves as several indexes because any leftmost set of columns in the index can be used to match rows. Such a set is called a “leftmost prefix.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given a table with a composite index on columns named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state, city, and zip&lt;/code&gt;, rows in the index are sorted in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state, city, zip&lt;/code&gt; order, so they’re automatically sorted in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state, city&lt;/code&gt; order and also in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; order. 
This means that MySQL can take advantage of the index even if you specify only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; values in a query, or only &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;city&lt;/code&gt; values. Thus, the index can be used to search the following combinations of columns: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state, city, zip&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state, city&lt;/code&gt; or just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;MySQL cannot use the index for searches that don’t involve a leftmost prefix. So, if you search by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;city&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zip&lt;/code&gt;, the index isn’t used at all.&lt;/p&gt;

&lt;h5 id=&quot;avoid-over-indexing&quot;&gt;Avoid over-indexing&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;Every additional index takes extra disk space and hurts performance of write operations.&lt;/li&gt;
  &lt;li&gt;Indexes must be updated and possibly reorganized when you modify the contents of your tables. The more indexes you have, the longer this will take.&lt;/li&gt;
  &lt;li&gt;MySQL considers indexes when generating an execution plan for retrievals. Creating extra indexes creates more work for the query optimizer.&lt;/li&gt;
  &lt;li&gt;It’s also possible (if unlikely) that MySQL will fail to choose the best index to use when you have too many indexes. Maintaining only the indexes you need helps the query optimizer avoid making such mistakes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re thinking about adding an index to a table that is already indexed, consider whether the index you’re thinking about adding is a leftmost prefix of an existing multiple-column index. 
If so, don’t bother adding the index because, in effect, you already have it. (Following the example above, if you already have an index on state, city, and zip, there is no point in adding an index on state.)&lt;/p&gt;

&lt;p&gt;Oh also, Use the slow-query log to identify queries that are &lt;em&gt;taking too long&lt;/em&gt;.&lt;/p&gt;

&lt;h4 id=&quot;understand-how-the-query-optimizer-works&quot;&gt;Understand how the query optimizer works&lt;/h4&gt;

&lt;p&gt;The query optimizer has different goals, but it will primarily aim to use the most restrictive index in order to eliminate as many rows as possible, as soon as possible. 
The faster it can eliminate rows from consideration, the more quickly the rows that do match your criteria can be found.&lt;/p&gt;

&lt;h5 id=&quot;how-do-i-help-the-optimizer&quot;&gt;How do I help the optimizer?&lt;/h5&gt;

&lt;ul&gt;
  &lt;li&gt;Try to compare columns that have the same data type.&lt;/li&gt;
  &lt;li&gt;Try to make indexed columns stand alone in comparison expressions.
    &lt;ul&gt;
      &lt;li&gt;If you use a column in a function call or as part of a more complex term in an arithmetic expression, MySQL can’t use the index because it must compute the value of the expression for every row. Sometimes this is unavoidable, but often you can rewrite a query to get the indexed column to appear by itself.&lt;/li&gt;
      &lt;li&gt;Say you have an indexed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;created_date&lt;/code&gt; column and you do a query like: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT * FROM test WHERE YEAR(created_date) &amp;lt; 1990;&lt;/code&gt;
        &lt;ul&gt;
          &lt;li&gt;The expression doesn’t compare 1990 to an indexed column; it compares 1990 to a value &lt;em&gt;calculated&lt;/em&gt; from the column, and that value must be computed for each row.&lt;/li&gt;
          &lt;li&gt;As a result, the index on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;created_date&lt;/code&gt; is not used because performing the query requires a full table scan.&lt;/li&gt;
          &lt;li&gt;You can fix this by just using a literal date like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WHERE created_date &amp;lt; 1990-01-01&lt;/code&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Don’t use wildcards at the beginning of a LIKE pattern if you’re really looking for the string only when it occurs at the beginning of the column, leave out the first &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EXPLAIN&lt;/code&gt; to verify optimizer operations. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EXPLAIN FORMAT=JSON&lt;/code&gt; is very, very cool. See the must-reads section for an awesome read about this one.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;Avoid overuse of MySQL’s automatic type conversion.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if int_col is an integer column, each of these queries will return the same result:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT * FROM test WHERE int_col = 7;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT * FROM test WHERE int_col = '7';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;BUT, the second query involves a small performance penalty for converting the integer and string to double to perform the comparison.
A more serious problem is that if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int_col&lt;/code&gt; is indexed, a comparison that involves type conversion may prevent the index from being used. Same can happen when comparing a string column to a numeric value.&lt;/p&gt;

&lt;hr style=&quot;clear:both;&quot; /&gt;

&lt;p&gt;There’s a lot more to touch when it comes to analyzing queries, indexes and the inner workings of the optimizer that I may come back here with more notes. I’ve listed a few great readings below for the interested.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;must-reads-on-this-topic&quot;&gt;Must-reads on this topic&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.ibm.com/developerworks/data/library/techarticle/dm-1309cardinal/index.html&quot; target=&quot;_blank&quot;&gt;Why low cardinality indexes negatively impact performance, Ember Crooks, IBM&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.informit.com/articles/article.aspx?p=377652&quot; target=&quot;_blank&quot;&gt;MySQL Query Optimization, Paul Dubois&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.percona.com/blog/2015/12/22/explain-formatjson-provides-us-all-details-about-subqueries-attached_subqueries-nested_loop-materialized_from_subquery-optimized_away_subqueries/&quot;&gt;EXPLAIN FORMAT=JSON: everything about attached_subqueries, optimized_away_subqueries, materialized_from_subquery, Sveta Smirnova, Percona&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://s.petrunia.net/blog/?p=83&quot;&gt;EXPLAIN FORMAT=JSON vs regular EXPLAIN, Sergey Petrunia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.percona.com/blog/2015/04/27/indexing-101-optimizing-mysql-queries-on-a-single-table/&quot;&gt;Indexing 101: Optimizing MySQL queries on a single table, Stephane Combaudon, Percona&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.percona.com/blog/2014/01/03/multiple-column-index-vs-multiple-indexes-with-mysql-56/&quot;&gt;Multiple column index vs multiple indexes, Stephane Combaudon, Percona&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.percona.com/blog/2015/05/08/mysql-indexing-101-a-challenging-single-table-query/&quot;&gt;MySQL indexing 101: a challenging single-table query, Stephane Combaudon, Percona&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Tue, 01 Oct 2019 07:15:21 +0000</pubDate>
        <link>http://ekins.space/2019/10/01/notes-on-analyzing-database-queries/</link>
        <guid isPermaLink="true">http://ekins.space/2019/10/01/notes-on-analyzing-database-queries/</guid>
        
        
      </item>
    
      <item>
        <title>Message Queues: RabbitMQ</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;Related posts: &lt;a href=&quot;http://ekins.space/2017/11/16/message-queues-overview/&quot;&gt;Message Queues: Overview&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;p&gt;RabbitMQ is a message broker; it accepts and forwards messages just like a post office.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Producing&lt;/code&gt; means sending a message. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Producers&lt;/code&gt; receive sent messages. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;queue&lt;/code&gt; is the name of a post box which lives inside RabbitMQ, only bound by the host’s memory and disk limits. Essentially it is a large message buffer.
Many producers can send messages that go to one queue and many consumers can try to receive data from that queue.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Consumers&lt;/code&gt; are programs that mostly wait to receive messages.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/workflow-rabbitmq.png&quot; alt=&quot;Basic RabbitMQ flow&quot; style=&quot;margin:0 auto&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;sending&quot;&gt;Sending&lt;/h5&gt;

&lt;p&gt;The publisher will connect to RabbitMQ, send a single message, then exit.&lt;/p&gt;

&lt;p&gt;Channel is where most of the API for getting things done resides. To send a message we declare a queue for us to send to; then we can publish a message.&lt;/p&gt;

&lt;p&gt;Declaring a queue is idempotent. Message content is a byte array so one can encode whatever needed.&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;declare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strict_types&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhpAmqpLib\Connection\AMQPStreamConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhpAmqpLib\Message\AMQPMessage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$connection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AMQPStreamConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'localhost'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5672&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'guest'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'guest'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$connection&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;queue_declare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'hello'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AMQPMessage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Hello World!'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;basic_publish&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'hello'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot; [x] Sent 'Hello World!'&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$connection&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h5 id=&quot;receiving&quot;&gt;Receiving&lt;/h5&gt;

&lt;p&gt;Same as sending, we open a connection and a channel and then declare a queue from which we will consume.&lt;/p&gt;

&lt;p&gt;Our code will block while our channel has callbacks. Whenever we receive a message our callback function will be passed the received message.&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;declare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;strict_types&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhpAmqpLib\Connection\AMQPStreamConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$connection&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AMQPStreamConnection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'localhost'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5672&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'guest'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'guest'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$connection&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;queue_declare&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'hello'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;' [*] Waiting for messages. To exit press CTRL+C'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$callback&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot; [x] Received &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;basic_consume&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'hello'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$callback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;callbacks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;img src=&quot;/assets/message-queue.png&quot; alt=&quot;Message Queue&quot; style=&quot;margin:0 auto&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;exchanges&quot;&gt;Exchanges&lt;/h4&gt;

&lt;p&gt;Messages are not published directly to a queue, instead, the producer sends messages to an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exchange&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;An exchange is responsible for the routing of the messages to the different queues.&lt;/li&gt;
  &lt;li&gt;An exchange accepts messages from the producer application and routes them to message queues with the help of bindings and routing keys.&lt;/li&gt;
  &lt;li&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binding&lt;/code&gt; is a link between a queue and an exchange.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;message-flow-in-rabbitmq&quot;&gt;Message Flow in RabbitMQ&lt;/h5&gt;

&lt;p&gt;&lt;img src=&quot;/assets/exchanges-bindings-routing-keys.png&quot; alt=&quot;Message flow in RabbitMQ&quot; style=&quot;margin:0 auto&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The producer publishes a message to an exchange. When you create the exchange, you have to specify the type of it. &lt;a href=&quot;https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchanges&quot;&gt;Read&lt;/a&gt; about different types of exchanges.&lt;/li&gt;
  &lt;li&gt;The exchange receives the message and is now responsible for the routing of the message. The exchange takes different message attributes into account, such as routing key, depending on the exchange type.&lt;/li&gt;
  &lt;li&gt;Bindings have to be created from the exchange to queues. In this case, we see two bindings to two different queues from the exchange. The Exchange routes the message into the queues depending on message attributes.&lt;/li&gt;
  &lt;li&gt;The messages stay in the queue until they are handled by a consumer&lt;/li&gt;
  &lt;li&gt;The consumer handles the message.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;key-terminology&quot;&gt;Key terminology:&lt;/h5&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Producer:&lt;/code&gt; Application that sends the messages.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Consumer:&lt;/code&gt; Application that receives the messages.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Queue:&lt;/code&gt; Buffer that stores messages.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Message:&lt;/code&gt; Information that is sent from the producer to a consumer through RabbitMQ.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection:&lt;/code&gt; A connection is a TCP connection between your application and the RabbitMQ broker.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Channel:&lt;/code&gt; A channel is a virtual connection inside a connection. When you are publishing or consuming messages from a queue - it’s all done over a channel.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Exchange:&lt;/code&gt; Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. In order to receive messages, a queue needs to be bound to at least one exchange.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Binding:&lt;/code&gt; A binding is a link between a queue and an exchange.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Routing key:&lt;/code&gt; The routing key is a key that the exchange looks at to decide how to route the message to queues. The routing key is like an address for the message.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMQP:&lt;/code&gt; AMQP (Advanced Message Queuing Protocol) is the protocol used by RabbitMQ for messaging.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Users:&lt;/code&gt; It is possible to connect to RabbitMQ with a given username and password. Every user can be assigned permissions such as rights to read, write and configure privileges within the instance. Users can also be assigned permissions to specific virtual hosts.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vhost, virtual host:&lt;/code&gt; A Virtual host provides a way to segregate applications using the same RabbitMQ instance. Different users can have different access privileges to different vhost and queues and exchanges can be created so they only exist in one vhost.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;resources&quot;&gt;Resources&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.rabbitmq.com/tutorials/amqp-concepts.html&quot; target=&quot;_blank&quot;&gt;AMQP Concepts&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cloudamqp.com/blog/2015-05-18-part1-rabbitmq-for-beginners-what-is-rabbitmq.html&quot; target=&quot;_blank&quot;&gt;CloudAMQP, RabbitMQ&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 16 Nov 2017 17:39:59 +0000</pubDate>
        <link>http://ekins.space/2017/11/16/message-queues-rabbitmq/</link>
        <guid isPermaLink="true">http://ekins.space/2017/11/16/message-queues-rabbitmq/</guid>
        
        
      </item>
    
      <item>
        <title>Message Queues: Overview</title>
        <description>&lt;h4 id=&quot;queue&quot;&gt;Queue&lt;/h4&gt;

&lt;p&gt;A queue is a kind of abstract data type, a collection, in which the entities are kept in order and its main operations are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enqueuing&lt;/code&gt; additional entities to the rear terminal position, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dequeuing&lt;/code&gt; entities from the front.
Hence a queue is a sequential, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;first in first out (FIFO)&lt;/code&gt; data structure.&lt;/p&gt;

&lt;p&gt;A bounded queue is a queue with a fixed (limited) size.&lt;/p&gt;

&lt;p&gt;An efficient implementation of a queue should perform the operations - enqueuing and dequeuing - in O(1) time. There are several implementations;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A doubly linked list has O(1) insertion and deletion at both ends.&lt;/li&gt;
  &lt;li&gt;A sinly linked list only has efficient insertion and deletion at one end, however keeping a pointer to the last note in addition to the first one allows one to implement a more efficient queue.&lt;/li&gt;
  &lt;li&gt;A deque implemented using a modified dynamic array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;message-queues&quot;&gt;Message Queues&lt;/h4&gt;

&lt;p&gt;Message queues provide an asynchronous communication protocol where the sender and receiver do not need to interact with the queue at the same time.
Messages inserted into the queue are stored until the recipient retrieves them.&lt;/p&gt;

&lt;p&gt;You could consider a queue as a line of tasks waiting to be handled in a sequential order. A message queue is a list of messages sent between applications.
A message is the transported data between the sender and receiver applications. Essentially it is a byte array with some headers. A message could tell a system to start processing a task or contain a response from a finished task or just be a plaintext.&lt;/p&gt;

&lt;p&gt;The consumer application connects to the queue and gets the messages to be placed, until then the messages are stored within the queue.&lt;/p&gt;

&lt;p&gt;Consider an email.&lt;/p&gt;

&lt;p&gt;When an email is sent the sender keeps processing other things without an instant response from the receiver. This decouples producers and consumers of a message, allowing them to interact with the queue separately from each other.&lt;/p&gt;

&lt;p&gt;Imagine you have a service that receives many requests every second, where no request is affordable to lose and all requests need to be processed by a process that consumes plenty of time.
If you want your service to always be highly available and ready to receive more requests instead of being locked processing the previous requests, it is very ideal to implement a queue between the services that take and process requests.&lt;/p&gt;

&lt;p&gt;Scaling such a system can be done now by adding more workers and receivers to work on queues faster.&lt;/p&gt;

&lt;h4 id=&quot;amqp---advanced-message-queuing-protocol&quot;&gt;AMQP - Advanced Message Queuing Protocol&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;p&gt;An open standard application layer protocol for message-oriented middleware, featuring message orientation, queuing, routing (pub-sub, point-to-point), reliability and security.
AMQP mandates the behavior of the messaging provider and client to the extent that implementations from different vendors are interoperable, in the same way as SMTP, HTTP, FTP, etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The basic unit of data in AMQP is a frame. There are nine AMQP frame bodies defined that are used to initiate, control and tear down the transfer of messages between two peers. These are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;open&lt;/li&gt;
  &lt;li&gt;begin&lt;/li&gt;
  &lt;li&gt;attach&lt;/li&gt;
  &lt;li&gt;transfer&lt;/li&gt;
  &lt;li&gt;flow&lt;/li&gt;
  &lt;li&gt;disposition&lt;/li&gt;
  &lt;li&gt;detach&lt;/li&gt;
  &lt;li&gt;end&lt;/li&gt;
  &lt;li&gt;close&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;link protocol&lt;/code&gt; is at the heart of AMQP.&lt;/p&gt;

&lt;p&gt;An attach frame body is sent to initiate a new link; a detach to tear down a link. Links may be established in order to receive or send messages.&lt;/p&gt;

&lt;p&gt;Messages are sent over an established link using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;the transfer frame&lt;/code&gt;. Messages on a link flow in only one direction.&lt;/p&gt;

&lt;p&gt;Transfers are subject to a credit based flow control scheme, managed using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flow frames&lt;/code&gt;. This allows a process to protect itself from being overwhelmed by too large a volume of messages or more simply to allow a subscribing link to pull messages as and when desired.&lt;/p&gt;

&lt;h5 id=&quot;amqp-0-9-1-model&quot;&gt;AMQP 0-9-1 Model&lt;/h5&gt;

&lt;p&gt;According to this model messages are published to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exchanges&lt;/code&gt;, which are often compared to mailboxes. 
Exchanges then distribute message copies to queues using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bindings&lt;/code&gt;. 
Then AMQP brokers either deliver messages to consumers subscribed to the queues or consumers fetch/pull messages from queues on demand.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/amqp.png&quot; alt=&quot;AMQP 0-9-1 Model&quot; style=&quot;margin:0 auto&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Networks are unreliable and applications may fail to process messages therefore the AMQP model has a notion of message acknowledgements: when a message is delivered to a consumer the consumer notifies the broker, either automatically or as soon as the application developer chooses to do so. 
When message acknowledgements are in use, a broker will only completely remove a message from a queue when it receives a notification for that message (or group of messages).&lt;/p&gt;

&lt;h4 id=&quot;stomp---streaming-text-oriented-message-protocol&quot;&gt;STOMP - Streaming Text Oriented Message Protocol&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;p&gt;STOMP/TTMP, is a simple text-based protocol, designed for working with message-oriented middleware. It provides an interoperable wire format that allows STOMP clients to talk with any message broker supporting the protocol. It is thus language-agnostic, meaning a broker developed for one programming language or platform can receive communications from client software developed in another language.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Being very similar to HTTP, it works over TCP using the following commands&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;CONNECT&lt;/li&gt;
  &lt;li&gt;SEND&lt;/li&gt;
  &lt;li&gt;SUBSCRIBE&lt;/li&gt;
  &lt;li&gt;UNSUBSCRIBE&lt;/li&gt;
  &lt;li&gt;BEGIN&lt;/li&gt;
  &lt;li&gt;COMMIT&lt;/li&gt;
  &lt;li&gt;ABORT&lt;/li&gt;
  &lt;li&gt;ACK&lt;/li&gt;
  &lt;li&gt;NACK&lt;/li&gt;
  &lt;li&gt;DISCONNECT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Communication between client and server is through a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame&lt;/code&gt; consisting of a number of lines. 
The first line contains the command, followed by headers in the form &lt;key&gt;: &lt;value&gt; (one per line), followed by a blank line and then the body content, ending in a `null character`. 
Communication between server and client is through a `MESSAGE`, `RECEIPT` or `ERROR frame` with a similar format of headers and body content.&lt;/value&gt;&lt;/key&gt;&lt;/p&gt;

&lt;h4 id=&quot;posix-message-queues&quot;&gt;POSIX message queues&lt;/h4&gt;

&lt;p&gt;POSIX message queues allow processes to exchange data in the form of messages, providing similar functionality to System V message queues &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(msgget, msgsnd, msgrcv)&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Message queues are created with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_open&lt;/code&gt; which returns a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;message queue descriptor (mqd_t)&lt;/code&gt; which is used to refer to the opened message queue in later calls.&lt;/li&gt;
  &lt;li&gt;Two processes can operate on the same queue by passing the same name to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_open&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Messages are transferred to and from a queue using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_send&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_receive&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;When a process has finished using the queue, it closes it using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_close&lt;/code&gt;, and when the queue is no longer required, it can be deleted using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_unlink&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Queue attributes can be retrieved and (in some cases) modified using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_getattr&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_setattr&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;A process can request asynchronous notification of the arrival of a message on a previously empty queue using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mq_notify&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;poll&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;epoll&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;Next: &lt;a href=&quot;http://ekins.space/2017/11/16/message-queues-rabbitmq/&quot;&gt;Message Queues: RabbitMQ&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;resources&quot;&gt;Resources&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.rabbitmq.com/tutorials/amqp-concepts.html&quot; target=&quot;_blank&quot;&gt;AMQP Concepts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 16 Nov 2017 16:19:59 +0000</pubDate>
        <link>http://ekins.space/2017/11/16/message-queues-overview/</link>
        <guid isPermaLink="true">http://ekins.space/2017/11/16/message-queues-overview/</guid>
        
        
      </item>
    
      <item>
        <title>Collision Resolution: Linear Probing</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;Related data structures: &lt;a href=&quot;http://ekins.space/2017/10/23/hashtable/&quot;&gt;Hashtable&lt;/a&gt;, &lt;a href=&quot;http://ekins.space/2017/07/20/array/&quot;&gt;Array&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;linear-probing&quot;&gt;Linear Probing&lt;/h5&gt;
&lt;p&gt;This method resolves the problem by probing or searching through alternative locations in the probe sequence until the target or an unused slot is found.&lt;/p&gt;

&lt;p&gt;When two records demand for the same location in the hash table, the collision can be solved by placing second record linearly down wherever an empty location is found.
Lookups are performed in the same way (by searching the table sequentially starting at the position given by the hash function) until finding a cell with a matching key or an empty cell.
Because linear probing is especially sensitive to unevenly distributed hash values, it is important to combine it with a high-quality hash function that does not produce such irregularities.&lt;/p&gt;

&lt;p&gt;Consider the following hash table.&lt;/p&gt;

&lt;table style=&quot;float: left;margin: 0 2% 0 0;font-size:12px;&quot; class=&quot;mbtablestyle&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Index&lt;/th&gt;
      &lt;th&gt;Data&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;131&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;2&lt;/td&gt;
      &lt;td&gt;21&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;3&lt;/td&gt;
      &lt;td&gt;31&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;4&lt;/td&gt;
      &lt;td&gt;4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;6&lt;/td&gt;
      &lt;td&gt;61&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;7&lt;/td&gt;
      &lt;td&gt;7&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;8&lt;/td&gt;
      &lt;td&gt;8&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;9&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;div style=&quot;padding: 10px;float: left;width: 80%;text-align: justify;&quot;&gt;
&lt;p&gt;Given the hash table on the left, if the first number which is to be placed is 131 then 131 % 10 equals to 1. The remainder is 1, so the hash key is 1. 
That means we are supposed to place the record at index 1.
&lt;br /&gt;&lt;br /&gt;
Next number is 21 which also gives hash key = 1 as 21 % 10 = 1. But 131 is already placed at index 1. That means collision has occurred. 
We will now apply linear probing. 
&lt;br /&gt;&lt;br /&gt;
In this method, we will search the place for number 21 from location of 131. In this case we can place the number 21 at index 2. 
Then if 31 has to be stored, it can be stored at index 3.
Similarly, 61 can be stored at 6 because number 4 and 5 are stored before 61.&lt;br /&gt;
This technique makes the search operation efficient, as we have to search only limited list to obtain the desired number.
&lt;/p&gt;
&lt;/div&gt;

&lt;p style=&quot;clear:both;&quot;&gt;
Example hashtable implementation holding maximum 10 student records.
&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
&lt;/span&gt; 
&lt;span class=&quot;cp&quot;&gt;#define LIMIT 10
&lt;/span&gt; 
&lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;record_status&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EMPTY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DELETED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OCCUPIED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
 
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Student&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
 
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Record&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;enum&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;record_status&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt; 
 
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hash_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;search_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EMPTY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;insert_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EMPTY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DELETED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OCCUPIED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
            &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;New record inserted into hashtable.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Cannot insert duplicate record.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;temp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Hashtable limit exceeded.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Hashtable&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;[%d]:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OCCUPIED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Occupied - ID: %d Name: %s Age: %d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DELETED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Record was deleted&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Empty&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;delete_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[])&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;search_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Could not find the key&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;position&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DELETED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Record&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Student&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EMPTY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;1. Insert a student record&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;2. Delete a student record&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;3. Search a student by ID&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;4. Display All&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;5. Exit&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enter option number:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Enter Student ID:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
                
                &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enter Student Name:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

                &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enter Student Age:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

                &lt;span class=&quot;n&quot;&gt;insert_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;student_rec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Enter the key (ID) to delete a student:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

                &lt;span class=&quot;n&quot;&gt;delete_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Enter the key (ID) to search for a student:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;      
                &lt;span class=&quot;n&quot;&gt;scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;search_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Could not find a student record with given ID.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;Student record found at index position:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;print_records&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hash_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; 
                &lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
</description>
        <pubDate>Mon, 30 Oct 2017 02:44:44 +0000</pubDate>
        <link>http://ekins.space/2017/10/30/collision-resolution-linear-probing/</link>
        <guid isPermaLink="true">http://ekins.space/2017/10/30/collision-resolution-linear-probing/</guid>
        
        
      </item>
    
      <item>
        <title>Collision Resolution: Bucket Hashing</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;Related data structures: &lt;a href=&quot;http://ekins.space/2017/10/23/hashtable/&quot;&gt;Hashtable&lt;/a&gt;, &lt;a href=&quot;http://ekins.space/2017/07/20/array/&quot;&gt;Array&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;bucket-hashing&quot;&gt;Bucket Hashing&lt;/h5&gt;
&lt;p&gt;Bucket hashing is treating the hash table as a two dimensional array instead of a linear array.&lt;/p&gt;

&lt;p&gt;Consider a hash table with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; slots that are divided into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; buckets, with each bucket consisting of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S/B&lt;/code&gt; slots.
The hash function assigns each record to the first slot within one of the buckets. If the slot was already occupied then the bucket slots are searched sequentially until an empty slot is found.
If the bucket is completely full, the record will be stored in an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;overflow bucket&lt;/code&gt; of infinite capacity at the end of the table, which is shared by all buckets. Which makes bucket hashing a form of closed hashing implementation.
An ideal implementation will use a hash function that distributes the records evenly among all buckets so there will be as few records as possible to store in the overflow bucket.&lt;/p&gt;

&lt;div style=&quot;float:left; width:20%;margin: 0 2% 0 0;&quot;&gt;
&lt;table style=&quot;font-size: 14px;&quot; class=&quot;mbtablestyle&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Index&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Bucket&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Data&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;0&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B0[0]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;30&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B0[1]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;20&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B1[0]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B1[1]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B2[0]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;5&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B2[1]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;6&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B3[0]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;18&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;7&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B3[1]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;38&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;8&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B4[0]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;9&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;B4[1]&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;table style=&quot;margin: 12% auto 0;font-size: 12px;&quot; class=&quot;mbtablestyle&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Overflow Bucket&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;48&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;25&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;

&lt;div style=&quot;padding: 0;float: left;width: 75%;text-align: justify;&quot; class=&quot;normalize&quot;&gt;
&lt;p&gt;
Given this bucket hash table for an array of size 10 storing 5 buckets, each bucket having two slots in size, let's demonstrate how this method works in practice.
We also have an overflow bucket of infinite size on the right to store records when the buckets in the main hash table are occupied. 
I will be using mod operation as the hash function.
&lt;/p&gt;
&lt;/div&gt;

&lt;div style=&quot;padding: 0;float: left;width: 75%;text-align: justify;&quot; class=&quot;normalize&quot;&gt;
&lt;p&gt;
Let us start by inserting the number 18 as our first record. Since we have 5 buckets, we take mod 5. 18 % 5 is 3. 
We put this into the top of B3, which is slot 6 of the hash table.&lt;br /&gt;
Now inserting a record for 30. 30 % 5 is 0. 30 goes into B0[0].&lt;br /&gt;
Next we insert a record for 38; 38 % 5 is 3 so it will be placed in B3[1].&lt;br /&gt;
Next up we have 48. 48 % 5 is 3, but the B3 is already full, hence we store 48 in the first available slot of our overflow bucket.&lt;br /&gt;
We can now try with 20. 20 % 5 is 0; B0[0] is occupied hence it will be stored in B0[1].&lt;br /&gt;
Now if we insert 25, 25 % 5 is 0 and we know both slots of B0 are occupied now, hence it will end up in our overflow bucket.
&lt;/p&gt;
&lt;/div&gt;

&lt;div style=&quot;padding: 0;float: left;width: 75%;text-align: justify;&quot; class=&quot;normalize&quot;&gt;
&lt;p&gt;
When looking for a record, we first take its hash value and search the resulting bucket.&lt;br /&gt;
If we search for key value 20, we search in B0, first checking B0[0] which holds a different value, so we check B0[1] and we find our key.&lt;br /&gt;
When searching for the key value 25, we look in B0 sequentially. We see it doesn't hold our key value and it is full, hence we look through the overflow bucket. First checking OB[0], then OB[1] and we have found it.&lt;br /&gt;
Note that if there are many records in the overflow bucket, this will be an expensive process. 
&lt;/p&gt;
&lt;/div&gt;

&lt;hr style=&quot;clear:both;&quot; /&gt;

&lt;p style=&quot;clear:both;&quot;&gt;Bucket methods are good for implementing hash tables stored on disk, because the bucket size can be set to the size of a disk block. Whenever search or insertion occurs, the entire bucket is read into memory. Because the entire bucket is then in memory, processing an insert or search operation requires only one disk access, unless the bucket is full. If the bucket is full, then the overflow bucket must be retrieved from disk as well. Naturally, overflow should be kept small to minimize unnecessary disk accesses.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Next: &lt;a href=&quot;http://ekins.space/2017/10/29/collision-resolution-linear-probing/&quot;&gt;Collision Resolution: Linear Probing&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
</description>
        <pubDate>Mon, 30 Oct 2017 01:14:59 +0000</pubDate>
        <link>http://ekins.space/2017/10/30/collision-resolution-bucket-hashing/</link>
        <guid isPermaLink="true">http://ekins.space/2017/10/30/collision-resolution-bucket-hashing/</guid>
        
        
      </item>
    
      <item>
        <title>Hashtable</title>
        <description>&lt;blockquote&gt;
  &lt;p&gt;Related data structures: &lt;a href=&quot;http://ekins.space/2017/07/20/array/&quot;&gt;Array&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A map, symbol table, a dictionary or an associative array is considered an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;abstract data type&lt;/code&gt; composed of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collection of (unique_key, value) pairs&lt;/code&gt;.
Operations associated with this data type are addition and removal of a pair in a collection, lookup values by their keys and modification of an existing pair.&lt;/p&gt;

&lt;p&gt;A hashtable uses the key of each record to determine the location in an array structure. To achieve this, the key is passed into a hash function which returns a numeric value based on the key.&lt;/p&gt;

&lt;h4 id=&quot;hash-functions&quot;&gt;Hash Functions&lt;/h4&gt;

&lt;p&gt;A good Hash Function should&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;be simple to compute.&lt;/li&gt;
  &lt;li&gt;always return the same numeric value when given the exact same key.&lt;/li&gt;
  &lt;li&gt;have less number of collisions while placing the record in the hash table. Ideally none should occur (perfect hash function).&lt;/li&gt;
  &lt;li&gt;produce such keys which will get distributed uniformly over an array.&lt;/li&gt;
  &lt;li&gt;depend on every bit of the key. Thus a hash function that simply extracts the portion of a key is not suitable.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;load-factor&quot;&gt;Load Factor&lt;/h5&gt;
&lt;p&gt;The load factor denoted by the symbol &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;λ&lt;/code&gt; measures the fullness of the hash table. It is calculated by the formula &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;λ = number of records in table ÷ number of locations&lt;/code&gt;.&lt;/p&gt;

&lt;h4 id=&quot;dictionary-problem&quot;&gt;Dictionary Problem&lt;/h4&gt;
&lt;p&gt;Data structures that maintain a set of data during search, delete and insert operations suffer from a data structural problem known as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dictionary problem&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Consider the following data structural problem.&lt;/p&gt;

&lt;p&gt;We have a set of items. Each item is a (key, value) pair. Keys are in the range &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{1,...,U}&lt;/code&gt; and values are arbitrary. 
A data structure supporting the following operations is called a dynamic dictionary.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;insert(k, v): insert a new item into the database with key k and value v. If an item with key k already exists in the database, update its value to v.&lt;/li&gt;
  &lt;li&gt;delete(x): delete item x from the database (we assume x is a pointer to the item)&lt;/li&gt;
  &lt;li&gt;query(k): return the the value associated with key k, or null if key k is not in the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a dynamic data structural problem since we also need to support insertion and deletion.
(Dynamic variants of data structure problems are ones that support the data set being updated.)&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hashing&lt;/code&gt; is a known method to solve this problem.&lt;/p&gt;

&lt;p&gt;A hash family H is a set of functions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h: {1,...,U} -&amp;gt; {1,...,m}&lt;/code&gt;. 
The idea is to maintain an array of size &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m&lt;/code&gt; where an item with key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; is stored at the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h(k)th&lt;/code&gt; position of the array.
Unfortunately, due to collisions this would not work; Two different keys &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k, k'&lt;/code&gt; in a database may have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h(k) = h(k')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Assume we initialize an array &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt; of size &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X[i]&lt;/code&gt; stores the pointer to the head of a doubly linked list. We pick a hash &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; at random from a hash family.
This way, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X[i]&lt;/code&gt; will be a doubly linked list containing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(key, value)&lt;/code&gt; pairs of all items whose key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; satisfies &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h(k) = i&lt;/code&gt;.
Basically, colliding items are stored in the same linked list.&lt;/p&gt;

&lt;p&gt;For inserting a (k,v) pair you simply insert the pair to the head of the list at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X[h(k)]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To query &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; you traverse the list at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X[h(k)]&lt;/code&gt; until finding an item with key &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt; or see that the key does not exist.&lt;/p&gt;

&lt;p&gt;Deletion splices the item out of the doubly linked list when the key is found.&lt;/p&gt;

&lt;h4 id=&quot;collisions&quot;&gt;Collisions&lt;/h4&gt;
&lt;p&gt;Since a hash function translates all keys into indexes in an array, it is often the case that there will be many more keys than there are indexes. 
Therefore it is always a possibility that multiple keys will be translated into the same index, which is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collision&lt;/code&gt;. Generally speaking, collisions are unavoidable.
Thus, hashing implementations must have a collision resolution policy. Collision resolution methods can be broken into two classes: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open hashing&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;closed hashing&lt;/code&gt;.
The difference between the two methods has to do with whether collisions are stored outside the table (open hashing) or at another slot in the same table (closed hashing).&lt;/p&gt;

&lt;p&gt;The simplest way of implementing a form of open hashing is to define each slot in the hash table to be the head of a linked list. All records that hash to a specific slot are placed on its linked list.
Ordering of records within a linked list of a slot can be done with insertion sort, key-value ordering, or frequency-of-access order.  Ordering the list by key value provides an advantage in the case of an unsuccessful search, because we know to stop searching the list once we encounter a key that is greater than the one being searched for. 
If records on the list are unordered or ordered by frequency, then an unsuccessful search will need to visit every record on the list.&lt;/p&gt;

&lt;p&gt;Open hashing is most appropriate when the hash table is kept in main memory, with the lists implemented by a standard in-memory linked list. Storing an open hash table on disk in an efficient way is difficult, because members of a given linked list might be stored on different disk blocks. 
This would result in multiple disk accesses when searching for a particular key value, which defeats the purpose of using hashing.&lt;/p&gt;

&lt;p&gt;Closed hashing stores all records directly in the hash table. Each record &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; with key value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;K&lt;/code&gt; has a home position &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h(K)&lt;/code&gt; that is computed by the hash function. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; is inserted and another value already fills the computed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h(K)&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; will be stored at some other slot in the table.
Which collision detection method is used determines which slot that may be.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Next: &lt;a href=&quot;http://ekins.space/2017/10/29/collision-resolution-bucket-hashing/&quot;&gt;Collision Resolution: Bucket Hashing&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
</description>
        <pubDate>Mon, 23 Oct 2017 18:28:59 +0000</pubDate>
        <link>http://ekins.space/2017/10/23/hashtable/</link>
        <guid isPermaLink="true">http://ekins.space/2017/10/23/hashtable/</guid>
        
        
      </item>
    
      <item>
        <title>Array</title>
        <description>&lt;p&gt;Array as a data structure, is simply a collection of elements each identified by an index or a key. In many cases you can consider an array as a synonym of the word table.
One can use arrays to implement other data structures and concepts such as strings, lists and lookup tables. Some programming languages use hash tables, linked lists, search trees etc. to implement arrays.&lt;/p&gt;

&lt;h5 id=&quot;from-a-mathematical-perspective&quot;&gt;From a mathematical perspective&lt;/h5&gt;
&lt;p&gt;A tuple is a finite ordered sequence of elements. A sequence of n (non-negative integer) elements is an n-tuple. 0-tuple is an empty sequence.
In this sense, one could consider tuples as the mathematical equivalent of arrays.&lt;/p&gt;

&lt;p&gt;Arrays exploit the addressing logic of computers. The memory address of the first element of an array is called the foundation address.&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula.
For example, an array of 10 32-bit integer variables, with indices 0 through 9, may be stored as 10 words at memory addresses 2000, 2004, 2008, … 2036, 
so that the element with index i has the address 2000 + 4 × i.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A matrix can be represented as a two-dimensional grid, hence two-dimensional arrays can be considered as matrices.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;arrays-in-different-programming-languages&quot;&gt;Arrays in different programming languages&lt;/h4&gt;

&lt;h5 id=&quot;c-arrays&quot;&gt;C Arrays&lt;/h5&gt;
&lt;p&gt;Arrays in C store related data under a single variable name with an index, also known as the subscript. It’s simply a list of ordered grouping for variables of the same type.
Arrays can be considered as a constant pointer.&lt;/p&gt;

&lt;p&gt;You declare an array as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type name[number of elements]={comma-separated values}&lt;/code&gt; e.g.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int point[6]={0,0,1,0,0,0};&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;char letters[29];&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;float matrix[ NROWS ][ NCOLS ];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can omit the array dimension if you type it as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int point[]={0,0,1,0,0,0};&lt;/code&gt; (The index in C is actually an offset from the beginning of the array. The first element of this array is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;point[0]&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;If the dimension is specified, but not all elements in the array are initialized, the remaining elements will contain a value of 0.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int numbers[2000]={245};&lt;/code&gt; sets the first value of the array to 245, and the rest to 0.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;char two_dim[3][5];&lt;/code&gt; is a two dimensional array that has 3 rows and 5 columns.
To access a value we need two indexes:&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;two_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;two_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;'x'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/* Initialization of a multi-dimensional array */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;two_d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/* More examples */&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;float&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;floatArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;piFractions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;141592654&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;570796327&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;785398163&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/* In C99 there is an alternative mechanism that allows you to initialize arrays that have non-sequential indexes. 
 * This method can be mixed in with traditional initialization.
 *
 * If the size of the array is not given, 
 * the largest initialized position determines the size of the array.
 */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;420&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt; 
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/* first 20 Fibonacci numbers */&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
        
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Fibonacci[ %d ] = %f&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fibonacci&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Array keys have to be integers and need to be continuous. If you have the keys 0, 1 and 2, then the next key has to be 3 and can’t be 234567890.&lt;/p&gt;

&lt;hr /&gt;

&lt;h5 id=&quot;php&quot;&gt;PHP&lt;/h5&gt;

&lt;p&gt;The underlying PHP implementation for arrays varies between different versions of PHP but its main difference with C’s own implementation is that it supports non-continuous integer keys and allows mixing them with string keys.&lt;/p&gt;

&lt;p&gt;One can implement such an array either by using&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;a binary search tree &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;search complexity: O(log n), access complexity (best case): O(n)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;a hashtable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;complexity: O(1)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the Resources section for a set of related articles about hashtables (hash maps) and varying PHP implementations that take the topic more in detail.&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bar&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;ohai&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# array(4) { [0]=&amp;gt; string(3) &quot;foo&quot; [1]=&amp;gt; string(3) &quot;bar&quot; [2]=&amp;gt; string(4) &quot;ohai&quot; }&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# array(4) { [0]=&amp;gt; string(1) &quot;a&quot; [1]=&amp;gt; string(1) &quot;b&quot; [6]=&amp;gt; string(1) &quot;c&quot; [7]=&amp;gt; string(1) &quot;d&quot; }&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;1&quot;&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;b&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;c&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;d&quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# array(1) { [1]=&amp;gt; string(1) &quot;d&quot; }&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;Foo&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Bar&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;    &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&quot;multi&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
         &lt;span class=&quot;s2&quot;&gt;&quot;dimensional&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
             &lt;span class=&quot;s2&quot;&gt;&quot;universe&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;what?&quot;&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Notice: Undefined index: foo in test.php on line 13&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# NULL&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Foo&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# string(3) &quot;Bar&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# int(42)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;var_dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;multi&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;dimensional&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;universe&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# string(5) &quot;what?&quot;&lt;/span&gt;
 &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;hr /&gt;

&lt;h5 id=&quot;javascript&quot;&gt;JavaScript&lt;/h5&gt;

&lt;p&gt;The JavaScript Array object is a global object that is used in the construction of arrays; which are high-level, list-like objects.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array’s length can change at any time, and data can be stored at non-contiguous locations in the array, JavaScript arrays are not guaranteed to be dense; this depends on how the programmer chooses to use them. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Arrays cannot use strings as element indexes (as in an associative array), but must use integers. Setting or accessing via non-integers using bracket notation (or dot notation) will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array’s object property collection. The array’s object properties and list of array elements are separate, and the array’s traversal and mutation operations cannot be applied to these named properties.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fruits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Apple&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Banana&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 2&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Apple&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Banana&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;fruits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Apple 0&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Banana 1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;hr /&gt;

&lt;h5 id=&quot;common-lisp&quot;&gt;Common Lisp&lt;/h5&gt;

&lt;p&gt;In principle, an array in Common Lisp may have any number of dimensions, including zero. (A zero-dimensional array has exactly one element.) 
In practice, an implementation may limit the number of dimensions supported, but every Common Lisp implementation must support arrays of up to seven dimensions.
Each dimension is a non-negative integer; if any dimension of an array is zero, the array has no elements.&lt;/p&gt;

&lt;p&gt;An array may be a general array, meaning each element may be any Lisp object, or it may be a specialized array, meaning that each element must be of a given restricted type.&lt;/p&gt;

&lt;p&gt;One-dimensional arrays are called vectors. General vectors may contain any Lisp object.&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-lisp&quot; data-lang=&quot;lisp&quot;&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dimensions&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;amp;key&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;element-type&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;initial-element&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;initial-contents&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;adjustable&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fill-pointer&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;displaced-to&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;displaced-index-offset&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;new-array&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;Argument&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;description:&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;dimensions&lt;/span&gt;              &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dimensions,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;non-negative&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;integer&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;element-type&lt;/span&gt;            &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;specifier,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;any&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;type&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;initial-element&lt;/span&gt;         &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;value,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;implementation&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;dependent&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;initial-contents&lt;/span&gt;        &lt;span class=&quot;nv&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;object&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;adjustable&lt;/span&gt;              &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;generalized&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;boolean,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NIL&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;fill-pointer&lt;/span&gt;            &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;valid&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;fill&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pointer&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;array,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NIL&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;displaced-to&lt;/span&gt;            &lt;span class=&quot;nv&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NIL,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NIL&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;displaced-index-offset&lt;/span&gt;  &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;valid&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;array&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;row-major&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;displaced&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;arrays,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;default&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;new-array&lt;/span&gt;               &lt;span class=&quot;nv&quot;&gt;an&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;array&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-element&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;#(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-element&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;#2A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;length&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:fill-pointer&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;array-dimensions&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:fill-pointer&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:element-type&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'bit&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-element&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;x&quot;&gt;#*0000000000&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:element-type&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'character&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-element&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;#\a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;aaaaaaaaaa&quot;&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-element&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'x&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:adjustable&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;adjust-array&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-element&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;'y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;#2A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;make-array&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial-contents&lt;/span&gt;
           &lt;span class=&quot;o&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;e&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;resources&quot;&gt;Resources&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://nikic.github.io/2014/12/22/PHPs-new-hashtable-implementation.html&quot; target=&quot;_blank&quot;&gt;PHP’s new hashtable implementation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.jpauli.tech/2016/04/08/hashtables.html&quot; target=&quot;_blank&quot;&gt;PHP 7 Arrays : HashTables&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.cppreference.com/w/c/language/array_initialization&quot; target=&quot;_blank&quot;&gt;C Arrays, Initialization&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array&quot; target=&quot;_blank&quot;&gt;Array, MDN&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.lispworks.com/documentation/HyperSpec/Body/f_mk_ar.htm&quot; target=&quot;_blank&quot;&gt;Common Lisp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Thu, 20 Jul 2017 18:29:59 +0000</pubDate>
        <link>http://ekins.space/2017/07/20/array/</link>
        <guid isPermaLink="true">http://ekins.space/2017/07/20/array/</guid>
        
        
      </item>
    
      <item>
        <title>Function Trampoline</title>
        <description>&lt;p&gt;Trampolines are memory locations holding addresses pointing to interrupt service routines (&lt;a href=&quot;https://en.wikipedia.org/wiki/Interrupt_handler&quot;&gt;interrupt handlers&lt;/a&gt;), I/O routines, etc. Also referred to as indirect jump vectors. Execution jumps into the trampoline and then immediately jumps out, or bounces, hence the term trampoline.&lt;/p&gt;

&lt;p&gt;Trampoline can be used to overcome the limitations imposed by a CPU architecture that expects to always find vectors in fixed locations.&lt;/p&gt;

&lt;p&gt;When an operating system is booted on a symmetric multiprocessing (SMP) machine, only one processor, the boot-strap processor, will be active.
After the operating system has configured itself, it will instruct the other processors to jump to a piece of trampoline code that will initialize the processors
and wait for the operating system to start scheduling threads on them.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;A trampoline can be a loop that iteratively invokes thunk-returning functions (continuation-passing style). A thunk is essentially a function that wraps a call to another function, along with any parameters it needs, for later execution. A way to emulate lazy evaluation.&lt;/p&gt;

&lt;p&gt;The goal of a trampoline is to prevent recursion in function calls.&lt;/p&gt;

&lt;p&gt;The idea is to transform our function, so that its caller can detect when it recurses or when it leaves. If it performs a recursive call to itself, the trampoline will control its stack by becoming its callee. If it returns its result, the trampoline should notice it and stop.&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trampoline_factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;trampoline_factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You see here that this function doesn’t directly call itself, but it wraps its recursive call into a closure. This is because now we won’t launch our recursive function directly, but using a trampoline.&lt;/p&gt;

&lt;p&gt;When the trampoline sees a closure returned, it launches it, when it sees a non-closure, it returns; hence the “trampoline” wording.&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;trampoline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;callable&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;is_callable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;trampoline&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'trampoline_factorial'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;trampolines--tail-recursive-function-calls&quot;&gt;Trampolines &amp;amp; tail-recursive function calls&lt;/h3&gt;

&lt;blockquote&gt;
  &lt;p&gt;When you need a tail call optimization like structure in a language that doesn’t (necessarily) provide it, but does provide closures,
you can use a trampoline to achieve constant stack space (with a trade off for heap space for closure objects, of course).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The trampoline is a generic solution to recursion. If you can’t refactor your function to eliminate recursive calls, then transform it to a tail-call function that can be launched through a trampoline.&lt;/p&gt;

&lt;p&gt;To illustrate the idea, here is a simple example of trampoline function that can be used effectively to eliminate recursive calls. In languages like C this concept is important because the compiler is usually unable to optimize tail recursive calls, unlike languages such as Scheme.&lt;/p&gt;

&lt;p&gt;A basic, recursive way of calculating a factorial in C looks like this:&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;result is %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The problem with above piece of code is that we are using the function stack to maintain the intermediate results. Although, because the size of the integer is more of a limitation than the size of the stack, this is not really a problem here but it can be a real concern in other recursive implementations.&lt;/p&gt;

&lt;p&gt;So what can we do using a trampoline function instead? Instead of calling the function itself, we will call a &lt;em&gt;pointer&lt;/em&gt; to a function, then return a pointer to a place where we need to go next:&lt;/p&gt;

&lt;figure class=&quot;highlight code&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fact0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;total %d, x: %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
   &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;result is %d&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On this example, at each time, the stack holds only the context for the function that is being called. Since we don’t need to store return information for the last call, this can always be done in a tail recursive function.&lt;/p&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;resources&quot;&gt;Resources&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://jpauli.github.io/2016/09/16/php-7-magic-function-call-trampoline.html&quot; target=&quot;_blank&quot;&gt;PHP 7, jpauli&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076&quot; target=&quot;_blank&quot;&gt;Java, johnmcclean&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://coliveira.net/software/recursion-using-trampoline-functions/&quot; target=&quot;_blank&quot;&gt;C, coliveira&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://stackoverflow.com/a/22046731/2852427&quot; target=&quot;_blank&quot;&gt;How do I jump out of a function in lisp?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.microsoft.com/en-us/research/publication/detours-binary-interception-of-win32-functions/&quot; target=&quot;_blank&quot;&gt;Detours: Binary Interception of Win32 Functions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Wed, 07 Jun 2017 18:28:59 +0000</pubDate>
        <link>http://ekins.space/2017/06/07/trampoline/</link>
        <guid isPermaLink="true">http://ekins.space/2017/06/07/trampoline/</guid>
        
        
      </item>
    
      <item>
        <title>Notes on Iteration &amp; Recursion</title>
        <description>&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Iteration&lt;/code&gt; is the repetition of a process to produce a sequence of outcomes. Each repetition of this process is a single iteration and the outcome of every iteration is the starting point of the next iteration.&lt;/p&gt;

&lt;p&gt;In mathematics, one can use iteration to find approximate solutions to equations. Repeatedly solving an equation to obtain a result using the result from the previous calculation is referred as an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iterative process&lt;/code&gt;. Many root-finding algorithms make use of iterative methods, producing a sequence of numbers that are hopefully approximating toward the root with each iteration. Take a look at Newton’s method and Brent’s method as example root-finding algorithms.&lt;/p&gt;

&lt;p&gt;In programming we use iteration quite often. Many programming languages have loop constructs that lets you iterate over a given set or we can write iterative functions that loop to repeat some piece of code. We also have the concept of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;recursion&lt;/code&gt;, which is similar to iteration but instead of repeating some piece of code, a recursive function calls itself to repeat the code.&lt;/p&gt;

&lt;p&gt;Using a simple for loop construct to loop through all the integers from 1 to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;, multiplying as we go along, would be an iterative process. Writing down a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fn(n)&lt;/code&gt; that calls itself taking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; as the argument and returning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n * fn(n - 1)&lt;/code&gt; unless &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n = 0&lt;/code&gt;, would be a recursive process.&lt;/p&gt;

&lt;p&gt;Most CPUs model recursion with loops and a stack data structure. Each recursive call pushes a new stack frame then pops it when it returns. Recursion hence usually has the overhead of method calls (unless you’re using tail recursion and a compiler that handles it), but it usually requires less code than iteration. Infinite recursion can lead to system crash while infinite iteration would consume CPU cycles.&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;Next: &lt;a href=&quot;http://ekins.space/2017/06/07/trampoline/&quot;&gt;Function Trampoline&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h4 id=&quot;must-reads-on-this-topic&quot;&gt;Must-reads on this topic&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.moertel.com/tags/recursion.html&quot; target=&quot;_blank&quot;&gt;Tricks of the trade: Recursion to Iteration series, Tom Moertel&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.cs.cornell.edu/info/courses/spring-98/cs211/lecturenotes/07-recursion.pdf&quot; target=&quot;_blank&quot;&gt;Lecture notes on recursion and iteration, Cornell&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Juggler_sequence&quot; target=&quot;_blank&quot;&gt;Juggler Sequence, for some maths fun&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Root-finding_algorithm#Iterative_methods&quot; target=&quot;_blank&quot;&gt;Iterative methods for root-finding Algorithms&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Tue, 09 May 2017 15:35:21 +0000</pubDate>
        <link>http://ekins.space/2017/05/09/notes-on-iteration-recursion/</link>
        <guid isPermaLink="true">http://ekins.space/2017/05/09/notes-on-iteration-recursion/</guid>
        
        
      </item>
    
  </channel>
</rss>
