#REFACTORING by stefan
authorStefan Vogel <sv@exept.de>
Wed, 13 Jul 2016 20:26:18 +0200
changeset 20149 3edf2e29ed0d
parent 20148 a0e964e1c7ac
child 20150 3a825b090d4e
child 20151 e47b8777a1cb
#REFACTORING by stefan class: Array changed: #replaceFrom:to:with:startingAt:
Array.st
--- a/Array.st	Wed Jul 13 16:39:15 2016 +0200
+++ b/Array.st	Wed Jul 13 20:26:18 2016 +0200
@@ -303,6 +303,7 @@
     "Modified: 23.4.1996 / 15:55:06 / cg"
 ! !
 
+
 !Array methodsFor:'accessing'!
 
 at:index
@@ -1629,138 +1630,138 @@
     OBJ myClass;
 
     if (
-	(__ClassInstPtr((myClass = __qClass(self)))->c_ninstvars == __mkSmallInteger(0))
+        (__ClassInstPtr((myClass = __qClass(self)))->c_ninstvars == __mkSmallInteger(0))
      && __isNonNilObject(aCollection)
-     && (((t = __qClass(aCollection)) == Array) || (t == myClass))
+     && (((t = __qClass(aCollection)) == Array) || t == ImmutableArray || (t == myClass))
      && __bothSmallInteger(start, stop)
      && __isSmallInteger(repStart)
     ) {
-	startIndex = __intVal(start) - 1;
-	if (startIndex >= 0) {
-	    nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
-	    stopIndex = __intVal(stop) - 1;
-	    count = stopIndex - startIndex + 1;
+        startIndex = __intVal(start) - 1;
+        if (startIndex >= 0) {
+            nIndex = __BYTES2OBJS__(__qSize(self) - OHDR_SIZE);
+            stopIndex = __intVal(stop) - 1;
+            count = stopIndex - startIndex + 1;
 
-	    if ((count > 0) && (stopIndex < nIndex)) {
-		repStartIndex = __intVal(repStart) - 1;
-		if (repStartIndex >= 0) {
-		    repNIndex = __BYTES2OBJS__(__qSize(aCollection)-OHDR_SIZE);
-		    repStopIndex = repStartIndex + (stopIndex - startIndex);
-		    if (repStopIndex < repNIndex) {
-			src = &(__InstPtr(aCollection)->i_instvars[repStartIndex]);
-			dst = &(__InstPtr(self)->i_instvars[startIndex]);
-			if (aCollection == self) {
-			    /*
-			     * no need to check stores if copying
-			     * from myself
-			     */
+            if ((count > 0) && (stopIndex < nIndex)) {
+                repStartIndex = __intVal(repStart) - 1;
+                if (repStartIndex >= 0) {
+                    repNIndex = __BYTES2OBJS__(__qSize(aCollection)-OHDR_SIZE);
+                    repStopIndex = repStartIndex + (stopIndex - startIndex);
+                    if (repStopIndex < repNIndex) {
+                        src = &(__InstPtr(aCollection)->i_instvars[repStartIndex]);
+                        dst = &(__InstPtr(self)->i_instvars[startIndex]);
+                        if (aCollection == self) {
+                            /*
+                             * no need to check stores if copying
+                             * from myself
+                             */
 
-			    /*
-			     * take care of overlapping copy
-			     * do not depend on memset being smart enough
-			     * (some are not ;-)
-			     */
-			    if (src < dst) {
-				/* must do a reverse copy */
-				src += count;
-				dst += count;
-#ifdef __UNROLL_LOOPS__
-				while (count > 8) {
-				    dst[-1] = src[-1];
-				    dst[-2] = src[-2];
-				    dst[-3] = src[-3];
-				    dst[-4] = src[-4];
-				    dst[-5] = src[-5];
-				    dst[-6] = src[-6];
-				    dst[-7] = src[-7];
-				    dst[-8] = src[-8];
-				    dst -= 8; src -= 8;
-				    count -= 8;
-				}
-#endif
-				while (count-- > 0) {
-				    *--dst = *--src;
-				}
-				RETURN ( self );
-			    }
+                            if (src < dst) {
+                                /*
+                                 * take care of overlapping copy
+                                 * memcpy() is not smart enough by definition for overlkapping copies
+                                 * memmove() is!
+                                 */
+#if defined(FAST_MEMCPY)
+                                memmove(dst, src, __OBJS2BYTES__(count));
+#else
+                                /* must do a reverse copy */
+                                src += count;
+                                dst += count;
+# ifdef __UNROLL_LOOPS__
+                                while (count > 8) {
+                                    dst[-1] = src[-1];
+                                    dst[-2] = src[-2];
+                                    dst[-3] = src[-3];
+                                    dst[-4] = src[-4];
+                                    dst[-5] = src[-5];
+                                    dst[-6] = src[-6];
+                                    dst[-7] = src[-7];
+                                    dst[-8] = src[-8];
+                                    dst -= 8; src -= 8;
+                                    count -= 8;
+                                }
+# endif // __UNROLL_LOOPS__
+                                while (count-- > 0) {
+                                    *--dst = *--src;
+                                }
+#endif // ! FAST_MEMCPY
+                                RETURN ( self );
+                            }
 #ifdef SOFTWARE_PIPELINE
-			    {
-				/*
-				 * the loop below fetches one longWord behind
-				 * this should not be a problem
-				 */
-				OBJ t1 = src[0];
-				count--;
-				if (count) {
-				    dst++; src++;
-				    do {
-					dst[-1] = t1;
-					t1 = src[0];
-					src++;
-					dst++;
-				    } while (count--);
-				} else {
-				    dst[0] = t1;
-				}
-			    }
+                            {
+                                /*
+                                 * the loop below fetches one longWord behind
+                                 * this should not be a problem
+                                 */
+                                OBJ t1 = src[0];
+                                count--;
+                                if (count) {
+                                    dst++; src++;
+                                    do {
+                                        dst[-1] = t1;
+                                        t1 = src[0];
+                                        src++;
+                                        dst++;
+                                    } while (count--);
+                                } else {
+                                    dst[0] = t1;
+                                }
+                            }
+#elif defined(bcopy4)
+                            count = __OBJS2BYTES__(count) / 4;
+                            bcopy4(src, dst, count);
+#elif defined(FAST_MEMCPY)
+                            memmove(dst, src, __OBJS2BYTES__(count));
 #else
+# if defined(__UNROLL_LOOPS__)
+                            while (count >= 8) {
+                                dst[0] = src[0];
+                                dst[1] = src[1];
+                                dst[2] = src[2];
+                                dst[3] = src[3];
+                                dst[4] = src[4];
+                                dst[5] = src[5];
+                                dst[6] = src[6];
+                                dst[7] = src[7];
+                                dst += 8; src += 8;
+                                count -= 8;
+                            }
+# endif // __UNROLL_LOOPS__
+                            while (count--) {
+                                *dst++ = *src++;
+                            }
+#endif
+                        } else {    
+                            // not copying into the same object
+                            REGISTER int spc = __qSpace(self);
+#ifdef __UNROLL_LOOPS__
+                            while (count >= 8) {
+                                t = src[0]; dst[0] = t; __STORE_SPC(self, t, spc);
+                                t = src[1]; dst[1] = t; __STORE_SPC(self, t, spc);
+                                t = src[2]; dst[2] = t; __STORE_SPC(self, t, spc);
+                                t = src[3]; dst[3] = t; __STORE_SPC(self, t, spc);
+                                t = src[4]; dst[4] = t; __STORE_SPC(self, t, spc);
+                                t = src[5]; dst[5] = t; __STORE_SPC(self, t, spc);
+                                t = src[6]; dst[6] = t; __STORE_SPC(self, t, spc);
+                                t = src[7]; dst[7] = t; __STORE_SPC(self, t, spc);
+                                count -= 8; src += 8; dst += 8;
+                            }
+#endif
+                            while (count-- > 0) {
+                                *dst++ = t = *src++;
+                                __STORE_SPC(self, t, spc);
+                            }
+                        }
+                        RETURN ( self );
+                    }
+                }
+            }
 
-# ifdef bcopy4
-			    bcopy4(src, dst, count);
-# else
-#  ifdef FAST_MEMCPY
-			    memcpy(dst, src, __OBJS2BYTES__(count));
-#  else
-#   ifdef __UNROLL_LOOPS__
-			    while (count >= 8) {
-				dst[0] = src[0];
-				dst[1] = src[1];
-				dst[2] = src[2];
-				dst[3] = src[3];
-				dst[4] = src[4];
-				dst[5] = src[5];
-				dst[6] = src[6];
-				dst[7] = src[7];
-				dst += 8; src += 8;
-				count -= 8;
-			    }
-#   endif
-			    while (count--) {
-				*dst++ = *src++;
-			    }
-#  endif
-# endif
-#endif
-			} else {
-			    REGISTER int spc = __qSpace(self);
-#ifdef __UNROLL_LOOPS__
-			    while (count >= 8) {
-				t = src[0]; dst[0] = t; __STORE_SPC(self, t, spc);
-				t = src[1]; dst[1] = t; __STORE_SPC(self, t, spc);
-				t = src[2]; dst[2] = t; __STORE_SPC(self, t, spc);
-				t = src[3]; dst[3] = t; __STORE_SPC(self, t, spc);
-				t = src[4]; dst[4] = t; __STORE_SPC(self, t, spc);
-				t = src[5]; dst[5] = t; __STORE_SPC(self, t, spc);
-				t = src[6]; dst[6] = t; __STORE_SPC(self, t, spc);
-				t = src[7]; dst[7] = t; __STORE_SPC(self, t, spc);
-				count -= 8; src += 8; dst += 8;
-			    }
-#endif
-			    while (count-- > 0) {
-				t = *src++;
-				*dst++ = t;
-				__STORE_SPC(self, t, spc);
-			    }
-			}
-			RETURN ( self );
-		    }
-		}
-	    }
-
-	    if (count == 0) {
-		RETURN ( self );
-	    }
-	}
+            if (count == 0) {
+                RETURN ( self );
+            }
+        }
     }
 %}.
     ^ super replaceFrom:start to:stop with:aCollection startingAt:repStart