rename repo, subdir for yearly challenges

This commit is contained in:
2021-07-25 11:17:46 +02:00
parent 1806f79e14
commit 4a2318edc9
203 changed files with 212 additions and 1 deletions

8
2020/day17/INPUT.txt Normal file
View File

@@ -0,0 +1,8 @@
##....#.
#.#..#..
...#....
...#.#..
###....#
#.#....#
.#....##
.#.###.#

31
2020/day17/Makefile Normal file
View File

@@ -0,0 +1,31 @@
INPUT := INPUT.txt
SHELL := /bin/bash
#CFLAGS := -w -g
#CFLAGS := -w -g -pg
CFLAGS := -w -O3
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
.PHONY: clean all compile deploy ex1 ex2
all: ex1 ex2
output:
@$(MAKE) --no-print-directory all 2>&1 > OUTPUT
compile: ex1-c ex2-c
ex1: ex1-c
@$(TIME) ex1-v1.bash < $(INPUT) 2>&1
@$(TIME) ex1.bash < $(INPUT) 2>&1
@$(TIME) ex1-c < $(INPUT) 2>&1
ex2: ex2-c
@$(TIME) ex2.bash < $(INPUT) 2>&1
@$(TIME) ex2-c < $(INPUT) 2>&1
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

20
2020/day17/OUTPUT Normal file
View File

@@ -0,0 +1,20 @@
ex1-v1.bash : res=263
time: 1:47.86 real, 74.63 user, 39.28 sys
context-switch: 6224+166221, page-faults: 0+7568225
ex1.bash : res=263
time: 0:00.37 real, 0.36 user, 0.00 sys
context-switch: 1+1, page-faults: 0+322
ex1-c : res=263
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+150
ex2.bash : res=1680
time: 0:06.47 real, 6.47 user, 0.00 sys
context-switch: 23+1, page-faults: 0+2852
ex2-c : res=1680
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+1634

407
2020/day17/README Normal file
View File

