diff options
Diffstat (limited to 'src/grail-bits.c')
| -rw-r--r-- | src/grail-bits.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/grail-bits.c b/src/grail-bits.c new file mode 100644 index 0000000..1b38d58 --- /dev/null +++ b/src/grail-bits.c | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | /***************************************************************************** | ||
| 2 | * | ||
| 3 | * grail - Gesture Recognition And Instantiation Library | ||
| 4 | * | ||
| 5 | * Copyright (C) 2010 Canonical Ltd. | ||
| 6 | * | ||
| 7 | * This program is free software: you can redistribute it and/or modify it | ||
| 8 | * under the terms of the GNU General Public License as published by the | ||
| 9 | * Free Software Foundation, either version 3 of the License, or (at your | ||
| 10 | * option) any later version. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, but | ||
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License along | ||
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | * | ||
| 20 | * Authors: | ||
| 21 | * Henrik Rydberg <rydberg@bitmath.org> | ||
| 22 | * | ||
| 23 | ****************************************************************************/ | ||
| 24 | |||
| 25 | #include "grail-bits.h" | ||
| 26 | |||
| 27 | static const int grail_bits_in_byte[256] = | ||
| 28 | { | ||
| 29 | 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, | ||
| 30 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | ||
| 31 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | ||
| 32 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | ||
| 33 | 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, | ||
| 34 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | ||
| 35 | 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, | ||
| 36 | 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8, | ||
| 37 | }; | ||
| 38 | |||
| 39 | static const int grail_first_set_bit[256] = | ||
| 40 | { | ||
| 41 | -1,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 42 | 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 43 | 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 44 | 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 45 | 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 46 | 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 47 | 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 48 | 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0, | ||
| 49 | }; | ||
| 50 | |||
| 51 | void grail_mask_set_mask(grail_mask_t *a, const grail_mask_t *b, int bytes) | ||
| 52 | { | ||
| 53 | while (bytes--) | ||
| 54 | *a++ |= *b++; | ||
| 55 | } | ||
| 56 | |||
| 57 | void grail_mask_clear_mask(grail_mask_t *a, const grail_mask_t *b, int bytes) | ||
| 58 | { | ||
| 59 | while (bytes--) | ||
| 60 | *a++ &= ~*b++; | ||
| 61 | } | ||
| 62 | |||
| 63 | int grail_mask_count(const grail_mask_t *mask, int bytes) | ||
| 64 | { | ||
| 65 | int count = 0; | ||
| 66 | while (bytes--) | ||
| 67 | count += grail_bits_in_byte[*mask++]; | ||
| 68 | return count; | ||
| 69 | } | ||
| 70 | |||
| 71 | int grail_mask_get_first(const grail_mask_t *mask, int bytes) | ||
| 72 | { | ||
| 73 | int k; | ||
| 74 | for (k = 0; k < bytes; k++) | ||
| 75 | if (mask[k]) | ||
| 76 | return (k << 3) | grail_first_set_bit[mask[k]]; | ||
| 77 | return -1; | ||
| 78 | } | ||
| 79 | |||
| 80 | int grail_mask_get_next(int i, const grail_mask_t *mask, int bytes) | ||
| 81 | { | ||
| 82 | int k = ++i >> 3; | ||
| 83 | if (k < bytes) { | ||
| 84 | i = grail_first_set_bit[mask[k] & (~0 << (i & 7))]; | ||
| 85 | if (i >= 0) | ||
| 86 | return (k << 3) | i; | ||
| 87 | while (++k < bytes) { | ||
| 88 | i = grail_first_set_bit[mask[k]]; | ||
| 89 | if (i >= 0) | ||
| 90 | return (k << 3) | i; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return -1; | ||
| 94 | } | ||
