001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.algorithms.decisiontrees.utils;
020
021import java.util.ArrayList;
022import java.util.List;
023
024public class SetUtils{
025
026        
027        //************************** OPERAZIONI SUI SOTTOINSIEMI DEL FRAME OF DISCERNEMENT     **********************************/
028         
029
030        /**
031         * Verifies if an element is in the list 
032         * @param elem
033         * @param list
034         * @return
035         */
036        public static <T> boolean isIn(T elem, List<T> list){
037                for(Object current:list){
038                        if (elem.equals(current)){
039                                
040                                return true;
041                        }
042                        
043                }
044                return false;
045                
046        }
047
048        /**
049         * Returns the intersection between two lists
050         * @param <T>
051         * @param list1
052         * @param list2
053         * @return
054         */
055        @SuppressWarnings("unchecked")
056        public  static <T> List<T> intersection(List<T> list1, List<T> list2){
057                // check the maximum lenght between list1 and  list1
058                if(list1.size()<=list2.size()){
059                        List<T> intersection= new ArrayList<>();
060                        // if  the element of list2 are contained in lista 2, list2 is the intersection
061                        for(Object elem: list2){
062                                if(isIn((T)elem, list1)){
063                                        intersection.add((T)elem);
064                                }
065                                        
066                                
067                        }
068                        
069                        return intersection;
070                }
071                else
072                        return intersection(list2,list1); // recursive call 
073        }
074        
075        /**
076         * Returns the list that is the union of the two list (without duplicates)       
077         * @param list1
078         * @param list2
079         * @return
080         */
081        public static <T> List<T> union(List<T> list1,List<T> list2){
082                List<T> result= intersection(list1, list2);// take the common elements between  list 2 and list 2
083                
084                for(T elem:list1){ //add the element that are in list1 but not in result yet
085                        if(!isIn(elem,list1)){
086                                result.add(elem);
087                        }
088                }
089                        
090                for(T elem:list2){ //add the elment that are in list2 but not in the result yet
091                        if(!isIn(elem,list2)){
092                                result.add(elem);
093                        }
094                }
095                return result;
096                
097        }
098        /**
099         * Check if the two lists contain exactly the same elements
100         * @param <T>
101         * @param l1
102         * @param l2
103         * @return
104         */
105        public static <T> boolean areEquals(List<T>l1, List<T> l2){
106                return l1.containsAll(l2) && l2.containsAll(l1);
107
108        }
109        
110        /**
111         * Find an element in a power set 
112         * @param classes
113         * @param powerSet
114         * @return
115         */
116        public static <T> int find(List<T> classes, List<List<T>> powerSet){
117                int pos=0;
118                for(List<T> elem:powerSet){
119                        
120                        if(areEquals(classes,elem))
121                                return pos;
122                        else
123                                pos++;
124                }
125                return -1; // nel caso in cui non lo trova
126                
127                
128        }
129        public static <T> int find(List<T> classes, List<T>[] powerSet){
130                int pos=0;
131                for(List<T> elem:powerSet){
132                        
133                        if(areEquals(classes,elem))
134                                return pos;
135                        else
136                                pos++;
137                }
138                return -1; // nel caso in cui non lo trova
139                
140                
141        }
142        public static <T> T extractValueFromSingleton(List<T> list){
143                
144                if (list.size()!=1)
145                        throw new RuntimeException("It is not a singleton");
146                return (list.get(0));
147                
148                
149        }
150        public static <T> void insertValueinSingleton(List<T> list,T elem){
151                
152                if (list.size()!=0)
153                        throw new RuntimeException("The list is not empty");
154                list.add(elem);
155                
156                
157        }
158        /**
159         * Returns sublists having a predetermined length
160         * @param powerSet
161         * @param cardinality
162         * @return
163         */
164        @SuppressWarnings("rawtypes")
165        public static <T> List[] getSubsets(List<T>[] powerSet, int cardinality){
166                if(cardinality>Math.log10(powerSet.length)/Math.log10(2))
167                        throw new RuntimeException("La cardinalit� � maggiore di"+(Math.log10(powerSet.length)/Math.log10(2)));
168                
169                List<List<T>> sottoinsiemi= new ArrayList<>();
170                for(List<T> elem:powerSet){
171                        if(elem.size()==cardinality)
172                                sottoinsiemi.add(elem);
173                        
174                }
175                //converto tutto in un array
176                List[] result= new List[sottoinsiemi.size()];
177                for(int i=0;i<result.length;i++){
178                        result[i]=sottoinsiemi.get(i);
179                        
180                }
181                return result;
182        } 
183        
184
185}