Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Chris Müller
crystal
Commits
45c782e8
Commit
45c782e8
authored
Nov 02, 2012
by
Chris Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lib: implement map interface based on red-black trees.
parent
63ac6d1d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
9 deletions
+101
-9
src/CMakeLists.txt
src/CMakeLists.txt
+1
-0
src/structures/map.c
src/structures/map.c
+89
-0
src/structures/map.h
src/structures/map.h
+11
-9
No files found.
src/CMakeLists.txt
View file @
45c782e8
...
...
@@ -7,6 +7,7 @@ set(SOURCES
structures/red_black_tree.c
structures/heap.c
structures/array.c
structures/map.c
structures/structures.c
)
...
...
src/structures/map.c
0 → 100644
View file @
45c782e8
/*
* Copyright (c) 2012 Christoph Mueller <ruunhb@googlemail.com>
*
* Crystal is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Crystal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "map.h"
#include "red_black_tree.h"
#include <assert.h>
struct
CryMap
*
cry_map_new
(
cry_ordering_funptr
compare
)
{
return
cry_cast
(
struct
CryMap
*
,
cry_rbtree_new
(
compare
));
}
void
cry_map_clear
(
struct
CryMap
*
map
,
cry_free_funptr
fun_key
,
cry_free_funptr
fun_data
)
{
assert
(
map
!=
0
);
cry_rbtree_clear
(
cry_cast
(
struct
CryRbTree
*
,
map
),
fun_key
,
fun_data
);
}
size_t
cry_map_size
(
struct
CryMap
*
map
)
{
assert
(
map
!=
0
);
return
cry_cast
(
struct
CryRbTree
*
,
map
)
->
nodes
;
}
pointer
cry_map_lookup
(
struct
CryMap
*
map
,
const_pointer
key
)
{
assert
(
map
!=
0
);
struct
CryRbNode
*
result
=
cry_rbtree_lookup
(
cry_cast
(
struct
CryRbTree
*
,
map
),
key
);
return
(
result
!=
0
)
?
cry_cast
(
pointer
,
result
->
data
)
:
0
;
}
int
cry_map_insert
(
struct
CryMap
*
map
,
const_pointer
key
,
const_pointer
value
)
{
assert
(
map
!=
0
);
struct
CryRbNode
*
result
=
cry_rbtree_insert
(
cry_cast
(
struct
CryRbTree
*
,
map
),
key
,
value
);
return
result
!=
0
;
}
struct
CryMapPair
cry_map_remove
(
struct
CryMap
*
map
,
const_pointer
key
)
{
assert
(
map
!=
0
);
struct
CryMapPair
pair
=
{
.
key
=
0
,
.
data
=
0
};
struct
CryRbNode
*
result
=
cry_rbtree_remove
(
cry_cast
(
struct
CryRbTree
*
,
map
),
key
);
if
(
result
)
{
pair
.
key
=
result
->
entry
;
pair
.
data
=
result
->
data
;
cry_rbnode_free
(
result
,
0
,
0
);
}
return
pair
;
}
src/structures/map.h
View file @
45c782e8
...
...
@@ -22,20 +22,22 @@
#include "structures.h"
struct
CryMap
;
struct
CryMapPair
{
const_pointer
key
;
const_pointer
data
;
};
struct
CryMap
*
cry_map_new
(
cry_ordering_funptr
compare
);
void
cry_map_free
(
struct
CryMap
*
map
,
cry_free_funptr
key_free
,
cry_free_funptr
value_free
);
struct
CryMap
;
s
ize_t
cry_map_
size
(
struct
CryMap
*
map
);
s
truct
CryMap
*
cry_map_
new
(
cry_ordering_funptr
compare
);
pointer
cry_map_lookup
(
struct
CryMap
*
map
,
const_pointer
key
);
return_code
cry_map_insert
(
struct
CryMap
*
map
,
const_pointer
key
,
pointer
value
);
pointer
cry_map_remove
(
struct
CryMap
*
map
,
const_pointer
key
,
cry_free_funptr
key_free
);
size_t
cry_map_size
(
struct
CryMap
*
map
);
pointer
cry_map_lookup
(
struct
CryMap
*
map
,
const_pointer
key
);
int
cry_map_insert
(
struct
CryMap
*
map
,
const_pointer
key
,
const_pointer
value
);
struct
CryMapPair
cry_map_remove
(
struct
CryMap
*
map
,
const_pointer
key
);
void
cry_map_clear
(
struct
CryMap
*
map
,
cry_free_funptr
fun_key
,
cry_free_funptr
fun_data
);
void
cry_map_dump_debug
(
struct
CryMap
*
map
);
#endif // CRYSTAL_STRUCTURES_MAP_H
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