Git fork
1/**
2 * Copyright 2013, GitHub, Inc
3 * Copyright 2009-2013, Daniel Lemire, Cliff Moon,
4 * David McIntosh, Robert Becho, Google Inc. and Veronika Zenz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#define DISABLE_SIGN_COMPARE_WARNINGS
21
22#include "git-compat-util.h"
23#include "ewok.h"
24#include "ewok_rlw.h"
25
26static inline int next_word(struct rlw_iterator *it)
27{
28 if (it->pointer >= it->size)
29 return 0;
30
31 it->rlw.word = &it->buffer[it->pointer];
32 it->pointer += rlw_get_literal_words(it->rlw.word) + 1;
33
34 it->rlw.literal_words = rlw_get_literal_words(it->rlw.word);
35 it->rlw.running_len = rlw_get_running_len(it->rlw.word);
36 it->rlw.running_bit = rlw_get_run_bit(it->rlw.word);
37 it->rlw.literal_word_offset = 0;
38
39 return 1;
40}
41
42void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *from_ewah)
43{
44 it->buffer = from_ewah->buffer;
45 it->size = from_ewah->buffer_size;
46 it->pointer = 0;
47
48 next_word(it);
49
50 it->literal_word_start = rlwit_literal_words(it) +
51 it->rlw.literal_word_offset;
52}
53
54void rlwit_discard_first_words(struct rlw_iterator *it, size_t x)
55{
56 while (x > 0) {
57 size_t discard;
58
59 if (it->rlw.running_len > x) {
60 it->rlw.running_len -= x;
61 return;
62 }
63
64 x -= it->rlw.running_len;
65 it->rlw.running_len = 0;
66
67 discard = (x > it->rlw.literal_words) ? it->rlw.literal_words : x;
68
69 it->literal_word_start += discard;
70 it->rlw.literal_words -= discard;
71 x -= discard;
72
73 if (x > 0 || rlwit_word_size(it) == 0) {
74 if (!next_word(it))
75 break;
76
77 it->literal_word_start =
78 rlwit_literal_words(it) + it->rlw.literal_word_offset;
79 }
80 }
81}
82
83size_t rlwit_discharge(
84 struct rlw_iterator *it, struct ewah_bitmap *out, size_t max, int negate)
85{
86 size_t index = 0;
87
88 while (index < max && rlwit_word_size(it) > 0) {
89 size_t pd, pl = it->rlw.running_len;
90
91 if (index + pl > max)
92 pl = max - index;
93
94 ewah_add_empty_words(out, it->rlw.running_bit ^ negate, pl);
95 index += pl;
96
97 pd = it->rlw.literal_words;
98 if (pd + index > max)
99 pd = max - index;
100
101 ewah_add_dirty_words(out,
102 it->buffer + it->literal_word_start, pd, negate);
103
104 rlwit_discard_first_words(it, pd + pl);
105 index += pd;
106 }
107
108 return index;
109}