kwqdictimpl.cpp

来自「手机浏览器源码程序,功能强大」· C++ 代码 · 共 145 行

CPP
145
字号
/*
 * Copyright (C) 2003 Apple Computer, Inc.  All rights reserved.
 * Portions Copyright (c) 2005 Nokia Corporation, 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 COMPUTER, 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 COMPUTER, 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 "KWQDictImpl.h"

static unsigned int KWQHashFunctionQString(QString str, unsigned int size)
{
	return str.hash() % size;
}

KWQDictImpl::KWQDictImpl(int size, bool caseSensitive, void (*delFunc)(void *)) :
    m_caseSensitive(caseSensitive), deleteFunc( delFunc ),
    table( new KWQHashTable<QString, void*>( KWQHashFunctionQString, size ) )
{
}

void KWQDictImpl::insert(const QString &key, const void *value)
{
    QString str = m_caseSensitive ? key : key.lower();
	table->insert( str, const_cast<void*>(value) );
}

bool KWQDictImpl::remove(const QString &key, bool deleteItem)
{
    QString str = m_caseSensitive ? key : key.lower();
	KWQHashTable<QString, void*>::iterator it = table->find( str );

	if( it != table->end() )
	{
		if( deleteItem && deleteFunc )
			deleteFunc( it.value() );

		it.remove();
		return true;
	}

	return false;
}

void *KWQDictImpl::find(const QString &key) const
{
    QString str = m_caseSensitive ? key : key.lower();
	KWQHashTable<QString, void*>::iterator it = table->find( str );
	if( it != table->end() )
		return it.value();

	return NULL;
}

KWQDictImpl::~KWQDictImpl()
	{
	}

void KWQDictImpl::clear( bool del_item )
{
	// delete all values
	if( del_item && deleteFunc )
	{
		KWQHashTable<QString, void*>::iterator it = table->begin();
		KWQHashTable<QString, void*>::iterator end = table->end();

		while( it != end )
		{
			deleteFunc( it.value() );
			++it;
		}
	}

	// delete all hash entries
	table->clear();
}

uint KWQDictImpl::count() const
{
	return table->count();
}

KWQDictImpl &KWQDictImpl::assign(const KWQDictImpl &di, bool deleteItems)
{
    if (deleteItems) {
		clear(true);
    }

	table = di.table;

    return *this;
}

uint KWQDictIteratorImpl::count() const
	{
	return dict.table->count();
	}

void *KWQDictIteratorImpl::current() const
	{
	if( iter != dict.table->end() )
		return iter.value();
	return NULL;
	}

void *KWQDictIteratorImpl::toFirst()
	{
	iter = dict.table->begin();
	if( iter != dict.table->end() )
		return iter.value();
	return NULL;
	}

void *KWQDictIteratorImpl::operator++()
	{
	++iter;
	if( iter != dict.table->end() ) return iter.value();
	else return NULL;
	}

QString KWQDictIteratorImpl::currentStringKey() const
	{
	if( iter != dict.table->end() )
		return iter.key();
	return QString();
	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?