Set
Set

Set is an abstract data structure used for storing elements. Since the structure reflects the mathematical term set, it does not guarantee any particular order of the stored elements and contains every value at most once (i.e. contains only unique values).

Similarly we can define multiset (bag) – a set, which may contain each value more than once. To implement a system of non-overlaping subsets, we use a data structure called disjoint set.

Implementation

To implement a set in the most straightforward way, we can use a list data structure and forget its ordering and check that it contains every value at most once. Although this approach is easy to implement, it is very ineffective, because almost every operation has to iterate over the underlying list (with O(n) asymptotic complexity). For this reason is the list based implementation used only very rarely. In practice set is usually implemented as a hash table (guarantees on average O(1) complexity of both operations put and contains).

Code

/**
 * Set implemented as a list (using ArrayList)
 * @author Pavel Micka
 * @param <ENTITY> Type parameter of the contained value
 */
public class Set<ENTITY> {
    private List<ENTITY> list;
    /**
     * Constructor
     * @param initialCapacity initial capacity of the underlying ArrayList
     */
    public Set(int initialCapacity){
        list = new ArrayList<ENTITY>(initialCapacity);
    }

    /**
     * Inserts entity into the set
     * @param e entity
     * @return true - if was the insertion successful, false - if the set already contained this value
     */
    public boolean put(ENTITY e) {
        if (list.contains(e)) return false;
        list.add(e);
        return true;
    }

    /**
     * Return (and remove) some element from the set
     * @return element, null if the set is empty
     */
    public ENTITY pick() {
        if(list.size() == 0) return null;
        return list.remove(list.size() - 1);
    }

    /**
     * Remove given entity from the set
     * @param e entity
     * @return true - if the set contained the entity, false - otherwise
     */
    public boolean remove(ENTITY e) {
        return list.remove(e);
    }

    /**
     * Query if the set set contains given entity
     * @param e entity
     * @return true - if the set contains the entity, false - otherwise
     */
    public boolean contains(ENTITY e) {
        return list.contains(e);
    }
    /**
     * Size of the set
     * @return number of stored entities
     */
    public int size() {
        return list.size();
    }
}

SEO od společnosti Digital Pylon


Online casino s algoritmem

České casino online online slot-vegas.cz

Hrajte nejlepší hry jako je GoodGame Empire.





Zajímavé články: Jak najít práci snů? Zvolte kariéru v IT!, Češi mají rádi hrací automaty online, Jak funguje algoritmické obchodování Casino, Online výuka Algoritmus a online marketing mají svá pravidla, Automaty, Matematický vliv, Ratings, Jak fungují algoritmy hazardních her online: více znalostí, více peněz, SYPWAI - nástroj pro vědecký vývoj, Vynikají na globálním trhu: Nejlepší vývojáři softwaru pro online výherní automaty, Jak si vybrat nejlepší české online casino, Proč byste měli hrát online casino VPN revoluce, Kde najdeme algoritmy v každodenním životě?, Čeká vás pracovní pohovor mimo město? Podívejte se, jak dokonale zvládnout včasný příchod, 5 úžasných technologií ze světa hazardních her, Mirror and access to Mostbet, Svou kancelář můžete mít stále po ruce, Jaké výhody má digitalizovaná firma oproti off-line konkurenci?, Jaký systém vybrat pro snadné řízení výroby?, Nahradí umělá inteligence ajťáky?, Důvody, proč používat SnapTik ke stahování videí TikTok, Dokonalý den na pláži: Co si vzít s sebou, aby byl výlet zábavný a bezpečný?, Jak přežít dlouhý let?, Go pay GoodGame Empire, Blockchain, Rozhovor


Doporučujeme

Internet pro vaši firmu na míru

https://www.algoritmy.net