@@ -0,0 +1,407 @@
--- Day 17: Conway Cubes ---
As your flight slowly drifts through the sky, the Elves at the Mythical Information Bureau at the North Pole contact you. They'd like some help debugging a malfunctioning experimental energy source aboard one of their super-secret imaging satellites.
The experimental energy source is based on cutting-edge technology: a set of Conway Cubes contained in a pocket dimension! When you hear it's having problems, you can't help but agree to take a look.
The pocket dimension contains an infinite 3-dimensional grid. At every integer 3-dimensional coordinate (x,y,z), there exists a single cube which is either active or inactive.
In the initial state of the pocket dimension, almost all cubes start inactive. The only exception to this is a small flat region of cubes (your puzzle input); the cubes in this region start in the specified active (#) or inactive (.) state.
The energy source then proceeds to boot up by executing six cycles.
Each cube only ever considers its neighbors: any of the 26 other cubes where any of their coordinates differ by at most 1. For example, given the cube at x=1,y=2,z=3, its neighbors include the cube at x=2,y=2,z=2, the cube at x=0,y=2,z=3, and so on.
During a cycle, all cubes simultaneously change their state according to the following rules:
If a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active. Otherwise, the cube becomes inactive.
If a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active. Otherwise, the cube remains inactive.
The engineers responsible for this experimental energy source would like you to simulate the pocket dimension and determine what the configuration of cubes should be at the end of the six-cycle boot process.
For example, consider the following initial state:
.#.
..#
###
Even though the pocket dimension is 3-dimensional, this initial state represents a small 2-dimensional slice of it. (In particular, this initial state defines a 3x3x1 region of the 3-dimensional space.)
Simulating a few cycles from this initial state produces the following configurations, where the result of each cycle is shown layer-by-layer at each given z coordinate (and the frame of view follows the active cells in each cycle):
Before any cycles:
z=0
.#.
..#
###
After 1 cycle:
z=-1
#..
..#
.#.
z=0
#.#
.##
.#.
z=1
#..
..#
.#.
After 2 cycles:
z=-2
.....
.....
..#..
.....
.....
z=-1
..#..
.#..#
....#
.#...
.....
z=0
##...
##...
#....
....#
.###.
z=1
..#..
.#..#
....#
.#...
.....
z=2
.....
.....
..#..
.....
.....
After 3 cycles:
z=-2
.......
.......
..##...
..###..
.......
.......
.......
z=-1
..#....
...#...
#......
.....##
.#...#.
..#.#..
...#...
z=0
...#...
.......
#......
.......
.....##
.##.#..
...#...
z=1
..#....
...#...
#......
.....##
.#...#.
..#.#..
...#...
z=2
.......
.......
..##...
..###..
.......
.......
.......
After the full six-cycle boot process completes, 112 cubes are left in the active state.
Starting with your given initial configuration, simulate six cycles. How many cubes are left in the active state after the sixth cycle?
Your puzzle answer was 263.
--- Part Two ---
For some reason, your simulated results don't match what the experimental energy source engineers expected. Apparently, the pocket dimension actually has four spatial dimensions, not three.
The pocket dimension contains an infinite 4-dimensional grid. At every integer 4-dimensional coordinate (x,y,z,w), there exists a single cube (really, a hypercube) which is still either active or inactive.
Each cube only ever considers its neighbors: any of the 80 other cubes where any of their coordinates differ by at most 1. For example, given the cube at x=1,y=2,z=3,w=4, its neighbors include the cube at x=2,y=2,z=3,w=3, the cube at x=0,y=2,z=3,w=4, and so on.
The initial state of the pocket dimension still consists of a small flat region of cubes. Furthermore, the same rules for cycle updating still apply: during each cycle, consider the number of active neighbors of each cube.
For example, consider the same initial state as in the example above. Even though the pocket dimension is 4-dimensional, this initial state represents a small 2-dimensional slice of it. (In particular, this initial state defines a 3x3x1x1 region of the 4-dimensional space.)
Simulating a few cycles from this initial state produces the following configurations, where the result of each cycle is shown layer-by-layer at each given z and w coordinate:
Before any cycles:
z=0, w=0
.#.
..#
###
After 1 cycle:
z=-1, w=-1
#..
..#
.#.
z=0, w=-1
#..
..#
.#.
z=1, w=-1
#..
..#
.#.
z=-1, w=0
#..
..#
.#.
z=0, w=0
#.#
.##
.#.
z=1, w=0
#..
..#
.#.
z=-1, w=1
#..
..#
.#.
z=0, w=1
#..
..#
.#.
z=1, w=1
#..
..#
.#.
After 2 cycles:
z=-2, w=-2
.....
.....
..#..
.....
.....
z=-1, w=-2
.....
.....
.....
.....
.....
z=0, w=-2
###..
##.##
#...#
.#..#
.###.
z=1, w=-2
.....
.....
.....
.....
.....
z=2, w=-2
.....
.....
..#..
.....
.....
z=-2, w=-1
.....
.....
.....
.....
.....
z=-1, w=-1
.....
.....
.....
.....
.....
z=0, w=-1
.....
.....
.....
.....
.....
z=1, w=-1
.....
.....
.....
.....
.....
z=2, w=-1
.....
.....
.....
.....
.....
z=-2, w=0
###..
##.##
#...#
.#..#
.###.
z=-1, w=0
.....
.....
.....
.....
.....
z=0, w=0
.....
.....
.....
.....
.....
z=1, w=0
.....
.....
.....
.....
.....
z=2, w=0
###..
##.##
#...#
.#..#
.###.
z=-2, w=1
.....
.....
.....
.....
.....
z=-1, w=1
.....
.....
.....
.....
.....
z=0, w=1
.....
.....
.....
.....
.....
z=1, w=1
.....
.....
.....
.....
.....
z=2, w=1
.....
.....
.....
.....
.....
z=-2, w=2
.....
.....
..#..
.....
.....
z=-1, w=2
.....
.....
.....
.....
.....
z=0, w=2
###..
##.##
#...#
.#..#
.###.
z=1, w=2
.....
.....
.....
.....
.....
z=2, w=2
.....
.....
..#..
.....
.....
After the full six-cycle boot process completes, 848 cubes are left in the active state.
Starting with your given initial configuration, simulate six cycles in a 4-dimensional space. How many cubes are left in the active state after the sixth cycle?
Your puzzle answer was 1680.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

3
2020/day17/TEST.txt Normal file
View File

@@ -0,0 +1,3 @@
.#.
..#
###

165
2020/day17/ex1-c.c Normal file
View File

@@ -0,0 +1,165 @@
/* ex1-c: Advent2020 game, day 17/game 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "list.h"
#define LOOPS 6
#define MAXINIT 8
#define SIZE ((2*LOOPS)+MAXINIT)
#define ZERO (SIZE/2)
#define ACTIVE '#'
#define INACTIVE '.'
typedef struct cell {
char state;
char oldstate;
bool visited; /* redundant ? */
unsigned count; /* active neighbors */
// for runs, we don't care unused cells
struct list_head set; /* current actives */
struct list_head viewed; /* current visited */
} CELL;
LIST_HEAD(qset);
LIST_HEAD(qviewed);
static CELL cube[SIZE][SIZE][SIZE];
#define CUBE2X(cell) (((cell)-&cube[0][0][0])/SIZE/SIZE)
#define CUBE2Y(cell) (((cell)-&cube[0][0][0])/SIZE%SIZE)
#define CUBE2Z(cell) (((cell)-&cube[0][0][0])/(SIZE/SIZE)%SIZE)
#define HEADRESET(elt) { \
(elt)->next=POISON_POINTER1;(elt)->prev=POISON_POINTER1; }
static void reset_cell(CELL *pcell)
{
pcell->state=INACTIVE;
pcell->oldstate=INACTIVE;
pcell->visited=false;
pcell->count=0;
}
static void reset_cube()
{
int i, j, k;
for (i=0; i<SIZE; ++i)
for (j=0; j<SIZE; ++j)
for (k=0; k<SIZE; ++k)
reset_cell(&cube[i][j][k]);
}
static int count_active()
{
struct list_head *p;
int i=0;
list_for_each(p, &qset) {
++i;
}
return i;
}
static void init_row(char *line, int row)
{
int i, x, y, z;
static int ncols=0;
CELL *pcell;
if (ncols == 0)
ncols=strlen(line)-1;
y=ZERO-ncols/2+row;
z=ZERO;
for (i=0; *line; ++i, ++line) {
x=ZERO+i-ncols/2;
pcell=&cube[x][y][z];
if (*line == '#') {
list_add(&pcell->set, &qset);
list_add(&pcell->viewed, &qviewed);
pcell->state=ACTIVE;
pcell->visited=1;
}
}
}
void run_life()
{
struct cell *pcell, *ptmp;
int x, y, z, x1, y1, z1;
/* 1) count +1 for neighbors */
list_for_each_entry(pcell, &qset, set) {
x=CUBE2X(pcell);
y=CUBE2Y(pcell);
z=CUBE2Z(pcell);
for (x1=x-1; x1<=x+1; x1++) {
for (y1=y-1; y1<=y+1; y1++) {
for (z1=z-1; z1<=z+1; z1++) {
if ((x1!=x || y1!=y || z1!=z)) {
ptmp=&cube[x1][y1][z1];
++ptmp->count;
if (!ptmp->visited) {
list_add(&ptmp->viewed, &qviewed);
ptmp->visited=1;
}
}
}
}
}
}
list_for_each_entry_safe(pcell, ptmp, &qviewed, viewed) {
switch (pcell->state) {
case ACTIVE:
if (pcell->count != 2 && pcell->count != 3) {
list_del(&pcell->set);
pcell->state=INACTIVE;
pcell->visited=0;
}
break;
case INACTIVE:
if (pcell->count == 3) {
list_add(&pcell->set, &qset);
pcell->state=ACTIVE;
pcell->visited=1;
}
break;
}
pcell->count=0;
if (pcell->state == INACTIVE) {
pcell->visited=0;
list_del(&pcell->viewed);
}
}
}
int main(ac, av)
int ac;
char **av;
{
char line[16];
int nline=0;
reset_cube();
INIT_LIST_HEAD(&qset);
INIT_LIST_HEAD(&qviewed);
while (fgets(line, sizeof line, stdin)) {
init_row(line, nline);
nline++;
}
for (int i=0; i<LOOPS; ++i)
run_life();
printf("%s : res=%d\n", *av, count_active());
exit (0);
}

