📄 multiset.java
字号:
// This is copyrighted source file, part of Rakiura JFern package. // See the file LICENSE for copyright information and the terms and conditions// for copying, distributing and modifications of Rakiura JFern package.// Copyright (C) 1999-2002 by Mariusz Nowostawski and others [http://www.rakiura.org]package org.rakiura.cpn;/**/import java.util.AbstractCollection;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.List;/** * Represents a bag of tokens, a multiset. * This is the actual implementation of a multiset. This is one of the basic * datastructures of JFern Petri net. Used to store and manipulate * tokens. Tokens in JFern are any valid Java datatypes, i.e. any * Java Object can be treated as a token, and can be inserted and * removed from the multiset. * *<br><br> * Multiset.java<br> * Created: Mon Sep 25 10:51:00 2000<br> * *@author <a href="mariusz@rakiura.org">Mariusz Nowostawski</a> *@version 2.1.0 $Revision: 1.11 $ *@since 1.0 */public class Multiset extends AbstractCollection { /** TODO: Re-implement this class to use faster more efficient storage. */ private List store = new ArrayList(); /** * Creates empty multiset. */ public Multiset() {} /** * Creates new multiset initialized with a given single element. *@param aToken an initial element for this multiset. */ public Multiset(final Object aToken) { this.store.add(aToken); } /** * Creates new multiset initialized with elements from * a given collection. *@param aCollection initial elements for this multiset. */ public Multiset(final Collection aCollection) { this.store.addAll(aCollection); } /** * Adds a token to this multiset. *@param aToken token to be added. */ public boolean add(final Object aToken) { return this.store.add(aToken); } /** * Removes the first occurance of the specified element * from this multiset. *@param aToken token to be removed *@return <code>true</code> if the element was removed, * <code>false</code> if the token was not present in this multiset. */ public boolean remove(final Object aToken) { return this.store.remove(aToken); } /** * Counts the tokens in this multiset. *@return the number of tokens in this bag. */ public int size() { return this.store.size(); } /** * Returns an iterator over elements of this bag. *@return iterator over elements of this bag. */ public Iterator iterator() { return this.store.iterator(); } /** * Returns a random value from this multiset. * This is a convenience method, useful to access * a single token value from a multiset. If the multiset has * more than one token an arbitrary token is picked. *@return a randomly picked token. */ public Object getAny() { if (this.store.size() > 0) { // ================================================== reimplement it to use random number generator return this.store.get(0); } else { return null; } } /** * Returns <code>n</code> random tokens from this multiset. * This is a convenience method, useful to access * <code>n</code> tokens from a multiset. *@param aNumber a number of randomly selected tokens *@return a multiset with <code>n</code> randomly picked tokens. */ public Multiset getAny(final int aNumber) { // ================================================== reimplement it to use random number generator final Multiset result = new Multiset(); for (int i = 0; i < aNumber; i++) { result.add(this.store.get(i)); } return result; } } // Multiset//////////////////// end of file ////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -