Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Klepl Jiří
asgn
Commits
e9580b15
Commit
e9580b15
authored
Apr 01, 2020
by
s_kleplj
Browse files
did some optimizations; map changed to unordered_map
parent
c98ccffa
Changes
2
Hide whitespace changes
Inline
Side-by-side
sol/macrosol.cpp
View file @
e9580b15
...
...
@@ -8,15 +8,15 @@ bool macroprocessor_impl::process(
const
std
::
string
&
s
,
std
::
string
&
output
)
{
std
::
vector
<
std
::
string_view
>
parsed
;
parsed
.
clear
()
;
bool
is_def
=
false
;
for
(
auto
[
i
,
j
]
=
std
::
tuple
{
s
.
begin
(),
s
.
end
()};;
auto
[
i
,
j
]
=
std
::
tuple
{
s
.
c
begin
(),
s
.
c
end
()};;
++
i
)
{
if
(
j
!=
s
.
end
())
{
if
(
i
==
s
.
end
())
{
if
(
j
!=
s
.
c
end
())
{
if
(
i
==
s
.
c
end
())
{
parsed
.
emplace_back
(
&*
j
,
&*
i
-
&*
j
);
break
;
}
else
if
(
*
i
==
' '
||
*
i
==
'='
)
{
...
...
@@ -25,10 +25,10 @@ bool macroprocessor_impl::process(
}
parsed
.
emplace_back
(
&*
j
,
&*
i
-
&*
j
);
j
=
s
.
end
();
j
=
s
.
c
end
();
}
}
else
{
if
(
i
==
s
.
end
())
{
if
(
i
==
s
.
c
end
())
{
break
;
}
else
if
(
*
i
==
'='
)
{
is_def
=
true
;
...
...
@@ -47,13 +47,14 @@ bool macroprocessor_impl::process(
const_cast
<
std
::
string_view
&>
(
it
->
first
)
=
it
->
second
.
name_
;
}
else
{
it
->
second
.
value_
.
clear
();
it
->
second
.
length
=
0
;
}
if
(
parsed
.
size
()
!=
1
)
{
std
::
vector
<
word
*>*
vector
=
&
(
it
->
second
.
value_
);
vector
->
reserve
(
parsed
.
size
()
-
1
);
for
(
auto
i
=
parsed
.
begin
()
+
1
;
i
!=
parsed
.
end
();
++
i
)
{
for
(
auto
i
=
parsed
.
c
begin
()
+
1
;
i
!=
parsed
.
c
end
();
++
i
)
{
auto
&&
[
it
,
inserted
]
=
words_
.
try_emplace
(
*
i
,
*
i
,
...
...
@@ -70,31 +71,36 @@ bool macroprocessor_impl::process(
return
false
;
}
else
{
std
::
size_t
length
=
0
;
found
.
clear
();
found
.
reserve
(
parsed
.
size
());
for
(
auto
&&
p
:
parsed
)
{
auto
it
=
words_
.
find
(
p
);
if
(
it
!=
words_
.
end
())
{
std
::
size_t
w_length
=
it
->
second
.
update_length
()
+
1
;
found
.
push_back
(
&
it
->
second
);
if
(
w_length
>
0
)
{
length
+=
w_length
+
1
;
}
}
else
{
found
.
push_back
(
nullptr
);
length
+=
p
.
size
()
+
1
;
}
}
output
.
reserve
(
length
);
for
(
auto
&&
p
:
parsed
)
{
auto
it
=
words_
.
find
(
p
);
if
(
it
!=
words_
.
end
())
{
if
(
it
->
second
.
length
>
0
)
{
it
->
second
.
append_to
(
output
);
for
(
std
::
size_t
i
=
0
;
i
<
parsed
.
size
();
++
i
)
{
if
(
found
[
i
]
!=
nullptr
)
{
if
(
found
[
i
]
->
length
>
0
)
{
found
[
i
]
->
append_to
(
output
);
output
+=
' '
;
}
}
else
{
output
+=
p
;
output
+=
p
arsed
[
i
]
;
output
+=
' '
;
}
}
...
...
@@ -105,8 +111,6 @@ bool macroprocessor_impl::process(
return
true
;
}
}
template
class
macroprocessor
<
policy_scalar
>;
...
...
sol/macrosol.hpp
View file @
e9580b15
...
...
@@ -7,13 +7,11 @@
#include <iomanip>
#include <array>
#include <memory>
#include <map>
#include <
unordered_
map>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include <type_traits>
namespace
macrosol
{
...
...
@@ -40,15 +38,15 @@ class macroprocessor_impl {
private:
struct
word
{
word
(
std
::
string_view
name
)
:
name_
{
std
::
string
{
name
}},
value_
{},
length
{}
name_
{
std
::
string
{
name
}},
length
{
0
}
{}
word
(
std
::
string_view
name
,
bool
)
:
value_
{},
name_
{
std
::
string
{
name
}},
value_
{
this
},
length
{}
length
{
name_
.
size
()}
{}
void
append_to
(
std
::
string
&
output
)
const
{
...
...
@@ -56,7 +54,7 @@ class macroprocessor_impl {
return
;
}
if
(
value_
.
size
()
==
1
&&
value_
[
0
]
==
this
)
{
if
(
value_
.
size
()
==
0
&&
length
>
0
)
{
output
+=
name_
;
}
else
{
for
(
word
*
w
:
value_
)
{
...
...
@@ -71,9 +69,7 @@ class macroprocessor_impl {
}
std
::
size_t
update_length
()
{
if
(
value_
.
size
()
==
1
&&
value_
[
0
]
==
this
)
{
length
=
name_
.
size
();
}
else
{
if
(
value_
.
size
()
!=
0
||
length
==
0
)
{
length
=
0
;
for
(
word
*
w
:
value_
)
{
std
::
size_t
w_length
=
w
->
update_length
();
...
...
@@ -90,12 +86,14 @@ class macroprocessor_impl {
return
length
;
}
std
::
string
name_
;
std
::
vector
<
word
*>
value_
;
std
::
string
name_
;
std
::
size_t
length
;
};
std
::
map
<
std
::
string_view
,
word
>
words_
;
std
::
unordered_map
<
std
::
string_view
,
word
>
words_
;
std
::
vector
<
std
::
string_view
>
parsed
;
std
::
vector
<
word
*>
found
;
};
template
<
typename
policy
>
...
...
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