Skip to content

Commit 3ba3c5f

Browse files
committed
allow constructing some arrays from ranges
gets a little weird with strings, though.
1 parent 32d0712 commit 3ba3c5f

File tree

6 files changed

+44
-32
lines changed

6 files changed

+44
-32
lines changed

include/clasp/core/array.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,14 @@ template <typename MyLeafType, typename ValueType, typename MyParentType> class
479479
template_SimpleVector(size_t length, value_type initialElement = value_type(), bool initialElementSupplied = false,
480480
size_t initialContentsSize = 0, const value_type* initialContents = NULL)
481481
: Base(), _Data(length, initialElement, initialElementSupplied, initialContentsSize, initialContents){};
482+
template_SimpleVector(size_t length, const value_type& initialElement)
483+
: Base(), _Data(length, initialElement) {};
484+
template <std::ranges::sized_range R>
485+
template_SimpleVector(R&& initialContents)
486+
: Base(), _Data(initialContents) {};
487+
template <std::input_iterator I, std::sized_sentinel_for<I> S>
488+
template_SimpleVector(I first, S last)
489+
: Base(), _Data(first, last) {};
482490

483491
public:
484492
static void never_invoke_allocator() { gctools::GCAbstractAllocator<template_SimpleVector>::never_invoke_allocator(); };

