Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
406dd556
Commit
406dd556
authored
Jul 10, 2013
by
Vicent Marti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bitvec: Simplify the bit vector code
parent
2b672d5b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
39 deletions
+22
-39
src/bitvec.h
+22
-39
No files found.
src/bitvec.h
View file @
406dd556
...
@@ -19,58 +19,43 @@
...
@@ -19,58 +19,43 @@
typedef
struct
{
typedef
struct
{
size_t
length
;
size_t
length
;
union
{
union
{
uint
8_t
*
ptr
;
uint
64_t
*
words
;
uint64_t
bits
;
uint64_t
bits
;
}
u
;
}
u
;
}
git_bitvec
;
}
git_bitvec
;
GIT_INLINE
(
int
)
git_bitvec_init
(
git_bitvec
*
bv
,
size_t
capacity
)
GIT_INLINE
(
int
)
git_bitvec_init
(
git_bitvec
*
bv
,
size_t
capacity
)
{
{
if
(
capacity
<
64
)
{
memset
(
bv
,
0x0
,
sizeof
(
*
bv
));
bv
->
length
=
0
;
bv
->
u
.
bits
=
0
;
if
(
capacity
>=
64
)
{
return
0
;
bv
->
length
=
(
capacity
/
64
)
+
1
;
bv
->
u
.
words
=
git__calloc
(
bv
->
length
,
sizeof
(
uint64_t
));
if
(
!
bv
->
u
.
words
)
return
-
1
;
}
}
bv
->
length
=
(
capacity
+
7
)
/
8
;
return
0
;
bv
->
u
.
ptr
=
git__calloc
(
bv
->
length
,
1
);
return
bv
->
u
.
ptr
?
0
:
-
1
;
}
}
#define GIT_BITVEC_MASK_INLINE(BIT) (((uint64_t)1) << BIT)
#define GIT_BITVEC_MASK(BIT) ((uint64_t)1 << (BIT % 64))
#define GIT_BITVEC_WORD(BV, BIT) (BV->length ? &BV->u.words[BIT / 64] : &BV->u.bits)
#define GIT_BITVEC_MASK_BYTE(BIT) (((uint8_t)1) << ((BIT) & 0x07))
#define GIT_BITVEC_INDEX_BYTE(BIT) ((BIT) >> 3)
GIT_INLINE
(
void
)
git_bitvec_set
(
git_bitvec
*
bv
,
size_t
bit
,
bool
on
)
GIT_INLINE
(
void
)
git_bitvec_set
(
git_bitvec
*
bv
,
size_t
bit
,
bool
on
)
{
{
if
(
!
bv
->
length
)
{
uint64_t
*
word
=
GIT_BITVEC_WORD
(
bv
,
bit
);
assert
(
bit
<
64
);
uint64_t
mask
=
GIT_BITVEC_MASK
(
bit
);
if
(
on
)
bv
->
u
.
bits
|=
GIT_BITVEC_MASK_INLINE
(
bit
);
else
bv
->
u
.
bits
&=
~
GIT_BITVEC_MASK_INLINE
(
bit
);
}
else
{
assert
(
bit
<
bv
->
length
*
8
);
if
(
on
)
if
(
on
)
bv
->
u
.
ptr
[
GIT_BITVEC_INDEX_BYTE
(
bit
)]
|=
GIT_BITVEC_MASK_BYTE
(
bit
);
*
word
|=
mask
;
else
else
bv
->
u
.
ptr
[
GIT_BITVEC_INDEX_BYTE
(
bit
)]
&=
~
GIT_BITVEC_MASK_BYTE
(
bit
);
*
word
&=
~
mask
;
}
}
}
GIT_INLINE
(
bool
)
git_bitvec_get
(
git_bitvec
*
bv
,
size_t
bit
)
GIT_INLINE
(
bool
)
git_bitvec_get
(
git_bitvec
*
bv
,
size_t
bit
)
{
{
if
(
!
bv
->
length
)
{
uint64_t
*
word
=
GIT_BITVEC_WORD
(
bv
,
bit
);
assert
(
bit
<
64
);
return
(
*
word
&
GIT_BITVEC_MASK
(
bit
))
!=
0
;
return
(
bv
->
u
.
bits
&
GIT_BITVEC_MASK_INLINE
(
bit
))
!=
0
;
}
else
{
assert
(
bit
<
bv
->
length
*
8
);
return
(
bv
->
u
.
ptr
[
GIT_BITVEC_INDEX_BYTE
(
bit
)]
&
GIT_BITVEC_MASK_BYTE
(
bit
))
!=
0
;
}
}
}
GIT_INLINE
(
void
)
git_bitvec_clear
(
git_bitvec
*
bv
)
GIT_INLINE
(
void
)
git_bitvec_clear
(
git_bitvec
*
bv
)
...
@@ -78,15 +63,13 @@ GIT_INLINE(void) git_bitvec_clear(git_bitvec *bv)
...
@@ -78,15 +63,13 @@ GIT_INLINE(void) git_bitvec_clear(git_bitvec *bv)
if
(
!
bv
->
length
)
if
(
!
bv
->
length
)
bv
->
u
.
bits
=
0
;
bv
->
u
.
bits
=
0
;
else
else
memset
(
bv
->
u
.
ptr
,
0
,
bv
->
length
);
memset
(
bv
->
u
.
words
,
0x0
,
bv
->
length
*
sizeof
(
uint64_t
)
);
}
}
GIT_INLINE
(
void
)
git_bitvec_free
(
git_bitvec
*
bv
)
GIT_INLINE
(
void
)
git_bitvec_free
(
git_bitvec
*
bv
)
{
{
if
(
bv
->
length
)
{
if
(
bv
->
length
)
git__free
(
bv
->
u
.
ptr
);
git__free
(
bv
->
u
.
words
);
memset
(
bv
,
0
,
sizeof
(
*
bv
));
}
}
}
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment