/*
 * Copyright (C) 2017 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include <wtf/DataLog.h>
#include "PtrHashSet.h"

namespace WTF {

PtrHashSet::PtrHashSet()
{
    initialize();
}

PtrHashSet::~PtrHashSet()
{
}

void PtrHashSet::clear()
{
    initialize();
}

void PtrHashSet::initialize()
{
    constexpr unsigned initialSize = 32;
    m_table = Table::create(initialSize);
}

bool PtrHashSet::addSlow(Table* table, unsigned mask, unsigned startIndex, unsigned index, void* ptr)
{
    if (++table->load >= table->maxLoad())
        return resizeAndAdd(ptr);
    
    for (;;) {
        void*& oldEntry = table->array[index];
        if (!oldEntry) {
        	oldEntry = ptr;
            if (m_table != table) {
                // We added an entry to an old table! We need to reexecute the add on the new table.
                return add(ptr);
            }
            return true;
        }
        if (oldEntry == ptr)
            return false;
        index = (index + 1) & mask;
        RELEASE_ASSERT(index != startIndex);
    }
    return false;
}

void PtrHashSet::resizeIfNecessary()
{
    Table* table = m_table.get();
    if (table->load < table->maxLoad())
        return;
    
    OwnPtr<Table> newTable = Table::create(table->size * 2);
    unsigned mask = newTable->mask;
    unsigned load = 0;
    for (unsigned i = 0; i < table->size; ++i) {
        void* ptr = table->array[i];
        if (!ptr)
            continue;
        
        unsigned startIndex = hash(ptr) & mask;
        unsigned index = startIndex;
        for (;;) {
            void*& entryRef = newTable->array[index];
            void* entry = entryRef;
            if (!entry) {
                entryRef = ptr;
                break;
            }
            RELEASE_ASSERT(entry != ptr);
            index = (index + 1) & mask;
            RELEASE_ASSERT(index != startIndex);
        }
        
        load++;
    }
    
    newTable->load = load;

    m_table = newTable.release();
}

bool PtrHashSet::resizeAndAdd(void* ptr)
{
    resizeIfNecessary();
    return add(ptr);
}

PassOwnPtr<PtrHashSet::Table> PtrHashSet::Table::create(unsigned size)
{
    OwnPtr<PtrHashSet::Table> result = adoptPtr(new (fastMalloc(sizeof(Table) + sizeof(void*) * size)) Table());
    result->size = size;
    result->mask = size - 1;
    result->load = 0;
    for (unsigned i = 0; i < size; ++i)
        result->array[i] = 0;
    return result.release();
}

} // namespace WTF

