/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.atlas.test;

import static org.apache.jena.atlas.lib.RandomLib.random ;

import java.util.Arrays ;


/** Support for testing B+Trees/BTrees.  May be  generally useful*/

public class Gen
{
    static boolean DIFFERENT = false ;
    /** Generate a random sequence between low (inclusive) and high (exclusive) */ 
    public static int[] rand(int numRand, int low, int high)
    {
        int[] k = new int[numRand] ;
        Arrays.fill(k, -1) ;
        for ( int i = 0 ; i < numRand ; i++ )
        {
            if ( DIFFERENT )
            {
                loop1: while(true)
                {
                    // All different.
                    int x = random.nextInt(high-low)+low ;
                    for ( int j = 0 ; j < i ; j++ )
                    {
                        if ( k[j] == x )
                            continue loop1 ;
                    }
                    k[i] = x ;
                    break ;
                }
            }
            else
            {
                int x = random.nextInt(high-low)+low ;
                k[i] = x ;
            }
            
        }
        
        return k ;
    }

    /** Sort-of jumble a sequence */
    public static int[] permute(int[] x, int num)
    {
        // Collections.shuffle.
        int[] x2 = new int[x.length] ;
        System.arraycopy(x, 0, x2, 0, x.length) ;
        
        for (int i = 0 ; i < num ; i++ )
        {
            int a = random.nextInt(x2.length) ;
            int b = random.nextInt(x2.length) ;
            int t = x2[a] ;
            x2[a] = x2[b] ;
            x2[b] = t ;
        }
        // Checking.
        for ( int k : x )
        {
            boolean found = false ;
            for ( int k2 : x2 )
                if ( k == k2 )
                {
                    found = true ;
                    break ;
                }
            if ( ! found )
                System.err.printf("Corrupted permute: [%s] [%s]\n", strings(x) , strings(x2)) ;
        }
        return x2 ;
    }
    
    public static String strings(int[] keys)
    {
        StringBuilder sb = new StringBuilder() ;
        boolean first = true ;
        for ( int k : keys)
        {
            if ( ! first )
                sb.append(", ") ;
            first = false ;
            sb.append(k) ;
        }
        return sb.toString() ;
    }
 }

@@ -924,17 +924,16 @@
 rays ;%0A%0A
-%0A
 /** Supp
@@ -951,23 +951,8 @@
 ting
- B+Trees/BTrees
 .  M
@@ -974,20 +974,20 @@
 y useful
+ 
 */%0A
-%0A
 public c
@@ -1005,42 +1005,236 @@
     
-static boolean DIFFERENT =
+/** Generate a random sequence between low (inclusive) and high (exclusive): may include duplicates. */ %0A    public static int%5B%5D rand(int numRand, int low, int high) %7B%0A        return rand(numRand, low, high,
  false
+)
  ;
+ %0A    %7D%0A    
 %0A   
@@ -1309,16 +1309,59 @@
 clusive)
+ - choose whether to have duplicates or not
  */ %0A   
@@ -1416,21 +1416,39 @@
 int high
-)%0A   
+, boolean allDifferent)
  %7B%0A     
@@ -1549,32 +1549,24 @@
 Rand ; i++ )
-%0A       
  %7B%0A         
@@ -1577,31 +1577,22 @@
 f ( 
-DIFFERENT )%0A           
+allDifferent )
  %7B%0A 
@@ -1615,37 +1615,23 @@
 oop1
+ 
 : while
+ 
 (true)
-%0A               
  %7B%0A 
@@ -1706,38 +1706,42 @@
 dom.nextInt(high
--
+ - 
 low)
-+
+ + 
 low ;%0A          
@@ -1781,36 +1781,16 @@
  ; j++ )
-%0A                   
  %7B%0A     
@@ -1986,37 +1986,13 @@
    %7D
-%0A            else%0A           
+ else
  %7B%0A 
@@ -2037,14 +2037,18 @@
 high
--
+ - 
 low)
-+
+ + 
 low 
@@ -2090,28 +2090,16 @@
       %7D%0A
-            
 %0A       
@@ -2101,24 +2101,16 @@
       %7D%0A
-        
 %0A       
@@ -2214,20 +2214,16 @@
 int num)
-%0A   
  %7B%0A     
@@ -2338,24 +2338,16 @@
 ngth) ;%0A
-        
 %0A       
@@ -2352,16 +2352,17 @@
    for (
+ 
 int i = 
@@ -2380,24 +2380,16 @@
  ; i++ )
-%0A       
  %7B%0A     
@@ -2615,24 +2615,16 @@
  k : x )
-%0A       
  %7B%0A     
@@ -2716,32 +2716,16 @@
  == k2 )
-%0A               
  %7B%0A     
@@ -2818,17 +2818,16 @@
   if ( !
- 
 found )%0A
@@ -2904,17 +2904,16 @@
 rings(x)
- 
 , string
@@ -2953,28 +2953,24 @@
  x2 ;%0A    %7D%0A
-    
 %0A    public 
@@ -3002,20 +3002,16 @@
 %5B%5D keys)
-%0A   
  %7B%0A     
@@ -3111,25 +3111,18 @@
 k : keys
-)%0A      
  
+)
  %7B%0A     
@@ -3134,17 +3134,16 @@
   if ( !
- 
 first )%0A

Generate with or without duplicates.