-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrList.cpp
More file actions
executable file
·123 lines (108 loc) · 2.14 KB
/
StrList.cpp
File metadata and controls
executable file
·123 lines (108 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* StrList.cpp
*
* Created on: 13 Aug 2013
* Author: lester
*/
#include "StrList.h"
#include "string.h"
#include "os.h"
#include "log.h"
StrList::StrList(MemPage* mem)
{
this->mem = mem;
this->f_node = 0;
save_space = 0;
waste_space = 0;
string_count = 0;
}
StrList::~StrList()
{
}
void StrList::clear()
{
f_node = 0;
}
/*!
get a new node to store a string of size = size
*/
struct _str_node* StrList::newNode(struct _str_node* next, const char* val, unsigned short size)
{
struct _str_node* p;
waste_space += (sizeof(*p) - sizeof(p->c)); // by overhead string with structure
mem->Get(sizeof(*p) - sizeof(p->c) + size + 1, (void**) &p);
p->next = next;
p->size = size;
*p->c = 0;
strncat(p->c, val, size);
return p;
}
/*!
always compare with the next node
*/
void StrList::push(const char* val, unsigned short size, char* &str)
{
int ir;
struct _str_node *node, *n;
string_count++;
n = 0;
str = 0;
// compare with first node
// check size
if (f_node)
{
if (size == f_node->size)
ir = strncmp(f_node->c, val, size);
else if (size < f_node->size)
ir = 1;
else
ir = -1;
} else
ir = 1;
// there is not first node or new value is below order than first node
if ((!f_node) || (ir > 0))
{
n = newNode(f_node, val, size);
f_node = n;
} else if (ir == 0)
{
// string has been found
save_space+= (size +1); // but reusing string
n = f_node;
} else
{
// compare with other nodes
for (node = f_node; node->next; node = node->next)
{
if (node->next->size == size)
{
if ((ir = strncmp(node->next->c, val, size)) == 0)
{
// string has been found
save_space+= (size +1); // but reusing string
n = node->next;
break;
}
if (ir > 0)
break;
} else if (size < node->next->size)
break;
}
// end of list reached or node is below in list
if (!n)
{
n = newNode(node->next, val, size);
node->next = n;
}
}
str = n->c;
}
void StrList::Print()
{
log_d("Pushed %d strings, wasted %d bytes, saved %d bytes",string_count,waste_space,save_space);
struct _str_node *node;
for (node = f_node; node; node = node->next)
{
//printf("%s\n",node->c);
}
}