📄 itemset.java
字号:
/*
* 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 2 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Stefan Ludwig
* @author Michael Thess
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @version 1.0
*/
package com.prudsys.pdm.Models.AssociationRules;
import com.prudsys.pdm.Utils.IntVector;
/**
* Itemset used for basket analysis. Just a vector of items which
* are integers. <p>
*
* From PDM CWM extension.
*/
public class ItemSet extends com.prudsys.pdm.Cwm.Core.ModelElement implements com.prudsys.pdm.Core.MiningMatrixElement
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** List of items. */
private IntVector itemList = new IntVector();
/** Support count. */
private int supportCount = 0;
// -----------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------
/**
* An empty itemset.
*/
public ItemSet()
{
}
/**
* An itemset with initial values. Included for
* compatibility with old versions.
*
* @param p The initial set of items in the itemset
*/
public ItemSet(ItemSet p) {
for (int i=0; i<p.getSize(); i++)
this.addItem(p.getIntegerAt(i));
// Included by M. Thess on 2001-04-02:
this.supportCount = p.getSupportCount();
}
/**
* An itemset with initial values and new value. Support count is initialized
* with zero value. Included for compatibility with old versions.
*
* @param p The initial set of items in the itemset
* @param item new item
*/
public ItemSet(ItemSet p, int item) { // Add an item to an existing itemset
for (int i=0; i<p.getSize(); i++)
this.addItem(p.getIntegerAt(i));
this.addItem(item);
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Get the support count for the itemset.
*
* @return support count
*/
public int getSupportCount()
{
return supportCount;
}
/**
* Set the support count value.
*
* @param suppcount the new support count
*/
public void setSupportCount(int suppcount)
{
supportCount=suppcount;
}
/**
* Get the number of items in the itemset.
*
* @return number of items in itemset
*/
public int getSize()
{
return itemList.size();
}
/**
* Sets the itemSetIndex. Does nothing since itemSetIndex was
* removed in Version 1.2.3 to save memory.
*
* @param itemSetIndex itemSetIndex to set
* @deprecated since Version 1.2.3
*/
public void setItemSetIndex( String itemSetIndex )
{
}
/**
* Returns itemSetIndex. Does nothing since itemSetIndex was
* removed in Version 1.2.3 to save memory.
*
* @return always ""
* @deprecated since Version 1.2.3
*/
public String getItemSetIndex()
{
return "";
}
// -----------------------------------------------------------------------
// Item manipulation methods
// -----------------------------------------------------------------------
/**
* Add an item to the itemset.
* The new item is put into the right place to preserve increasing
* order in the set (vector). If the item already exists, it is rejected.
*
* @param element the new item to add
* @return true, if item did not exist in itemset and was added, else false
*/
public boolean addItem(int element)
{
if (itemList.contains(element)) return false;
int i;
for (i=0; i<itemList.size() && itemList.IntegerAt(i)<element; ) i++;
itemList.insertElementAt(element,i);
return true;
}
/**
* Add an item to the itemset.
* Like addItem but without checking for right position and duplication.
*
* @param element the new item to add
*/
public void insertItem(int element)
{
itemList.addElement(element);
}
/**
* Delete item at given position.
*
* @param index position of item to remove
*/
public void delItemAt(int index)
{
if (index<itemList.size())
{
itemList.removeElementAt(index);
}
}
/**
* Remove all items from itemset. Set support count to zero.
*/
public void removeAllItems()
{
itemList.removeAllElements();
supportCount = 0;
}
/**
* Gets item at specified index.
*
* @param index position of item
* @return item at specified position
*/
public int getItemAt(int index)
{
return getIntegerAt(index);
}
/**
* Get the item (an integer) at the given position in the itemset.
*
* @param index the index in the itemset
* @return integer at given index
*/
public int getIntegerAt(int index)
{
return itemList.IntegerAt(index);
}
/**
* Test if the itemset contains a given item.
* Search for the item using interpolation search ( O(log log n) ).
*
* @param element search for this item
* @return true if item is contained in itemset, else false
*/
public boolean contains(int element)
{
if(itemList.size()!=0) return (containsAtPos(element,0)>=0);
else return false;
}
/**
* Tests if the itemset contains a given item from a given start index.
*
* @param element search for this item
* @param from position to start the search
* @return position of item, -1 if not found
*/
public int containsAtPos(int element, int from)
{
int lo=from;
int hi=itemList.size()-1;
int m=0;
while(itemList.IntegerAt(hi)>=element && element>itemList.IntegerAt(lo))
{
m=lo+((element-itemList.IntegerAt(lo))/(itemList.IntegerAt(hi)-itemList.IntegerAt(lo)))*(hi-lo);
if (element>itemList.IntegerAt(m)) lo=m+1;
else if (element<itemList.IntegerAt(m)) hi=m-1; else lo=m;
}
if (itemList.IntegerAt(lo)==element) return lo;
return -1;
}
/**
* Test if the itemset contains a given set of items.
*
* @param subset test if this is really a subset of the itemset
* @return true if it is a subset, else false
*/
public boolean contains(ItemSet subset)
{
// search for sorted subset in sorted itemset
if (itemList.size()<subset.getSize()) return false; // not a SUBset
int idx=0;
for (int i=0; i<subset.getSize(); i++)
{
idx=this.containsAtPos(subset.getIntegerAt(i),idx);
if (idx<0) return false;
}
return true;
}
// -----------------------------------------------------------------------
// java.lang.Object methods
// -----------------------------------------------------------------------
/**
* Find out if two itemsets are equal.
* They must have the same size and contain the same items.
* The support count variables are not compared.
*
* @param obj the itemset to compare with
* @return true if equal, otherwise false
*/
public boolean equals(Object obj)
{
ItemSet is = (ItemSet)obj;
if( itemList.size() != is.getSize() ) return false; // different sizes
for( int i=0; i < itemList.size(); i++ )
if( itemList.IntegerAt(i) != is.getIntegerAt(i)) return false;
return true;
}
/**
* Calculates the hash code.
*
* @return hash code
*/
public int hashCode() {
return itemList.IntegerAt(0);
}
/**
* String representation of itemset.
*
* @return String representation of itemset
*/
public String toString()
{
String text = "";
for (int i = 0; i < itemList.size(); i++)
{
text = text + itemList.IntegerAt(i);
text = text + "\t";
}
text = text + "SuppCount = " + supportCount;
return text;
}
/**
* Print the itemset to the screen.
*/
public void print()
{
System.out.println(toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -