From: Joe Groff Date: Tue, 8 Jun 2010 22:00:11 +0000 (-0700) Subject: specialized-arrays, specialized-vectors: add direct-slice, direct-head, direct-tail... X-Git-Tag: 0.97~4669^2~35 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=5ca4c343c4330fa2670f83b987058de705363287 specialized-arrays, specialized-vectors: add direct-slice, direct-head, direct-tail, etc. words for creating specialized-arrays over subsets of specialized sequences --- diff --git a/basis/specialized-arrays/specialized-arrays-docs.factor b/basis/specialized-arrays/specialized-arrays-docs.factor index fd1a4a72f2..b476a47072 100644 --- a/basis/specialized-arrays/specialized-arrays-docs.factor +++ b/basis/specialized-arrays/specialized-arrays-docs.factor @@ -1,4 +1,4 @@ -USING: help.markup help.syntax byte-arrays alien ; +USING: help.markup help.syntax byte-arrays alien math sequences ; IN: specialized-arrays HELP: SPECIALIZED-ARRAY: @@ -13,6 +13,28 @@ HELP: SPECIALIZED-ARRAYS: { POSTPONE: SPECIALIZED-ARRAY: POSTPONE: SPECIALIZED-ARRAYS: } related-words +HELP: direct-slice +{ $values { "from" integer } { "to" integer } { "seq" "a specialized array" } { "seq'" "a new specialized array" } } +{ $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as the subsequence of " { $snippet "seq" } " from elements " { $snippet "from" } " up to but not including " { $snippet "to" } ". Like " { $link slice } ", raises an error if " { $snippet "from" } " or " { $snippet "to" } " is out of bounds." } ; + +HELP: direct-head +{ $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } } +{ $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as the first " { $snippet "n" } " elements of " { $snippet "seq" } ". Like " { $link head } ", raises an error if " { $snippet "n" } " is out of bounds." } ; + +HELP: direct-tail +{ $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } } +{ $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as " { $snippet "seq" } " without the first " { $snippet "n" } " elements. Like " { $link tail } ", raises an error if " { $snippet "n" } " is out of bounds." } ; + +HELP: direct-head* +{ $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } } +{ $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as " { $snippet "seq" } " without the last " { $snippet "n" } " elements. Like " { $link head* } ", raises an error if " { $snippet "n" } " is out of bounds." } ; + +HELP: direct-tail* +{ $values { "seq" "a specialized array" } { "n" integer } { "seq'" "a new specialized array" } } +{ $description "Constructs a new specialized array of the same type as " { $snippet "seq" } " sharing the same underlying memory as the last " { $snippet "n" } " elements of " { $snippet "seq" } ". Like " { $link tail* } ", raises an error if " { $snippet "n" } " is out of bounds." } ; + +{ direct-slice direct-head direct-tail direct-head* direct-tail* } related-words + ARTICLE: "specialized-array-words" "Specialized array words" "The " { $link POSTPONE: SPECIALIZED-ARRAY: } " and " { $link POSTPONE: SPECIALIZED-ARRAYS: } " parsing words generate specialized array types if they haven't been generated already and add the following words to the vocabulary search path, where " { $snippet "T" } " is the C type in question:" { $table @@ -25,7 +47,16 @@ ARTICLE: "specialized-array-words" "Specialized array words" { { $snippet ">T-array" } { "Converts a sequence into a specialized array of type " { $snippet "T" } "; stack effect " { $snippet "( seq -- array )" } } } { { $snippet "T-array{" } { "Literal syntax, consists of a series of values terminated by " { $snippet "}" } } } } -"Behind the scenes, these words are placed in a vocabulary named " { $snippet "specialized-arrays.instances.T" } ", however this vocabulary should not be placed in a " { $link POSTPONE: USING: } " form directly. Instead, always use " { $link POSTPONE: SPECIALIZED-ARRAY: } " or " { $link POSTPONE: SPECIALIZED-ARRAYS: } ". This ensures that the vocabulary can get generated the first time it is needed." ; +"Behind the scenes, these words are placed in a vocabulary named " { $snippet "specialized-arrays.instances.T" } ", however this vocabulary should not be placed in a " { $link POSTPONE: USING: } " form directly. Instead, always use " { $link POSTPONE: SPECIALIZED-ARRAY: } " or " { $link POSTPONE: SPECIALIZED-ARRAYS: } ". This ensures that the vocabulary can get generated the first time it is needed." +$nl +"Additionally, special versions of the standard " { $link } ", " { $link head } ", and " { $link tail } " sequence operations are provided for specialized arrays to create a new specialized array object sharing storage with a subsequence of an existing array:" +{ $subsections + direct-slice + direct-head + direct-tail + direct-head* + direct-tail* +} ; ARTICLE: "specialized-array-c" "Passing specialized arrays to C functions" "If a C function is declared as taking a parameter with a pointer or an array type (for example, " { $snippet "float*" } " or " { $snippet "int[3]" } "), instances of the relevant specialized array can be passed in." diff --git a/basis/specialized-arrays/specialized-arrays-tests.factor b/basis/specialized-arrays/specialized-arrays-tests.factor index 3a34b3891b..02424a22fd 100644 --- a/basis/specialized-arrays/specialized-arrays-tests.factor +++ b/basis/specialized-arrays/specialized-arrays-tests.factor @@ -191,3 +191,16 @@ SPECIALIZED-ARRAY: struct-resize-test \ struct-resize-test-usage forget ] with-compilation-unit ] unit-test + +[ int-array{ 4 5 6 } ] [ 3 6 int-array{ 1 2 3 4 5 6 7 8 } direct-slice ] unit-test +[ int-array{ 1 2 3 } ] [ int-array{ 1 2 3 4 5 6 7 8 } 3 direct-head ] unit-test +[ int-array{ 1 2 3 4 5 } ] [ int-array{ 1 2 3 4 5 6 7 8 } 3 direct-head* ] unit-test +[ int-array{ 4 5 6 7 8 } ] [ int-array{ 1 2 3 4 5 6 7 8 } 3 direct-tail ] unit-test +[ int-array{ 6 7 8 } ] [ int-array{ 1 2 3 4 5 6 7 8 } 3 direct-tail* ] unit-test + + +[ int-array{ 1 2 3 4 55555 6 7 8 } ] [ + int-array{ 1 2 3 4 5 6 7 8 } + 3 6 pick direct-slice [ 55555 1 ] dip set-nth +] unit-test + diff --git a/basis/specialized-arrays/specialized-arrays.factor b/basis/specialized-arrays/specialized-arrays.factor index dc070f99b4..5fa88e39a2 100644 --- a/basis/specialized-arrays/specialized-arrays.factor +++ b/basis/specialized-arrays/specialized-arrays.factor @@ -32,6 +32,9 @@ M: not-a-byte-array summary ( alien len -- specialized-array ) A boa ; inline +M: A direct-like drop ; inline + : ( n -- specialized-array ) [ \ T ] keep ; inline @@ -71,6 +76,8 @@ M: A length length>> ; inline M: A nth-unsafe underlying>> \ T alien-element ; inline +M: A nth-c-ptr underlying>> \ T array-accessor drop swap ; inline + M: A set-nth-unsafe underlying>> \ T set-alien-element ; inline : >A ( seq -- specialized-array ) A new clone-like ; @@ -132,6 +139,17 @@ M: pointer underlying-type PRIVATE> +: direct-slice ( from to seq -- seq' ) + check-slice + [ nip nth-c-ptr ] + [ drop swap - ] + [ 2nip ] 3tri direct-like ; inline + +: direct-head ( seq n -- seq' ) (head) direct-slice ; inline +: direct-tail ( seq n -- seq' ) (tail) direct-slice ; inline +: direct-head* ( seq n -- seq' ) from-end direct-head ; inline +: direct-tail* ( seq n -- seq' ) from-end direct-tail ; inline + : define-array-vocab ( type -- vocab ) underlying-type [ specialized-array-vocab ] [ '[ _ define-array ] ] bi diff --git a/basis/specialized-vectors/specialized-vectors.factor b/basis/specialized-vectors/specialized-vectors.factor index 5c47a9e879..f96aea6815 100644 --- a/basis/specialized-vectors/specialized-vectors.factor +++ b/basis/specialized-vectors/specialized-vectors.factor @@ -6,6 +6,7 @@ parser prettyprint.custom sequences specialized-arrays specialized-arrays.private strings vocabs vocabs.parser vocabs.generated fry make ; FROM: sequences.private => nth-unsafe ; +FROM: specialized-arrays.private => nth-c-ptr direct-like ; QUALIFIED: vectors.functor IN: specialized-vectors @@ -17,6 +18,7 @@ V DEFINES-CLASS ${T}-vector A IS ${T}-array IS <${A}> + IS >V DEFERS >${V} V{ DEFINES ${V}{ @@ -38,6 +40,9 @@ M: V pprint* pprint-object ; M: V >c-ptr underlying>> underlying>> ; inline M: V byte-length [ length ] [ element-size ] bi * ; inline +M: V direct-like drop ; inline +M: V nth-c-ptr underlying>> nth-c-ptr ; inline + SYNTAX: V{ \ } [ >V ] parse-literal ; INSTANCE: V growable