+ L <- length(indices)
+ nb_workers <- floor( L / nb_per_set )
+ rem <- L %% nb_per_set
+ if (nb_workers == 0 || (nb_workers==1 && rem==0))
+ {
+ # L <= nb_per_set, simple case
+ return (list(indices))
+ }
+
+ indices_workers <- lapply( seq_len(nb_workers), function(i)
+ indices[(nb_per_set*(i-1)+1):(nb_per_set*i)] )
+
+ rem <- L %% nb_per_set #number of remaining unassigned items
+ if (rem == 0)
+ return (indices_workers)
+
+ rem <- (L-rem+1):L
+ # If remainder is smaller than min_size, feed it with indices from other sets
+ # until either its size exceed min_size (success) or other sets' size
+ # get lower min_size (failure).
+ while (length(rem) < min_size)
+ {
+ index <- length(rem) %% nb_workers + 1
+ if (length(indices_workers[[index]]) <= min_size)
+ {
+ stop("Impossible to split indices properly for clustering.
+ Try increasing nb_items_clust or decreasing K1")
+ }
+ rem <- c(rem, tail(indices_workers[[index]],1))
+ indices_workers[[index]] <- head( indices_workers[[index]], -1)
+ }
+ return ( c(indices_workers, list(rem) ) )