conyard 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +0 -1
- data/structure-kits/vector/vector.c.erb +103 -0
- data/structure-kits/vector/vector.h.erb +53 -0
- data/structure-kits/vector/vector.rake +24 -0
- metadata +5 -4
- data/tests/vector/vector_test.c +0 -18
- data/tests/vector/vector_test.rake +0 -11
data/Rakefile
CHANGED
@@ -0,0 +1,103 @@
|
|
1
|
+
//! Created by Chris Miller on 24 December 2009.
|
2
|
+
//! Copyright 2009-2011 Chris Miller. All rights reserved.
|
3
|
+
|
4
|
+
//! This file is generated; modifying it is totally OK, but
|
5
|
+
//! your modifications might get removed if this file is
|
6
|
+
//! regenerated again. I would suggest putting re-implementations
|
7
|
+
//! and/or extensions in another file.
|
8
|
+
|
9
|
+
#include "<%= @outfile.pathmap("%f") %>.gen.h"
|
10
|
+
|
11
|
+
#ifdef __cplusplus
|
12
|
+
extern "C" {
|
13
|
+
#endif
|
14
|
+
|
15
|
+
float __<%= @name %>_growth = 2.0f;
|
16
|
+
|
17
|
+
struct <%= @name %>* <%= @name %>_create(const register size_t capacity) {
|
18
|
+
struct <%= @name %>* v = (struct <%= @name %>*)malloc(sizeof(struct <%= @name %>));
|
19
|
+
v->data = (<%= @type %>*)malloc(sizeof(<%= @type %>)*capacity);
|
20
|
+
v->length = 0;
|
21
|
+
v->capacity = capacity;
|
22
|
+
return v;
|
23
|
+
}
|
24
|
+
void <%= @name %>_free(struct <%= @name %>* v) {
|
25
|
+
free(v->data);
|
26
|
+
free(v);
|
27
|
+
}
|
28
|
+
void <%= @name %>_free_with_block(struct <%= @name %>* v, void(^block)(<%= @type %>)) {
|
29
|
+
for (size_t i=0; i<v->length; ++i)
|
30
|
+
block(v->data[i]);
|
31
|
+
free(v->data);
|
32
|
+
free(v);
|
33
|
+
}
|
34
|
+
void <%= @name %>_grow(struct <%= @name %>* v) {
|
35
|
+
register size_t new_capacity = v->capacity * __<%= @name %>_growth;
|
36
|
+
v-> data = (<%= @type %>*)realloc(v->data,
|
37
|
+
(size_t)(sizeof(<%= @type %>) * new_capacity));
|
38
|
+
v->capacity = new_capacity;
|
39
|
+
}
|
40
|
+
void <%= @name %>_grow_to(struct <%= @name %>* v, register size_t s) {
|
41
|
+
v->data = (<%= @type %>*)realloc(v->data, (size_t)(sizeof(<%= @type %>) * s));
|
42
|
+
v->capacity = s;
|
43
|
+
}
|
44
|
+
void <%= @name %>_shrink(struct <%= @name %>* v) {
|
45
|
+
<%= @name %>_grow_to(v, v->size);
|
46
|
+
}
|
47
|
+
void <%= @name %>_push_back(struct <%= @name %>* v, const <%= @type %> t) {
|
48
|
+
if (v->capacity <= v->length)
|
49
|
+
<%= @name %>_grow(v);
|
50
|
+
v->data[v->length ++] = t;
|
51
|
+
}
|
52
|
+
<%= @type %> <%= @name %>_pop_back(struct <%= @name %>* v) {
|
53
|
+
return v->data[-- v->length];
|
54
|
+
}
|
55
|
+
void <%= @name %>_insert(struct <%= @name %>* v,
|
56
|
+
register const size_t idx,
|
57
|
+
const <%= @type %> t) {
|
58
|
+
if(v->capacity <= v->length)
|
59
|
+
<%= @name %>_grow(v);
|
60
|
+
memmove(v->data+idx+1, v->data+idx, sizeof(t) * (v->length - idx));
|
61
|
+
++ v->length;
|
62
|
+
v->data[idx] = t;
|
63
|
+
}
|
64
|
+
void <%= @name %>_insert_range(struct <%= @name %>* v,
|
65
|
+
register const size_t idx,
|
66
|
+
<%= @type %>* begin,
|
67
|
+
<%= @type %>* end) {
|
68
|
+
register size_t a1 = end - begin;
|
69
|
+
register size_t a2 = a1 + v->length;
|
70
|
+
if(v->capacity <= a2)
|
71
|
+
<%= @name %>_grow_to(v, a2);
|
72
|
+
memmove(v->data+idx+a1, v->data+idx, sizeof(<%= @type %>) * (v->length - idx));
|
73
|
+
memcpy(v->data+idx, begin, sizeof(<%= @type %>) * a1);
|
74
|
+
v->length += a1;
|
75
|
+
}
|
76
|
+
void <%= @name %>_remove_range(struct <%= @name %>* v,
|
77
|
+
register const size_t begin,
|
78
|
+
register const size_t end) {
|
79
|
+
<%= @type %>* tmp = (<%= @type %>*)calloc(v->length - end, sizeof(<%= @type %>));
|
80
|
+
memcpy(tmp, v->data+end, sizeof(<%= @type %>) * (v->length - end));
|
81
|
+
memcpy(v->data+begin, tmp, sizeof(<%= @type %>) * (v->length - end));
|
82
|
+
v->length -= end - begin;
|
83
|
+
free(tmp);
|
84
|
+
}
|
85
|
+
void <%= @name %>_remove_range_with_block(struct <%= @name %>* v,
|
86
|
+
register const size_t begin,
|
87
|
+
register const size_t end,
|
88
|
+
void(^block)(<%= @type %>)) {
|
89
|
+
<%= @type %>* tmp = (<%= @type %>*)calloc(v->length - end, sizeof(<%= @type %>));
|
90
|
+
memcpy(tmp, v->data+end, sizeof(<%= @type %>) * (v->length - end));
|
91
|
+
memcpy(v->data+begin, tmp, sizeof(<%= @type %>) * (v->length - end));
|
92
|
+
for (register size_t i=0; i<v->length-end; ++i)
|
93
|
+
block(tmp[i]);
|
94
|
+
v->length -= end - begin;
|
95
|
+
free(tmp);
|
96
|
+
}
|
97
|
+
void <%= @name %>_remove(struct <%= @name %>* v, register const size_t idx) {
|
98
|
+
<%= @name %>_remove_range(v, idx, idx+1);
|
99
|
+
}
|
100
|
+
|
101
|
+
#ifdef __cplusplus
|
102
|
+
}
|
103
|
+
#endif
|
@@ -0,0 +1,53 @@
|
|
1
|
+
//! Created by Chris Miller on 24 December 2009.
|
2
|
+
//! Copyright 2009-2011 Chris Miller. All rights reserved.
|
3
|
+
|
4
|
+
//! This file is generated; modifying it is totally OK, but
|
5
|
+
//! your modifications might get removed if this file is
|
6
|
+
//! regenerated again. I would suggest putting re-implementations
|
7
|
+
//! and/or extensions in another file.
|
8
|
+
|
9
|
+
#ifndef __<%= @guard %>_H__
|
10
|
+
#define __<%= @guard %>_H__
|
11
|
+
|
12
|
+
#include <stddef.h>
|
13
|
+
#include <stdlib.h>
|
14
|
+
#include <string.h>
|
15
|
+
|
16
|
+
#ifdef __cplusplus
|
17
|
+
extern "C" {
|
18
|
+
#endif
|
19
|
+
|
20
|
+
/**
|
21
|
+
* This defines how much the vector will grow when it needs to expand. By default, this is `2.0f`.
|
22
|
+
*/
|
23
|
+
extern
|
24
|
+
float __<%= @name %>_growth;
|
25
|
+
|
26
|
+
struct <%= @name %> {
|
27
|
+
<%= @type %>* data;
|
28
|
+
union {
|
29
|
+
size_t length;
|
30
|
+
size_t size;
|
31
|
+
};
|
32
|
+
size_t capacity;
|
33
|
+
};
|
34
|
+
|
35
|
+
struct <%= @name %>* <%= @name %>_create(const register size_t capacity);
|
36
|
+
void <%= @name %>_free(struct <%= @name %>* v);
|
37
|
+
void <%= @name %>_free_with_block(struct <%= @name %>* v, void(^block)(<%= @type %>));
|
38
|
+
void <%= @name %>_grow(struct <%= @name %>* v);
|
39
|
+
void <%= @name %>_grow_to(struct <%= @name %>* v, register size_t s);
|
40
|
+
void <%= @name %>_shrink(struct <%= @name %>* v);
|
41
|
+
void <%= @name %>_push_back(struct <%= @name %>* v, const <%= @type %> t);
|
42
|
+
<%= @type %> <%= @name %>_pop_back(struct <%= @name %>* v);
|
43
|
+
void <%= @name %>_insert(struct <%= @name %>* v, register const size_t idx, const <%= @type %> t);
|
44
|
+
void <%= @name %>_insert_range(struct <%= @name %>* v, register const size_t idx, <%= @type %>* begin, <%= @type %>* end);
|
45
|
+
void <%= @name %>_remove_range(struct <%= @name %>* v, register const size_t begin, register const size_t end);
|
46
|
+
void <%= @name %>_remove_range_with_block(struct <%= @name %>* v, register const size_t begin, register const size_t end, void(^block)(<%= @type %>));
|
47
|
+
void <%= @name %>_remove(struct <%= @name %>* v, register const size_t idx);
|
48
|
+
|
49
|
+
#ifdef __cplusplus
|
50
|
+
}
|
51
|
+
#endif
|
52
|
+
|
53
|
+
#endif
|
@@ -0,0 +1,24 @@
|
|
1
|
+
def emit_vector name, datatype, outfile
|
2
|
+
@name = name
|
3
|
+
@type = datatype
|
4
|
+
@outfile = File.expand_path outfile
|
5
|
+
@guard = outfile.pathmap("%n").gsub(/\-/,'_').upcase
|
6
|
+
|
7
|
+
headertmpl = 'structure-kits/vector/vector.h.erb'
|
8
|
+
impltmpl = 'structure-kits/vector/vector.c.erb'
|
9
|
+
|
10
|
+
ofh=File.new(@outfile+".gen.h", "w")
|
11
|
+
ofh.write(ERB.new(File.read(headertmpl)).result)
|
12
|
+
ofh.close
|
13
|
+
ofi=File.new(@outfile+".gen.c", "w")
|
14
|
+
ofi.write(ERB.new(File.read(impltmpl)).result)
|
15
|
+
ofi.close
|
16
|
+
end
|
17
|
+
|
18
|
+
namespace :generate do
|
19
|
+
desc "Creates a new fast, lean C vector implementation; useage is: rake vector[name, type, outfile]"
|
20
|
+
task :vector, :name, :datatype, :outfile do |t, args|
|
21
|
+
args.with_defaults(:name => 'int_vector', :datatype => 'int', :outfile => 'int_vector')
|
22
|
+
emit_vector args.name, args.datatype, File.expand_path(args.outfile)
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Christopher Miller
|
@@ -38,8 +38,9 @@ extensions: []
|
|
38
38
|
extra_rdoc_files: []
|
39
39
|
|
40
40
|
files:
|
41
|
-
-
|
42
|
-
-
|
41
|
+
- structure-kits/vector/vector.c.erb
|
42
|
+
- structure-kits/vector/vector.h.erb
|
43
|
+
- structure-kits/vector/vector.rake
|
43
44
|
- bin/conyard
|
44
45
|
- Rakefile
|
45
46
|
- README.md
|
data/tests/vector/vector_test.c
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
//! Created by Chris Miller on 10 October 2011.
|
2
|
-
//! Copyright 2011 Chris Miller. All rights reserved.
|
3
|
-
|
4
|
-
#include <stdio.h>
|
5
|
-
|
6
|
-
#include "int_vector.gen.h"
|
7
|
-
|
8
|
-
int main(int argc, char **argv) {
|
9
|
-
printf("Creating vector... \n");
|
10
|
-
struct int_vector* v = int_vector_create(10);
|
11
|
-
for (int i=0; i<10; ++i) {
|
12
|
-
int_vector_push_back(v, i);
|
13
|
-
printf(" Added element %d to vector\n", i);
|
14
|
-
}
|
15
|
-
int_vector_free(v);
|
16
|
-
printf("Freed vector.\n");
|
17
|
-
return 0;
|
18
|
-
}
|
@@ -1,11 +0,0 @@
|
|
1
|
-
namespace :test do
|
2
|
-
task :vector do
|
3
|
-
Rake::Task['run'].invoke 'tests/vector/vector_test.genconfig'
|
4
|
-
src = [ 'tests/vector/int_vector.gen.c', 'tests/vector/vector_test.c' ]
|
5
|
-
src.each do |src_file|
|
6
|
-
sh "#{$CC} -x c -arch x86_64 -std=gnu99 -fblocks -c #{File.expand_path src_file} -o #{File.expand_path 'tests/vector/'+src_file.pathmap('%n')}.o"
|
7
|
-
end
|
8
|
-
sh "#{$CC} -arch x86_64 -o #{File.expand_path 'tests/vector/vector'} #{File.expand_path 'tests/vector/int_vector.gen.o'} #{File.expand_path 'tests/vector/vector_test.o'}"
|
9
|
-
sh File.expand_path('tests/vector/vector')
|
10
|
-
end
|
11
|
-
end
|