87
2020/day17/ex1-v1.bash Executable file
View File

@@ -0,0 +1,87 @@
#!/bin/bash
#
# ex1-v1.bash: Advent2020 game, day 17/game 1.
# too slow to adapt for task 2. See ex1.bash.
CMD=${0##*/}
#shopt -s extglob
declare -A life=()
declare -i x=-1 y=-1 z=-1 res=0
declare -i maxx maxy maxz
function count_neighbors () {
local -i x=$1 y=$2 z=$3 x1 y1 z1 count=0
local str=""
for ((x1=x-1; x1<x+2; ++x1)); do
for ((y1=y-1; y1<y+2; ++y1)); do
for ((z1=z-1; z1<z+2; ++z1)); do
if ((x1!=x || y1!=y || z1!=z)); then
if [[ ${life[$x1-$y1-$z1]} == "#" ]]; then
((count++))
str="$str $x1-$y1-$z1"
fi
fi
done
done
done
echo "$count"
}
function run_cycle () {
local -i x y z count=0
local -A lifetmp=()
for ((x=0; x<maxx; ++x)); do
for ((y=0; y<maxy; ++y)); do
for ((z=0; z<maxz; ++z)); do
count=$(count_neighbors "$x" "$y" "$z")
if [[ ${life[$x-$y-$z]} == "#" ]]; then
((count!=2 && count!=3)) && lifetmp[$x-$y-$z]="."
else
((count==3)) && lifetmp[$x-$y-$z]="#"
fi
done
done
done
for k in "${!lifetmp[@]}"; do
life[$k]=${lifetmp[$k]}
done
}
function count_active () {
local k
local -i count=0
for k in "${!life[@]}"; do
if [[ ${life[$k]} == "#" ]]; then
((count++))
fi
done
echo "$count"
}
x=6; y=6; z=6
while read -r line; do
if ((y==6)); then # 1st line
((maxx=6+${#line}+6+1))
((maxy=maxx))
((maxz=maxx))
fi
for ((j=0; j<${#line}; ++j)); do
((curx=x+j))
life[$curx-$y-$z]=${line:j:1}
done
((y++))
done
for ((i=0; i<6; ++i)); do
run_cycle
done
res=$(count_active)
printf "%s : res=%d\n" "$CMD" "$res"
exit 0

71
2020/day17/ex1.bash Executable file
View File

@@ -0,0 +1,71 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 17/game 1.
CMD=${0##*/}
#shopt -s extglob
declare -A life=()
declare -i x=-1 y=-1 z=-1 res=0
declare -i loops=6
function run_cycle () {
local -i x y z count=0 x1 y1 z1 v
local -A lifetmp=()
local -a values
for k in "${!life[@]}"; do
# shellcheck disable=SC2206
values=(${k//-/ })
x=${values[0]}
y=${values[1]}
z=${values[2]}
for ((x1=x-1; x1<x+2; ++x1)); do
for ((y1=y-1; y1<y+2; ++y1)); do
for ((z1=z-1; z1<z+2; ++z1)); do
if ((x1!=x || y1!=y || z1!=z)); then
((++lifetmp[$x1-$y1-$z1]))
fi
done
done
done
done
# clean elements with no neighbors
for k in "${!life[@]}"; do
if [[ ! -v lifetmp[$k] ]]; then
unset "life[$k]"
fi
done
for k in "${!lifetmp[@]}"; do
v=${lifetmp[$k]}
if [[ -v life[$k] ]]; then
(( v!=2 && v!=3 )) && unset "life[$k]"
else
(( v==3 )) && life[$k]="#"
fi
done
}
x=$loops; y=$loops; z=$loops
while read -r line; do
for ((j=0; j<${#line}; ++j)); do
((curx=x+j))
c=${line:j:1}
if [[ $c == "#" ]]; then
life[$curx-$y-$z]=$c
fi
done
((y++))
done
for ((i=0; i<6; ++i)); do
run_cycle
done
res=${#life[@]}
printf "%s : res=%d\n" "$CMD" "$res"
exit 0

170
2020/day17/ex2-c.c Normal file
View File

@@ -0,0 +1,170 @@
/* ex1-c: Advent2020 game, day 17/game 2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "list.h"
#define LOOPS 6
#define MAXINIT 8
#define SIZE ((2*LOOPS)+MAXINIT)
#define ZERO (SIZE/2)
#define ACTIVE '#'
#define INACTIVE '.'
typedef struct cell {
char state;
char oldstate;
bool visited; /* redundant ? */
unsigned count; /* active neighbors */
// for runs, we don't care unused cells
struct list_head set; /* current actives */
struct list_head viewed; /* current visited */
} CELL;
LIST_HEAD(qset);
LIST_HEAD(qviewed);
static CELL cube[SIZE][SIZE][SIZE][SIZE];
#define CUBEZERO cube[0][0][0][0]
#define CUBE2X(cell) (((cell)-&CUBEZERO)/SIZE/SIZE/SIZE)
#define CUBE2Y(cell) (((cell)-&CUBEZERO)/SIZE/SIZE%SIZE)
#define CUBE2Z(cell) (((cell)-&CUBEZERO)/SIZE%SIZE)
#define CUBE2W(cell) (((cell)-&CUBEZERO)%SIZE)
#define HEADRESET(elt) { \
(elt)->next=POISON_POINTER1;(elt)->prev=POISON_POINTER1; }
static void reset_cell(CELL *pcell)
{
pcell->state=INACTIVE;
pcell->oldstate=INACTIVE;
pcell->visited=false;
pcell->count=0;
}
static void reset_cube()
{
int i, j, k, l;
for (i=0; i<SIZE; ++i)
for (j=0; j<SIZE; ++j)
for (k=0; k<SIZE; ++k)
for (l=0; l<SIZE; ++l)
reset_cell(&cube[i][j][k][l]);
}
static int count_active()
{
struct list_head *p;
int i=0;
list_for_each(p, &qset) {
++i;
}
return i;
}
static void init_row(char *line, int row)
{
int i, x, y, z, w;
static int ncols=0;
CELL *pcell;
if (ncols == 0)
ncols=strlen(line)-1;
y=ZERO-ncols/2+row;
z=ZERO;
w=ZERO;
for (i=0; *line; ++i, ++line) {
x=ZERO+i-ncols/2;
pcell=&cube[x][y][z][w];
if (*line == '#') {
list_add(&pcell->set, &qset);
list_add(&pcell->viewed, &qviewed);
pcell->state=ACTIVE;
pcell->visited=1;
}
}
}
void run_life()
{
struct cell *pcell, *ptmp;
int x, y, z, w, x1, y1, z1, w1;
/* 1) count +1 for neighbors */
list_for_each_entry(pcell, &qset, set) {
x=CUBE2X(pcell);
y=CUBE2Y(pcell);
z=CUBE2Z(pcell);
w=CUBE2W(pcell);
for (x1=x-1; x1<=x+1; x1++) {
for (y1=y-1; y1<=y+1; y1++) {
for (z1=z-1; z1<=z+1; z1++) {
for (w1=w-1; w1<=w+1; w1++) {
if ((x1!=x || y1!=y || z1!=z || w1!=w)) {
ptmp=&cube[x1][y1][z1][w1];
++ptmp->count;
if (!ptmp->visited) {
list_add(&ptmp->viewed, &qviewed);
ptmp->visited=1;
}
}
}
}
}
}
}
list_for_each_entry_safe(pcell, ptmp, &qviewed, viewed) {
switch (pcell->state) {
case ACTIVE:
if (pcell->count != 2 && pcell->count != 3) {
list_del(&pcell->set);
pcell->state=INACTIVE;
pcell->visited=0;
}
break;
case INACTIVE:
if (pcell->count == 3) {
list_add(&pcell->set, &qset);
pcell->state=ACTIVE;
pcell->visited=1;
}
break;
}
pcell->count=0;
if (pcell->state == INACTIVE) {
pcell->visited=0;
list_del(&pcell->viewed);
}
}
}
int main(ac, av)
int ac;
char **av;
{
char line[16];
int nline=0;
reset_cube();
INIT_LIST_HEAD(&qset);
INIT_LIST_HEAD(&qviewed);
while (fgets(line, sizeof line, stdin)) {
init_row(line, nline);
nline++;
}
for (int i=0; i<LOOPS; ++i)
run_life();
printf("%s : res=%d\n", *av, count_active());
exit (0);
}

74
2020/day17/ex2.bash Executable file
View File

@@ -0,0 +1,74 @@
#!/bin/bash
#
# ex2.bash: Advent2020 game, day 17/game 2.
CMD=${0##*/}
#shopt -s extglob
declare -A life=()
declare -i x=-1 y=-1 z=-1 w=-1 res=0
declare -i loops=6
function run_cycle () {
local -i x y z w x1 y1 z1 w1 v
local -A lifetmp=()
local -a values
for k in "${!life[@]}"; do
# shellcheck disable=SC2206
values=(${k//-/ })
x=${values[0]}
y=${values[1]}
z=${values[2]}
w=${values[3]}
for ((x1=x-1; x1<x+2; ++x1)); do
for ((y1=y-1; y1<y+2; ++y1)); do
for ((z1=z-1; z1<z+2; ++z1)); do
for ((w1=w-1; w1<w+2; ++w1)); do
if ((x1!=x || y1!=y || z1!=z || w1!=w)); then
((++lifetmp[$x1-$y1-$z1-$w1]))
fi
done
done
done
done
done
# clean elements with no neighbors
for k in "${!life[@]}"; do
if [[ ! -v lifetmp[$k] ]]; then
unset "life[$k]"
fi
done
for k in "${!lifetmp[@]}"; do
v=${lifetmp[$k]}
if [[ -v life[$k] ]]; then
(( v!=2 && v!=3 )) && unset "life[$k]"
else
(( v==3 )) && life[$k]="#"
fi
done
}
x=$loops; y=$loops; z=$loops; w=$loops
while read -r line; do
for ((j=0; j<${#line}; ++j)); do
((curx=x+j))
c=${line:j:1}
if [[ $c == "#" ]]; then
life[$curx-$y-$z-$w]=$c
fi
done
((y++))
done
for ((i=0; i<6; ++i)); do
run_cycle
done
res=${#life[@]}
printf "%s : res=%d\n" "$CMD" "$res"
exit 0

165
2020/day17/list.h Normal file
View File

@@ -0,0 +1,165 @@
/* SPDX-License-Identifier: GPL-2.0 */
/* circular list management.
*
* inspired from kernel's <linux/lists.h>
*/
#ifndef _BR_LIST_H
#define _BR_LIST_H
#include <stddef.h>
#define POISON_POINTER1 ((void *) 0x1)
#define POISON_POINTER2 ((void *) 0x2)
struct list_head {
struct list_head *next, *prev;
};
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member) * __mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
#define LIST_HEAD(name) \
struct list_head name = { &(name), &(name) }
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
static inline void __list_join(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
static inline void __list_del_entry(struct list_head *entry)
{
__list_join(entry->prev, entry->next);
}
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
static inline void list_del(struct list_head *entry)
{
__list_del_entry(entry);
entry->next = POISON_POINTER1;
entry->prev = POISON_POINTER1;
}
static inline void list_replace(struct list_head *old,
struct list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}
static inline void list_swap(struct list_head *entry1,
struct list_head *entry2)
{
struct list_head *pos = entry2->prev;
list_del(entry2);
list_replace(entry1, entry2);
if (pos == entry1)
pos = entry2;
list_add(entry1, pos);
}
static inline int list_is_first(const struct list_head *list,
const struct list_head *head)
{
return list->prev == head;
}
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
#define list_first_entry_or_null(ptr, type, member) ({ \
struct list_head *head__ = (ptr); \
struct list_head *pos__ = READ_ONCE(head__->next); \
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define list_for_each_continue(pos, head) \
for (pos = pos->next; pos != (head); pos = pos->next)
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)
#define list_entry_is_head(pos, head, member) \
(&pos->member == (head))
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_next_entry(pos, member))
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member), \
n = list_next_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = n, n = list_next_entry(n, member))
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
!list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_prev_entry(pos, member); \
!list_entry_is_head(pos, head, member); \
pos = list_prev_entry(pos, member))
#endif /* _BR_LIST_H */