trace_visualization 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,93 +0,0 @@
1
- #include "hashmap.h"
2
- #include <stdio.h>
3
-
4
- hashmap_t* hashmap_new() {
5
- hashmap_t* hashmap = (hashmap_t*)malloc(sizeof(hashmap_t));
6
-
7
- hashmap->size = 0;
8
- hashmap->table = (hashmap_element**)calloc(HASHMAP_SIZE, sizeof(hashmap_element*));
9
-
10
- return hashmap;
11
- }
12
-
13
- void* hashmap_get(hashmap_t* map, long key) {
14
- hashmap_element* ptr;
15
- int idx = (int)(key % HASHMAP_SIZE);
16
-
17
- for (ptr = map->table[idx]; ptr != NULL; ptr = ptr->next) {
18
- if (ptr->key == key) {
19
- return ptr->value;
20
- }
21
- }
22
-
23
- return NULL;
24
- }
25
-
26
- int hashmap_put(hashmap_t* map, long key, void* value) {
27
- int result = 1;
28
-
29
- if (hashmap_get(map, key) == NULL) {
30
- hashmap_element* ptr;
31
- hashmap_element* element = (hashmap_element*) malloc(sizeof(hashmap_element));
32
-
33
- element->key = key;
34
- element->value = value;
35
- element->next = NULL;
36
-
37
- int idx = (int)(key % HASHMAP_SIZE);
38
- ptr = map->table[idx];
39
-
40
- if (ptr) {
41
- while (ptr->next) ptr = ptr->next;
42
- ptr->next = element;
43
- } else {
44
- map->table[idx] = element;
45
- }
46
-
47
- map->size += 1;
48
-
49
- result = 0;
50
- }
51
-
52
- return result;
53
- }
54
-
55
- void** hashmap_values(hashmap_t* map) {
56
- int i = 0;
57
- int j = 0;
58
- hashmap_element* ptr;
59
- void** values = (void**)malloc(map->size * sizeof(void*));
60
-
61
- for (; i < HASHMAP_SIZE; i += 1) {
62
- ptr = map->table[i];
63
-
64
- while (ptr) {
65
- values[j] = ptr->value;
66
- ptr = ptr->next;
67
- j += 1;
68
- }
69
- }
70
-
71
- return values;
72
- }
73
-
74
- void hashmap_values_free(void** values) {
75
- free(values);
76
- }
77
-
78
- void hashmap_free(hashmap_t* map) {
79
- hashmap_element* ptr;
80
- hashmap_element* tmp;
81
- int i;
82
- for (i = 0; i < HASHMAP_SIZE; i++) {
83
- for (ptr = map->table[i]; ptr != NULL; ) {
84
- tmp = ptr;
85
- ptr = (hashmap_element*)ptr->next;
86
-
87
- free(tmp);
88
- }
89
- }
90
- free(map);
91
- }
92
-
93
-
@@ -1,27 +0,0 @@
1
- #ifndef __HASHMAP_H__
2
- #define __HASHMAP_H__
3
-
4
- #include <stdlib.h>
5
-
6
- #define HASHMAP_SIZE (256)
7
-
8
- // typedef struct hashmap_element hashmap_element;
9
- struct hashmap_element {
10
- long key;
11
- void* value;
12
- hashmap_element * next;
13
- };
14
-
15
- struct hashmap_t {
16
- hashmap_element** table;
17
- int size;
18
- };
19
-
20
- extern hashmap_t* hashmap_new();
21
- extern int hashmap_put(hashmap_t* map, long key, void* value);
22
- extern void* hashmap_get(hashmap_t* map, long key);
23
- extern void** hashmap_values(hashmap_t* map);
24
- extern void hashmap_values_free(void** values);
25
- extern void hashmap_free(hashmap_t* map);
26
-
27
- #endif
@@ -1,90 +0,0 @@
1
- #include <cppunit/extensions/HelperMacros.h>
2
- #include <iostream>
3
- #include "hashmap.h"
4
-
5
- using namespace std;
6
-
7
- class hashmap_test : public CPPUNIT_NS::TestFixture {
8
- CPPUNIT_TEST_SUITE(hashmap_test);
9
- CPPUNIT_TEST(smoke_test);
10
- CPPUNIT_TEST(complex_test);
11
- CPPUNIT_TEST(hashmap_values_test);
12
- CPPUNIT_TEST_SUITE_END();
13
- public:
14
- void smoke_test(void);
15
- void complex_test(void);
16
- void hashmap_values_test(void);
17
- };
18
-
19
- CPPUNIT_TEST_SUITE_REGISTRATION(hashmap_test);
20
-
21
- void hashmap_test::smoke_test() {
22
- hashmap_t* map = hashmap_new();
23
-
24
- const char* value = "value";
25
- hashmap_put(map, 12345, (char*)value);
26
-
27
- void* ptr = hashmap_get(map, 1);
28
- CPPUNIT_ASSERT(ptr == NULL);
29
-
30
- ptr = hashmap_get(map, 12345);
31
- CPPUNIT_ASSERT(ptr != NULL);
32
- CPPUNIT_ASSERT(strcmp((char*)ptr, value) == 0);
33
-
34
- hashmap_free(map);
35
- }
36
-
37
- void hashmap_test::complex_test() {
38
- hashmap_t* map = hashmap_new();
39
-
40
- CPPUNIT_ASSERT(map != NULL);
41
-
42
- for (long i = 0; i < HASHMAP_SIZE << 3; i += HASHMAP_SIZE >> 2) {
43
- hashmap_put(map, i, (void*)i);
44
- }
45
-
46
- for (int i = 0; i < HASHMAP_SIZE; i++) {
47
- if (i % (HASHMAP_SIZE >> 2) == 0) {
48
- CPPUNIT_ASSERT(map->table[i] != NULL);
49
- } else {
50
- CPPUNIT_ASSERT(map->table[i] == NULL);
51
- }
52
- }
53
-
54
- for (long i = 0; i < HASHMAP_SIZE << 3; i += HASHMAP_SIZE >> 2) {
55
- void* ptr = hashmap_get(map, i);
56
- CPPUNIT_ASSERT(ptr == (void*)i);
57
-
58
- CPPUNIT_ASSERT(hashmap_get(map, i + 1) == NULL);
59
- CPPUNIT_ASSERT(hashmap_get(map, i - 1) == NULL);
60
- }
61
-
62
- hashmap_free(map);
63
- }
64
-
65
- void hashmap_test::hashmap_values_test() {
66
- hashmap_t* map = hashmap_new();
67
-
68
- CPPUNIT_ASSERT(map != NULL);
69
-
70
- int k = 2;
71
-
72
- for (long i = 0; i < HASHMAP_SIZE * k; i += 1) {
73
- hashmap_put(map, i, (void*)i);
74
- }
75
-
76
- CPPUNIT_ASSERT(map->size == HASHMAP_SIZE * k);
77
-
78
- void** values = hashmap_values(map);
79
- CPPUNIT_ASSERT(values != NULL);
80
-
81
- for (int i = 0; i < map->size; i++) {
82
- void* value = values[i];
83
-
84
- int int_value = (i % k) * HASHMAP_SIZE + i / k;
85
- CPPUNIT_ASSERT(value == (void*)int_value);
86
- }
87
-
88
- hashmap_values_free(values);
89
- hashmap_free(map);
90
- }
@@ -1,11 +0,0 @@
1
- #ifndef __LEXEME_H__
2
- #define __LEXEME_H__
3
-
4
- struct lexeme_t {
5
- const char* name;
6
- const char* source;
7
- long numeric;
8
- int ord;
9
- };
10
-
11
- #endif
@@ -1,50 +0,0 @@
1
- #include "lexeme_table.h"
2
- #include <stdlib.h>
3
-
4
- hashmap_t* lexeme_table_new() {
5
- return hashmap_new();
6
- }
7
-
8
- lexeme_t* install_lexeme(hashmap_t* lexeme_table, const char* name, const char* source, long numeric) {
9
- lexeme_t* lexeme = NULL;
10
-
11
- void* ptr = hashmap_get(lexeme_table, numeric);
12
-
13
- if (ptr == NULL) {
14
- lexeme = (lexeme_t*)malloc(sizeof(lexeme_t));
15
-
16
- lexeme->name = name;
17
- lexeme->source = source;
18
- lexeme->numeric = numeric;
19
-
20
- hashmap_put(lexeme_table, numeric, lexeme);
21
- } else {
22
- lexeme = (lexeme_t*)ptr;
23
- }
24
-
25
- return lexeme;
26
- }
27
-
28
- int compare_lexeme(const void* a, const void* b) {
29
- return ((lexeme_t*)*(void**)a)->numeric - ((lexeme_t*)*(void**)b)->numeric;
30
- }
31
-
32
- void reorder_lexemes(hashmap_t* lexeme_table) {
33
- void** values = hashmap_values(lexeme_table);
34
-
35
- qsort(values, lexeme_table->size, sizeof(void*), compare_lexeme);
36
-
37
- int ord = 0;
38
- long prev = -1;
39
-
40
- for (int i = 0; i < lexeme_table->size; i++) {
41
- int numeric = ((lexeme_t*)values[i])->numeric;
42
-
43
- if (numeric != prev) {
44
- ord += 1;
45
- prev = numeric;
46
- }
47
-
48
- ((lexeme_t*)values[i])->ord = ord;
49
- }
50
- }
@@ -1,13 +0,0 @@
1
- #ifndef __LEXEME_TABLE_H__
2
- #define __LEXEME_TABLE_H__
3
-
4
- #include "lexeme.h"
5
- #include "hashmap.h"
6
-
7
- extern hashmap_t* lexeme_table_new();
8
-
9
- extern lexeme_t* install_lexeme(hashmap_t* lexeme_table, const char* name, const char* source, long numeric);
10
-
11
- extern void reorder_lexemes(hashmap_t* lexeme_table);
12
-
13
- #endif
@@ -1,61 +0,0 @@
1
- #ifndef __LEXEME_TABLE_H__
2
- #define __LEXEME_TABLE_H__
3
-
4
- #include <map>
5
- #include <vector>
6
- #include <algorithm>
7
- #include "lexeme.h"
8
-
9
- class lexeme_table {
10
- public:
11
-
12
- ~lexeme_table() {
13
- for (std::map<long, lexeme_t*>::iterator iter = lexeme_table.begin(); iter != lexeme_table.end(); iter++) {
14
- delete iter->second;
15
- }
16
- }
17
-
18
- lexeme_t* install_lexeme(const char* name, const char* source, long numeric) {
19
- std::map<long, lexeme_t*>::iterator iter = lexeme_table.find(numeric);
20
-
21
- lexeme_t* lexeme = NULL;
22
-
23
- if (iter == lexeme_table.end()) {
24
- lexeme = new lexeme_t(name, source, numeric);
25
- lexeme_table[numeric] = lexeme;
26
- } else {
27
- lexeme = iter->second;
28
- }
29
-
30
- return lexeme;
31
- }
32
-
33
- void reorder() {
34
- std::vector<lexeme_t*> tmp;
35
- for (std::map<long, lexeme_t*>::iterator iter = lexeme_table.begin(); iter != lexeme_table.end(); iter++) {
36
- tmp.push_back(iter->second);
37
- }
38
-
39
- std::sort(tmp.begin(), tmp.end(), reorder_function);
40
-
41
- int ord = 0;
42
- long prev = -1;
43
- for (std::vector<lexeme_t*>::iterator iter = tmp.begin(); iter != tmp.end(); iter++) {
44
- if (prev != (*iter)->numeric) {
45
- ord += 1;
46
- prev = (*iter)->numeric;
47
- }
48
-
49
- (*iter)->ord = ord;
50
- }
51
- }
52
-
53
- private:
54
- std::map<long, lexeme_t*> lexeme_table;
55
-
56
- static bool reorder_function(lexeme_t* a, lexeme_t* b) {
57
- return a->numeric < b->numeric;
58
- }
59
- };
60
-
61
- #endif
@@ -1,42 +0,0 @@
1
- #include <stdio.h>
2
- #include <time.h>
3
-
4
- /**
5
- * When flex finds a match, yytext points to the first character of the match
6
- * in the input buffer. The string itself is part of the input buffer, and is
7
- * NOT allocated separately
8
- */
9
- extern const char* yytext;
10
-
11
- /**
12
- * Implements in two ways: (f)lex and unit-tests
13
- */
14
- extern void output_lexeme(const char* name, const char* source, int value);
15
-
16
- //-----------------------------------------------------------------------------
17
- void parse_identifier() {
18
- int id;
19
- sscanf(yytext, "[%d]", &id);
20
- output_lexeme("ID", yytext, id);
21
- }
22
-
23
- //-----------------------------------------------------------------------------
24
- void parse_ip() {
25
- unsigned ips[4];
26
- sscanf(yytext, "%d.%d.%d.%d", ips, ips + 1, ips + 2, ips + 3);
27
- unsigned result = (((ips[0] << 24) & 0xFF000000) | ((ips[1] << 16) & 0xFF0000) | ((ips[2] << 8) & 0xFF00) | (ips[3] & 0xFF));
28
- output_lexeme("IP", yytext, result);
29
- }
30
-
31
- //-----------------------------------------------------------------------------
32
- void parse_date() {
33
- struct tm tm;
34
- time_t t;
35
-
36
- if (strptime(yytext, "[%d %b %Y %H:%M:%S]", &tm) == NULL)
37
- /* handle error */ ;
38
-
39
- t = mktime(&tm);
40
-
41
- output_lexeme("TIME", yytext, t);
42
- }
@@ -1,71 +0,0 @@
1
- #include <cppunit/extensions/HelperMacros.h>
2
- #include <iostream>
3
- #include "lexeme.h"
4
- #include "lexeme_table.h"
5
-
6
- const char* yytext;
7
-
8
- void output_lexeme(const char* name, long value);
9
-
10
- extern void parse_identifier();
11
- extern void parse_ip();
12
- extern void parse_date();
13
-
14
- using namespace std;
15
-
16
- class parser_test : public CPPUNIT_NS::TestFixture {
17
- CPPUNIT_TEST_SUITE(parser_test);
18
- CPPUNIT_TEST(identifier_parser_test);
19
- CPPUNIT_TEST(lexeme_table_test);
20
- CPPUNIT_TEST_SUITE_END();
21
- public:
22
- void identifier_parser_test(void);
23
- void lexeme_table_test(void);
24
- };
25
-
26
- CPPUNIT_TEST_SUITE_REGISTRATION(parser_test);
27
-
28
- lexeme_t lexeme;
29
- // lexeme.name = "";
30
- // lexeme.source = "";
31
- // lexeme.numeric = -1;
32
- // lexeme.ord = -1;
33
-
34
- void output_lexeme(const char* name, long value) {
35
- lexeme.name = name;
36
- lexeme.numeric = value;
37
- }
38
-
39
- //-----------------------------------------------------------------------------
40
- void parser_test::identifier_parser_test(void) {
41
- yytext = "[123]";
42
-
43
- parse_identifier();
44
-
45
- CPPUNIT_ASSERT(strcmp("ID", lexeme.name) == 0);
46
- }
47
-
48
-
49
- //-----------------------------------------------------------------------------
50
- void parser_test::lexeme_table_test(void) {
51
- hashmap_t* table = lexeme_table_new();
52
-
53
- lexeme_t* lexeme = install_lexeme(table, "id", "[123]", 123);
54
- lexeme_t* another_lexeme = install_lexeme(table, "id", "[124]", 124);
55
- lexeme_t* repeat_lexeme = install_lexeme(table, "id", "[123]", 123);
56
-
57
- CPPUNIT_ASSERT(lexeme != NULL);
58
- CPPUNIT_ASSERT(another_lexeme != NULL);
59
- CPPUNIT_ASSERT(repeat_lexeme != NULL);
60
-
61
- CPPUNIT_ASSERT_EQUAL(lexeme, repeat_lexeme);
62
-
63
- reorder_lexemes(table);
64
-
65
- CPPUNIT_ASSERT_EQUAL(1, lexeme->ord);
66
- CPPUNIT_ASSERT_EQUAL(2, another_lexeme->ord);
67
- }
68
-
69
-
70
-
71
-