Ticket #1102: z-bitflag.h

File z-bitflag.h, 3.3 KB (added by MarbleDice, 9 years ago)

Preliminary bitflag header file.

Line 
1/*
2 * File: z-bitflag.h
3 * Purpose: Low-level bit vector manipulation
4 *
5 * Copyright (c) 2009 Andrew Sidwell
6 *
7 * This work is free software; you can redistribute it and/or modify it
8 * under the terms of either:
9 *
10 * a) the GNU General Public License as published by the Free Software
11 *    Foundation, version 2, or
12 *
13 * b) the "Angband licence":
14 *    This software may be copied and distributed for educational, research,
15 *    and not for profit purposes provided that this copyright and statement
16 *    are included in all such copies.  Other copyrights may also apply.
17 */
18
19#ifndef INCLUDED_Z_BITFLAG_H
20#define INCLUDED_Z_BITFLAG_H
21
22#include "h-basic.h"
23#include "z-virt.h"
24
25/* The basic datatype of bitfields */
26typedef byte bitfield;
27#define BITFIELD_WIDTH (sizeof(bitfield)*8)
28
29/* Enum flag value of the first valid flag in a set */
30#define FLAG_START        1
31
32/* Sentinel value indicates no more flags present for va-arg functions */
33#define FLAG_END          (FLAG_START - 1)
34
35/* The array size necessary to hold "n" flags */
36#define FIELD_SIZE(n)     ((((n) - FLAG_START) + BITFIELD_WIDTH - 1) / BITFIELD_WIDTH)
37
38/* The maximum plus one number of flags contained in an array of size "n" */
39#define MAX_FLAGS(n)      (int)((n) * BITFIELD_WIDTH + FLAG_START + 1)
40
41/* Convert a sequential flag enum value to its array index */
42#define FLAG_OFFSET(id)   (((id) - FLAG_START) / BITFIELD_WIDTH)
43
44/* Convert a sequential flag enum value to its binary flag value. */
45#define FLAG_BINARY(id)   (1 << ((id) - FLAG_START) % BITFIELD_WIDTH)
46
47
48bool field_has       (const size_t size, const bitfield *field, const int flag);
49int  field_next      (const size_t size, const bitfield *field, const int flag);
50bool field_is_empty  (const size_t size, const bitfield *field);
51bool field_is_full   (const size_t size, const bitfield *field);
52bool field_is_inter  (const size_t size, const bitfield *field1, const bitfield *field2);
53bool field_is_subset (const size_t size, const bitfield *field1, const bitfield *field2);
54bool field_is_equal  (const size_t size, const bitfield *field1, const bitfield *field2);
55bool field_on        (const size_t size, bitfield *field, const int flag);
56bool field_off       (const size_t size, bitfield *field, const int flag);
57void field_wipe      (const size_t size, bitfield *field);
58void field_setall    (const size_t size, bitfield *field);
59void field_negate    (const size_t size, bitfield *field);
60void field_copy      (const size_t size, bitfield *field1, const bitfield *field2);
61bool field_union     (const size_t size, bitfield *field1, const bitfield *field2);
62bool field_comp_union(const size_t size, bitfield *field1, const bitfield *field2);
63bool field_inter     (const size_t size, bitfield *field1, const bitfield *field2);
64bool field_diff      (const size_t size, bitfield *field1, const bitfield *field2);
65
66bool field_test_flags    (const size_t size, const bitfield *field, ...);
67bool field_test_all_flags(const size_t size, const bitfield *field, ...);
68bool field_clear_flags   (const size_t size, bitfield *field, ...);
69bool field_set_flags     (const size_t size, bitfield *field, ...);
70void field_init_flags    (const size_t size, bitfield *field, ...);
71bool field_mask_flags    (const size_t size, bitfield *field, ...);
72
73#endif