
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  dropDuplicateColumns_.R                                                                #
  #           Last Updated Funclist  :  08 Feb 2015,  5:11 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   dropDuplicateColumns_ ( DT, If.CantDrop.MakeNameUnique=TRUE, verbose=TRUE, uniqueSep="." )                               #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

#####################################################
###   ###  OLD VERSION:     
###   ###  See note in the new version.     
###   ###  Might be better sticking with old    
#####################################################
# dropDuplicateColumns_ <- function(DT, If.CantDrop.MakeNameUnique=TRUE, showWarnings=TRUE, uniqueSep=".") {
#     ## delete dup columns
#   dups <- names(DT)[duplicated(names(DT))]
#   for (dup in dups) {
#      if(!areEqual( DT[, names(DT) == dup, with=FALSE] )) {
#       browser()
#         if (showWarnings)
#           warning("Different column values for duplicate column  \'", dup, "\'\n")
#       } else {
#         while (sum(names(DT) == dup) > 1)
#             DT[, (dup) := NULL]
#       }
#   }
#   setnames(DT, make.unique(names(DT), sep=uniqueSep))
# }# 
#####################################################


dropDuplicateColumns_ <- function(DT, If.CantDrop.MakeNameUnique=TRUE, verbose=TRUE, uniqueSep=".") {
## delete dup columns

  if (verbose) {
    unDroppedCols <- c()
    droppedCols   <- c()
  }

  ## DOH!!
  ##    names(DT)[duplicated(names(DT))]
  ##  Already iterates over triples!!!  Dumbass    
  dups <- unique(names(DT)[duplicated(names(DT))])
  for (dup in dups) {
    cols <- which(names(DT)==dup)
    # might be 3 cols with same name, hence we iterate
    toDrop <- c()
    for (i in 2:length(cols)) {
      browser(expr={i > length(DT) || i > length(cols)})
      c1 <- cols[[1]]
      ci <- cols[[i]]
      if (identical(DT[[c1]], DT[[ci]]))
        toDrop <- c(i, toDrop) # highest to lowest
    } # // inner for-loop

    # Notice we are dropping form highest column number to lowest
    for (j in seq_along(toDrop))
      DT[, (cols[toDrop[j]]) := NULL]

    # log for output
    if (verbose) { 
      if (length(cols) - length(toDrop) > 1)
        unDroppedCols <- c(unDroppedCols, names(DT)[[c1]]) 
      if (length(toDrop))
        droppedCols   <- c(droppedCols, names(DT)[[c1]]) 
    }
  } # // outter for-loop

  if (If.CantDrop.MakeNameUnique && anyDuplicated(names(DT)))
    setnames(DT, make.unique(names(DT)))

  if (verbose) {
    if (length(droppedCols))
      cat(warningCols("The following columns were dropped:", droppedCols))
    if(length(unDroppedCols))
      cat(warningCols(paste0("The following columns have same names but were NOT dropped", ifelse(If.CantDrop.MakeNameUnique, " (but were converted to unique names).", ".")) , unDroppedCols))
  }
}

## TESTING 
if (FALSE)  {
    x <- seq(3L)
    ch <- LETTERS[1:3]
    tripEq <- 11L:13L

    DT.TEST <- data.table(
      ch.f = ch,
      ch.f = factor(ch),
      ch.dup = ch,
      ch.dup = ch,
      double = x, 
      double = x, 
      diffName = x,
      tripEq = tripEq,
      tripEq = tripEq,
      tripEq = tripEq,
      tripEq = tripEq,
      tripDiff = tripEq,
      tripDiff = x,
      tripDiff = tripEq,
      x.int.num = as.integer(x),
      x.int.num = as.numeric(x)
    )

    dropDuplicateColumns_(DT.TEST, verbose=TRUE, If.CantDrop.MakeNameUnique=TRUE)
}
