/*
 * ARX: Efficient, Stable and Optimal Data Anonymization
 * Copyright (C) 2012 - 2013 Florian Kohlmayer, Fabian Prasser
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx;

import java.io.Serializable;


/**
 * A set of rows
 * 
 * @author Prasser, Kohlmayer
 */
public class RowSet implements Serializable {

    private static final int   ADDRESS_BITS_PER_UNIT = 6;
    private static final int   BITS_PER_UNIT         = 64;
    private static final int   BIT_INDEX_MASK        = 63;
    
    private final long[]       array;
    private final int          length;
    private int                size;

    public static RowSet create(Data data){
        return new RowSet(data);
    }
    
    private RowSet(Data data) {
        this.length = data.getHandle().getNumRows();
        int chunks = (int) (Math.ceil((double) this.length / 64d));
        array = new long[chunks];
    }

    public void add(int rowIndex) {
        int offset = rowIndex >> ADDRESS_BITS_PER_UNIT;
        long temp = array[offset];
        array[offset] |= 1L << (rowIndex & BIT_INDEX_MASK);
        size += array[offset] != temp ? 1 : 0; 
    }
    
    public void remove(int rowIndex){
        int offset = rowIndex >> ADDRESS_BITS_PER_UNIT;
        long temp = array[offset];
        array[offset] &= ~(1L << (rowIndex & BIT_INDEX_MASK));
        size -= array[offset] != temp ? 1 : 0; 
    }

    public boolean contains(int rowIndex) {
        return ((array[rowIndex >> ADDRESS_BITS_PER_UNIT] & (1L << (rowIndex & BIT_INDEX_MASK))) != 0);
    }

    public int length() {
        return this.length;
    }
    
    public int size() {
        return this.size;
    }
}

@@ -968,41 +968,59 @@
 nal 
-int   ADDRESS_BITS_PER_UNIT = 6
+long serialVersionUID = 1492499772279795327L
 ;%0A
+    %0A
     
@@ -1046,16 +1046,24 @@
 l int   
+ADDRESS_
 BITS_PER
@@ -1072,20 +1072,11 @@
 NIT 
-        
 = 6
-4
 ;%0A  
@@ -2302,11 +2302,405 @@
 e;%0A    %7D
+%0A%0A    public void swap(int rowIndex1, int rowIndex2) %7B%0A        %0A        final boolean temp1 = contains(rowIndex1);%0A        final boolean temp2 = contains(rowIndex2);%0A        %0A        if (temp2) %7B%0A            add(rowIndex1);%0A        %7D else %7B%0A            remove(rowIndex1);%0A        %7D%0A        if (temp1) %7B%0A            add(rowIndex2);%0A        %7D else %7B%0A            remove(rowIndex2);%0A        %7D%0A    %7D
 %0A%7D%0A

Implement swap() for RowSets