1999-02-24 07:58:06 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-02-24 07:58:06 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-02-24 07:58:06 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-02-24 07:58:06 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-11-30 23:36:32 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-02-24 07:58:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIEnumerator.h"
|
|
|
|
|
1999-03-11 09:56:59 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Intersection Enumerators
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-02-24 07:58:06 +00:00
|
|
|
class nsConjoiningEnumerator : public nsIBidirectionalEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIEnumerator methods:
|
1999-08-22 11:25:50 +00:00
|
|
|
NS_DECL_NSIENUMERATOR
|
1999-02-24 07:58:06 +00:00
|
|
|
|
|
|
|
// nsIBidirectionalEnumerator methods:
|
1999-08-22 11:25:50 +00:00
|
|
|
NS_DECL_NSIBIDIRECTIONALENUMERATOR
|
1999-02-24 07:58:06 +00:00
|
|
|
|
|
|
|
// nsConjoiningEnumerator methods:
|
|
|
|
nsConjoiningEnumerator(nsIEnumerator* first, nsIEnumerator* second);
|
|
|
|
virtual ~nsConjoiningEnumerator(void);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsIEnumerator* mFirst;
|
|
|
|
nsIEnumerator* mSecond;
|
|
|
|
nsIEnumerator* mCurrent;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsConjoiningEnumerator::nsConjoiningEnumerator(nsIEnumerator* first, nsIEnumerator* second)
|
|
|
|
: mFirst(first), mSecond(second), mCurrent(first)
|
|
|
|
{
|
|
|
|
NS_ADDREF(mFirst);
|
|
|
|
NS_ADDREF(mSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsConjoiningEnumerator::~nsConjoiningEnumerator(void)
|
|
|
|
{
|
|
|
|
NS_RELEASE(mFirst);
|
|
|
|
NS_RELEASE(mSecond);
|
|
|
|
}
|
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsConjoiningEnumerator, nsIBidirectionalEnumerator, nsIEnumerator)
|
1999-02-24 07:58:06 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConjoiningEnumerator::First(void)
|
|
|
|
{
|
|
|
|
mCurrent = mFirst;
|
|
|
|
return mCurrent->First();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-11 09:56:59 +00:00
|
|
|
nsConjoiningEnumerator::Next(void)
|
1999-02-24 07:58:06 +00:00
|
|
|
{
|
|
|
|
nsresult rv = mCurrent->Next();
|
|
|
|
if (NS_FAILED(rv) && mCurrent == mFirst) {
|
|
|
|
mCurrent = mSecond;
|
|
|
|
rv = mCurrent->First();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-11 09:56:59 +00:00
|
|
|
nsConjoiningEnumerator::CurrentItem(nsISupports **aItem)
|
1999-02-24 07:58:06 +00:00
|
|
|
{
|
|
|
|
return mCurrent->CurrentItem(aItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-11 09:56:59 +00:00
|
|
|
nsConjoiningEnumerator::IsDone(void)
|
1999-02-24 07:58:06 +00:00
|
|
|
{
|
1999-03-11 09:56:59 +00:00
|
|
|
return (mCurrent == mFirst && mCurrent->IsDone() == NS_OK)
|
|
|
|
|| (mCurrent == mSecond && mCurrent->IsDone() == NS_OK)
|
1999-10-28 03:16:48 +00:00
|
|
|
? NS_OK : NS_ENUMERATOR_FALSE;
|
1999-02-24 07:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-11 09:56:59 +00:00
|
|
|
nsConjoiningEnumerator::Last(void)
|
1999-02-24 07:58:06 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIBidirectionalEnumerator* be;
|
1999-11-30 23:36:32 +00:00
|
|
|
rv = mSecond->QueryInterface(NS_GET_IID(nsIBidirectionalEnumerator), (void**)&be);
|
1999-02-24 07:58:06 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
mCurrent = mSecond;
|
|
|
|
rv = be->Last();
|
|
|
|
NS_RELEASE(be);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-03-11 09:56:59 +00:00
|
|
|
nsConjoiningEnumerator::Prev(void)
|
1999-02-24 07:58:06 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIBidirectionalEnumerator* be;
|
1999-11-30 23:36:32 +00:00
|
|
|
rv = mCurrent->QueryInterface(NS_GET_IID(nsIBidirectionalEnumerator), (void**)&be);
|
1999-02-24 07:58:06 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = be->Prev();
|
|
|
|
NS_RELEASE(be);
|
|
|
|
if (NS_FAILED(rv) && mCurrent == mSecond) {
|
1999-11-30 23:36:32 +00:00
|
|
|
rv = mFirst->QueryInterface(NS_GET_IID(nsIBidirectionalEnumerator), (void**)&be);
|
1999-02-24 07:58:06 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
mCurrent = mFirst;
|
|
|
|
rv = be->Last();
|
|
|
|
NS_RELEASE(be);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-03-11 10:47:54 +00:00
|
|
|
extern "C" NS_COM nsresult
|
1999-02-24 07:58:06 +00:00
|
|
|
NS_NewConjoiningEnumerator(nsIEnumerator* first, nsIEnumerator* second,
|
|
|
|
nsIBidirectionalEnumerator* *aInstancePtrResult)
|
|
|
|
{
|
|
|
|
if (aInstancePtrResult == 0)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsConjoiningEnumerator* e = new nsConjoiningEnumerator(first, second);
|
|
|
|
if (e == 0)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(e);
|
|
|
|
*aInstancePtrResult = e;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-03-11 09:56:59 +00:00
|
|
|
|
|
|
|
static nsresult
|
|
|
|
nsEnumeratorContains(nsIEnumerator* e, nsISupports* item)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
for (e->First(); e->IsDone() != NS_OK; e->Next()) {
|
|
|
|
nsISupports* other;
|
|
|
|
rv = e->CurrentItem(&other);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (item == other) {
|
|
|
|
NS_RELEASE(other);
|
|
|
|
return NS_OK; // true -- exists in enumerator
|
|
|
|
}
|
|
|
|
NS_RELEASE(other);
|
|
|
|
}
|
1999-10-28 03:16:48 +00:00
|
|
|
return NS_ENUMERATOR_FALSE; // false -- doesn't exist
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Intersection Enumerators
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsIntersectionEnumerator : public nsIEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIEnumerator methods:
|
1999-08-22 11:25:50 +00:00
|
|
|
NS_DECL_NSIENUMERATOR
|
1999-03-11 09:56:59 +00:00
|
|
|
|
|
|
|
// nsIntersectionEnumerator methods:
|
|
|
|
nsIntersectionEnumerator(nsIEnumerator* first, nsIEnumerator* second);
|
|
|
|
virtual ~nsIntersectionEnumerator(void);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsIEnumerator* mFirst;
|
|
|
|
nsIEnumerator* mSecond;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsIntersectionEnumerator::nsIntersectionEnumerator(nsIEnumerator* first, nsIEnumerator* second)
|
|
|
|
: mFirst(first), mSecond(second)
|
|
|
|
{
|
|
|
|
NS_ADDREF(mFirst);
|
|
|
|
NS_ADDREF(mSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntersectionEnumerator::~nsIntersectionEnumerator(void)
|
|
|
|
{
|
|
|
|
NS_RELEASE(mFirst);
|
|
|
|
NS_RELEASE(mSecond);
|
|
|
|
}
|
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsIntersectionEnumerator, nsIEnumerator)
|
1999-03-11 09:56:59 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIntersectionEnumerator::First(void)
|
|
|
|
{
|
|
|
|
nsresult rv = mFirst->First();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIntersectionEnumerator::Next(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// find the first item that exists in both
|
|
|
|
for (; mFirst->IsDone() != NS_OK; mFirst->Next()) {
|
|
|
|
nsISupports* item;
|
|
|
|
rv = mFirst->CurrentItem(&item);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// see if it also exists in mSecond
|
|
|
|
rv = nsEnumeratorContains(mSecond, item);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_RELEASE(item);
|
1999-03-11 11:01:16 +00:00
|
|
|
if (rv == NS_OK) {
|
|
|
|
// found in both, so return leaving it as the current item of mFirst
|
1999-03-11 09:56:59 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
1999-03-11 11:01:16 +00:00
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIntersectionEnumerator::CurrentItem(nsISupports **aItem)
|
|
|
|
{
|
1999-03-11 11:01:16 +00:00
|
|
|
return mFirst->CurrentItem(aItem);
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIntersectionEnumerator::IsDone(void)
|
|
|
|
{
|
1999-03-11 11:01:16 +00:00
|
|
|
return mFirst->IsDone();
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-03-11 10:47:54 +00:00
|
|
|
extern "C" NS_COM nsresult
|
1999-03-11 09:56:59 +00:00
|
|
|
NS_NewIntersectionEnumerator(nsIEnumerator* first, nsIEnumerator* second,
|
|
|
|
nsIEnumerator* *aInstancePtrResult)
|
|
|
|
{
|
|
|
|
if (aInstancePtrResult == 0)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsIntersectionEnumerator* e = new nsIntersectionEnumerator(first, second);
|
|
|
|
if (e == 0)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(e);
|
|
|
|
*aInstancePtrResult = e;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Union Enumerators
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsUnionEnumerator : public nsIEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIEnumerator methods:
|
1999-08-22 11:25:50 +00:00
|
|
|
NS_DECL_NSIENUMERATOR
|
1999-03-11 09:56:59 +00:00
|
|
|
|
|
|
|
// nsUnionEnumerator methods:
|
|
|
|
nsUnionEnumerator(nsIEnumerator* first, nsIEnumerator* second);
|
|
|
|
virtual ~nsUnionEnumerator(void);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsIEnumerator* mFirst;
|
|
|
|
nsIEnumerator* mSecond;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsUnionEnumerator::nsUnionEnumerator(nsIEnumerator* first, nsIEnumerator* second)
|
|
|
|
: mFirst(first), mSecond(second)
|
|
|
|
{
|
|
|
|
NS_ADDREF(mFirst);
|
|
|
|
NS_ADDREF(mSecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUnionEnumerator::~nsUnionEnumerator(void)
|
|
|
|
{
|
1999-03-11 11:01:16 +00:00
|
|
|
NS_RELEASE(mFirst);
|
1999-03-11 09:56:59 +00:00
|
|
|
NS_RELEASE(mSecond);
|
|
|
|
}
|
|
|
|
|
1999-08-23 10:14:16 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsIEnumerator)
|
1999-03-11 09:56:59 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnionEnumerator::First(void)
|
|
|
|
{
|
|
|
|
nsresult rv = mFirst->First();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnionEnumerator::Next(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// find the first item that exists in both
|
|
|
|
for (; mFirst->IsDone() != NS_OK; mFirst->Next()) {
|
|
|
|
nsISupports* item;
|
|
|
|
rv = mFirst->CurrentItem(&item);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// see if it also exists in mSecond
|
|
|
|
rv = nsEnumeratorContains(mSecond, item);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_RELEASE(item);
|
1999-03-11 11:01:16 +00:00
|
|
|
if (rv != NS_OK) {
|
|
|
|
// if it didn't exist in mSecond, return, making it the current item
|
1999-03-11 09:56:59 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-03-11 11:01:16 +00:00
|
|
|
|
|
|
|
// each time around, make sure that mSecond gets reset to the beginning
|
|
|
|
// so that when mFirst is done, we'll be ready to enumerate mSecond
|
|
|
|
rv = mSecond->First();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
1999-03-11 11:01:16 +00:00
|
|
|
|
|
|
|
return mSecond->Next();
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnionEnumerator::CurrentItem(nsISupports **aItem)
|
|
|
|
{
|
1999-03-11 11:01:16 +00:00
|
|
|
if (mFirst->IsDone() != NS_OK)
|
|
|
|
return mFirst->CurrentItem(aItem);
|
|
|
|
else
|
|
|
|
return mSecond->CurrentItem(aItem);
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUnionEnumerator::IsDone(void)
|
|
|
|
{
|
1999-03-11 11:01:16 +00:00
|
|
|
return (mFirst->IsDone() == NS_OK && mSecond->IsDone() == NS_OK)
|
1999-10-28 03:16:48 +00:00
|
|
|
? NS_OK : NS_ENUMERATOR_FALSE;
|
1999-03-11 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-03-11 10:47:54 +00:00
|
|
|
extern "C" NS_COM nsresult
|
1999-03-11 09:56:59 +00:00
|
|
|
NS_NewUnionEnumerator(nsIEnumerator* first, nsIEnumerator* second,
|
|
|
|
nsIEnumerator* *aInstancePtrResult)
|
|
|
|
{
|
|
|
|
if (aInstancePtrResult == 0)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsUnionEnumerator* e = new nsUnionEnumerator(first, second);
|
|
|
|
if (e == 0)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(e);
|
|
|
|
*aInstancePtrResult = e;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|