include/clasp/core/array_int8.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,21 @@ class SimpleVector_byte8_t_O : public specialized_SimpleVector_byte8_t {
3434
SimpleVector_byte8_t_O(size_t length, value_type initialElement = value_type(), bool initialElementSupplied = false,
3535
size_t initialContentsSize = 0, const value_type* initialContents = NULL)
3636
: TemplatedBase(length, initialElement, initialElementSupplied, initialContentsSize, initialContents){};
37+
template <std::ranges::sized_range R>
38+
SimpleVector_byte8_t_O(size_t length, R&& initialContents)
39+
: TemplatedBase(initialContents) { (void)length; };
40+
3741
static smart_ptr_type make(size_t length, value_type initialElement = value_type(), bool initialElementSupplied = false,
3842
size_t initialContentsSize = 0, const value_type* initialContents = NULL,
3943
bool static_vector_p = false) {
4044
auto bs = gctools::GC<my_type>::allocate_container<gctools::RuntimeStage>(
4145
static_vector_p, length, initialElement, initialElementSupplied, initialContentsSize, initialContents);
4246
return bs;
4347
}
48+
template <std::ranges::sized_range R>
49+
static smart_ptr_type make(R&& initialContents) {
50+
return gctools::GC<my_type>::allocate_container<gctools::RuntimeStage>(false, std::ranges::ssize(initialContents), initialContents);
51+
}
4452

4553
public:
4654
virtual T_sp element_type() const override { return ext::_sym_byte8; };

include/clasp/core/array_t.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@ class SimpleVector_O : public specialized_SimpleVector {
3333
SimpleVector_O(size_t length, value_type initialElement = default_initial_element(), bool initialElementSupplied = true,
3434
size_t initialContentsSize = 0, const value_type* initialContents = NULL)
3535
: TemplatedBase(length, initialElement, initialElementSupplied, initialContentsSize, initialContents){};
36+
template <std::ranges::sized_range R>
37+
// the length arg is necessary even though it's unused, because it's
38+
// passed by the allocate_container machinery.
39+
// the length was computed by make below from the range anyway.
40+
SimpleVector_O(size_t length, R&& initialContents)
41+
: TemplatedBase(initialContents) { (void)length; };
3642
static SimpleVector_sp make(size_t length, T_sp initialElement = nil<T_O>(), bool initialElementSupplied = true,
3743
size_t initialContentsSize = 0, const T_sp* initialContents = NULL, bool static_vector_p = false) {
3844
auto bs = gctools::GC<SimpleVector_O>::allocate_container<gctools::RuntimeStage>(
3945
static_vector_p, length, initialElement, initialElementSupplied, initialContentsSize, initialContents);
4046
return bs;
4147
}
42-
// Used in one place in lisp.cc. FIXME: Maybe remove?
43-
static SimpleVector_sp make(const gc::Vec0<T_sp>& objs) {
44-
size_t len = objs.size();
45-
if (len == 0)
46-
return make(0);
47-
else
48-
return make(len, nil<T_O>(), true, len, &(objs[0]));
48+
template <std::ranges::sized_range R>
49+
static SimpleVector_sp make(R&& initialContents) {
50+
return gctools::GC<SimpleVector_O>::allocate_container<gctools::RuntimeStage>(false, std::ranges::ssize(initialContents), initialContents);
4951
}
5052

5153
public:

include/clasp/gctools/gcarray.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ template <class T> class __attribute__((__packed__)) GCArray_moveable : public G
7777
}
7878
#endif
7979
}
80+
GCArray_moveable(int64_t size, const T& initialElement)
81+
: _MaybeSignedLength(size) {
82+
std::fill(_Data, _Data + size, initialElement);
83+
}
84+
template <std::ranges::sized_range R>
85+
GCArray_moveable(R&& initialContents)
86+
: _MaybeSignedLength(std::ranges::size(initialContents)) {
87+
std::ranges::copy(initialContents, _Data);
88+
}
89+
template <std::input_iterator I, std::sized_sentinel_for<I> S>
90+
GCArray_moveable(I first, S last)
91+
: _MaybeSignedLength(last - first) {
92+
std::ranges::copy(first, last, _Data);
93+
}
8094

8195
public:
8296
inline uint64_t size() const { return this->length(); };

src/core/primitives.cc

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,16 +1891,10 @@ CL_DEFUN SimpleVector_byte8_t_sp core__base_string_to_octets(T_sp tarray) {
18911891
}
18921892
if (gc::IsA<SimpleBaseString_sp>(tarray)) {
18931893
SimpleBaseString_sp sarray = gc::As_unsafe<SimpleBaseString_sp>(tarray);
1894-
SimpleVector_byte8_t_sp result = SimpleVector_byte8_t_O::make(sarray->length(), 0, false, sarray->length(), &(*sarray)[0]);
1895-
return result;
1894+
return SimpleVector_byte8_t_O::make(sarray);
18961895
} else if (gc::IsA<Str8Ns_sp>(tarray)) {
18971896
Str8Ns_sp sarray = gc::As_unsafe<Str8Ns_sp>(tarray);
1898-
AbstractSimpleVector_sp basesv;
1899-
size_t start, end;
1900-
sarray->asAbstractSimpleVectorRange(basesv, start, end);
1901-
SimpleBaseString_sp sbs = gc::As_unsafe<SimpleBaseString_sp>(basesv);
1902-
SimpleVector_byte8_t_sp result = SimpleVector_byte8_t_O::make((end - start), 0, false, (end - start), &(*sbs)[start]);
1903-
return result;
1897+
return SimpleVector_byte8_t_O::make(sarray);
19041898
}
19051899
SIMPLE_ERROR("Don't get here");
19061900
}
@@ -1909,23 +1903,10 @@ DOCGROUP(clasp);
19091903
CL_DEFUN SimpleVector_byte8_t_sp core__character_string_that_fits_in_base_string_to_octets(T_sp tarray) {
19101904
if (gc::IsA<SimpleCharacterString_sp>(tarray)) {
19111905
SimpleCharacterString_sp sarray = gc::As_unsafe<SimpleCharacterString_sp>(tarray);
1912-
SimpleVector_byte8_t_sp result = SimpleVector_byte8_t_O::make(sarray->length(), 0, false);
1913-
for (int i = 0; i < sarray->length(); ++i) {
1914-
int c = sarray[i];
1915-
result[i] = c;
1916-
}
1917-
return result;
1906+
return SimpleVector_byte8_t_O::make(sarray);
19181907
} else if (gc::IsA<StrWNs_sp>(tarray)) {
19191908
StrWNs_sp sarray = gc::As_unsafe<StrWNs_sp>(tarray);
1920-
AbstractSimpleVector_sp basesv;
1921-
size_t start, end;
1922-
sarray->asAbstractSimpleVectorRange(basesv, start, end);
1923-
SimpleVector_byte8_t_sp result = SimpleVector_byte8_t_O::make((end - start), 0, false);
1924-
for (int i = 0; i < sarray->length(); ++i) {
1925-
int c = sarray[i];
1926-
result[i] = c;
1927-
}
1928-
return result;
1909+
return SimpleVector_byte8_t_O::make(sarray);
19291910
}
19301911
SIMPLE_ERROR("Handle Don't get here");
19311912
}

src/llvmo/llvmoExpose.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,7 @@ CL_DEFMETHOD core::T_sp TargetMachine_O::emitModule(core::T_sp stream, core::T_s
627627

628628
if (stream == kw::_sym_simple_vector_byte8) {
629629
SYMBOL_EXPORT_SC_(KeywordPkg, simple_vector_byte8);
630-
core::SimpleVector_byte8_t_sp vector_byte8 = core::SimpleVector_byte8_t_O::make(
631-
stringOutput.size(), 0, false, stringOutput.size(), (const unsigned char*)stringOutput.data());
630+
core::SimpleVector_byte8_t_sp vector_byte8 = core::SimpleVector_byte8_t_O::make(stringOutput);
632631
if (dwo_stream.notnilp()) {
633632
SIMPLE_ERROR("dwo_stream must be nil");
634633
}

0 commit comments

Comments
 (0)