-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset.cpp
More file actions
107 lines (96 loc) · 2.8 KB
/
Copy pathset.cpp
File metadata and controls
107 lines (96 loc) · 2.8 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
#include "set.h"
#include "../engine.h"
#include "class.h"
#include "file.h"
#include "set_iterator.h"
Set *Set::create() {
Set2 v = Gc::alloc<Set>();
::new(&v->hset) SetType();
return v;
}
Value next_set_clear(const Value *args, int numargs) {
(void)numargs;
args[0].toSet()->hset.clear();
return ValueNil;
}
Value next_set_insert(const Value *args, int numargs) {
(void)numargs;
Value h;
if(!ExecutionEngine::getHash(args[1], &h))
return ValueNil;
auto res = args[0].toSet()->hset.insert(h);
return Value(res.second);
}
Value next_set_iterate(const Value *args, int numargs) {
(void)numargs;
return Value(SetIterator::from(args[0].toSet()));
}
Value next_set_has(const Value *args, int numargs) {
(void)numargs;
Value h;
if(!ExecutionEngine::getHash(args[1], &h))
return ValueNil;
return Value(args[0].toSet()->hset.count(h) > 0);
}
Value next_set_size(const Value *args, int numargs) {
(void)numargs;
return Value((double)args[0].toSet()->hset.size());
}
Value next_set_str(const Value *args, int numargs) {
(void)numargs;
EXPECT(set, "str(f)", 1, File);
File *f = args[1].toFile();
if(!f->stream->isWritable()) {
return FileError::sete("File is not writable!");
}
f->writableStream()->write("{");
Set *a = args[0].toSet();
if(a->hset.size() > 0) {
auto v = a->hset.begin();
if(String::toStringValue(*v, f) == ValueNil)
return ValueNil;
v = std::next(v);
for(auto e = a->hset.end(); v != e; v = std::next(v)) {
f->writableStream()->write(", ");
if(String::toStringValue(*v, f) == ValueNil)
return ValueNil;
}
}
f->writableStream()->write("}");
return ValueTrue;
}
Value next_set_remove(const Value *args, int numargs) {
(void)numargs;
Value h;
if(!ExecutionEngine::getHash(args[1], &h))
return ValueNil;
return Value(args[0].toSet()->hset.erase(h) == 1);
}
Value next_set_values(const Value *args, int numargs) {
(void)numargs;
Set * vs = args[0].toSet();
Array2 a = Array::create(vs->hset.size());
for(auto &v : vs->hset) {
a->insert(v);
}
return Value(a);
}
Value next_set_construct(const Value *args, int numargs) {
(void)numargs;
(void)args;
return Value(Set::create());
}
void Set::init(Class *SetClass) {
// Initialize set class
SetClass->add_builtin_fn("()", 0, next_set_construct);
SetClass->add_builtin_fn("clear()", 0, next_set_clear);
SetClass->add_builtin_fn_nest("insert(_)", 1,
next_set_insert); // can nest
SetClass->add_builtin_fn("iterate()", 0, next_set_iterate);
SetClass->add_builtin_fn_nest("has(_)", 1, next_set_has); // can nest
SetClass->add_builtin_fn("size()", 0, next_set_size);
SetClass->add_builtin_fn_nest("str(_)", 1, next_set_str);
SetClass->add_builtin_fn_nest("remove(_)", 1,
next_set_remove); // can nest
SetClass->add_builtin_fn("values()", 0, next_set_values);
}