Skip to content
Permalink
Browse files

c++17 add for_each_n function

	modified:   src/cl-stl-algorithm.lisp
	modified:   src/cl-stl-base.lisp
	modified:   src/user-package.lisp
  • Loading branch information...
show-matz committed Feb 25, 2019
1 parent f809000 commit 162c51b42243243c1710a4423c5c5c8db92c31a2
Showing with 80 additions and 0 deletions.
  1. +57 −0 src/cl-stl-algorithm.lisp
  2. +19 −0 src/cl-stl-base.lisp
  3. +4 −0 src/user-package.lisp
@@ -5584,6 +5584,63 @@



; first : input_iterator
; n : integer
; func : unary_function
; returns : input_iterator ( first + n )
#-(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(locally (declare (optimize speed))

;;PTN; for_each_n : 0 - i
(defmethod for_each_n ((first input_iterator) (n integer) func)
(let ((itr (clone first)))
(if (<= n 0)
itr
(let* ((fnc (clone func))
(uf (functor_function fnc)))
(declare (type cl:function uf))
(dotimes (i n)
(funcall uf (_* itr))
(_++ itr))
itr))))

;;PTN; for_each_n : 1 - cci
(defmethod for_each_n ((first cons_const_iterator) (n integer) func)
;;(format t "specialized for_each_n for cons_const_iterator is invoked.~%")
(let ((itr (clone first)))
(if (<= n 0)
itr
(let* ((cns (__cons-itr-cons itr))
(fnc (clone func))
(uf (functor_function fnc)))
(declare (type cl:function uf))
(dotimes (i n)
(funcall uf (car cns))
(setf cns (cdr cns)))
(setf (__cons-itr-cons itr) cns)
itr))))

;;PTN; for_each_n : 2 - cvp
(defmethod for_each_n ((first const-vector-pointer) (n integer) func)
;;(format t "specialized for_each_n for const-vector-pointer is invoked.~%")
(with-operators
(let ((itr @~first))
(if (<= n 0)
itr
(let* ((buf (opr::vec-ptr-buffer itr))
(idx1 (opr::vec-ptr-index itr))
(idx2 (+ idx1 n))
(fnc @~func)
(uf (functor_function fnc)))
(declare (type cl:vector buf))
(declare (type fixnum idx1 idx2))
(declare (type cl:function uf))
(for (nil (< idx1 idx2) (incf idx1))
(funcall uf (aref buf idx1)))
(setf (opr::vec-ptr-index itr) idx2)
itr))))))


; first : input_iterator
; last : input_iterator
; returns : iterator point to found element.
@@ -289,6 +289,10 @@
#-cl-stl-0x98 :any_of
#-cl-stl-0x98 :none_of
:for_each
#-(or
cl-stl-0x98
cl-stl-0x11
cl-stl-0x14) :for_each_n
:find
:find_if
#-cl-stl-0x98 :find_if_not
@@ -1131,6 +1135,21 @@
copy of func.
"))

#-(or cl-stl-0x98 cl-stl-0x11 cl-stl-0x14)
(defgeneric for_each_n (first n func)
(:documentation "
<<signature>>
(cl-stl:for_each_n first n func)
<<parameters>>
first : input_iterator.
n : integer.
func : unary functor.
<<return value>>
iterator point to first + n.
"))

(declare-method-overload find (2 3 #-cl-stl-noextra 4)
:documentation "
<<signature>>
@@ -286,6 +286,10 @@
#-cl-stl-0x98 :any_of
#-cl-stl-0x98 :none_of
:for_each
#-(or
cl-stl-0x98
cl-stl-0x11
cl-stl-0x14) :for_each_n
; :find ; shadowed
:find_if
#-cl-stl-0x98 :find_if_not

0 comments on commit 162c51b

Please sign in to comment.
You can’t perform that action at this time.