initial checkin. StringComparator is a virtual class used for doing String
comparisons. DefaultStringComparator is the default implementation of StringComparator. Map.h/cpp is a simple HashMap implementation for TxObjects. git-svn-id: svn://10.0.0.236/trunk@65695 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla 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/MPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is XSL:P XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Keith Visco.
|
||||
* Portions created by Keith Visco (C) 1999-2000 Keith Visco.
|
||||
* All Rights Reserved..
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: DefaultStringComparator.cpp,v 1.1 2000-04-12 10:49:27 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "StringComparator.h"
|
||||
|
||||
/**
|
||||
* Creates a new DefaultStringComparator
|
||||
**/
|
||||
DefaultStringComparator::DefaultStringComparator() {
|
||||
//-- do nothing for now
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys this DefaultStringComparator
|
||||
**/
|
||||
DefaultStringComparator::~DefaultStringComparator() {
|
||||
//-- do nothing for now
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the given Strings. -1 is returned if str1 is less than str2,
|
||||
* 0 is returned if the two Strings are equal, and 1 is return if str1 is
|
||||
* greater than str2.
|
||||
**/
|
||||
int DefaultStringComparator::compare(const String& str1, const String& str2) {
|
||||
|
||||
int len1 = str1.length();
|
||||
int len2 = str2.length();
|
||||
|
||||
int c = 0;
|
||||
while ((c < len1) && (c < len2)) {
|
||||
Int32 ch1 = str1.charAt(c);
|
||||
Int32 ch2 = str2.charAt(c);
|
||||
if (ch1 < ch2) return -1;
|
||||
else if (ch2 < ch1) return 1;
|
||||
++c;
|
||||
}
|
||||
|
||||
if (len1 == len2) return 0;
|
||||
else if (len1 < len2) return -1;
|
||||
else return 1;
|
||||
|
||||
} //-- compare
|
||||
|
||||
|
||||
288
mozilla/extensions/transformiix/source/base/Map.cpp
Normal file
288
mozilla/extensions/transformiix/source/base/Map.cpp
Normal file
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla 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/MPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is XSL:P XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Keith Visco.
|
||||
*
|
||||
* Portions created by Keith Visco (C) 1999-2000 Keith Visco.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: Map.cpp,v 1.1 2000-04-12 10:50:03 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
* A Hashtable for TxObjects
|
||||
* @version $Revision: 1.1 $ $Date: 2000-04-12 10:50:03 $
|
||||
*/
|
||||
|
||||
|
||||
#include "Map.h"
|
||||
|
||||
//-------------/
|
||||
//- Constants -/
|
||||
//-------------/
|
||||
|
||||
const int Map::DEFAULT_SIZE = 13;
|
||||
|
||||
//----------------/
|
||||
//- Constructors -/
|
||||
//----------------/
|
||||
|
||||
/**
|
||||
* Creates a new Map with the default Size
|
||||
**/
|
||||
Map::Map() {
|
||||
initialize(DEFAULT_SIZE);
|
||||
} //-- Map
|
||||
|
||||
/**
|
||||
* Creates a new Map with the specified number of buckets
|
||||
**/
|
||||
Map::Map(int size) {
|
||||
initialize(size);
|
||||
} //-- Map
|
||||
|
||||
/**
|
||||
* Helper method for Constructors
|
||||
**/
|
||||
void Map::initialize(Int32 size) {
|
||||
|
||||
//-- by default the Map will not delete it's
|
||||
//-- object references
|
||||
doObjectDeletion = MB_FALSE;
|
||||
|
||||
//-- create a new array of bucket pointers
|
||||
elements = new BucketItem*[size];
|
||||
|
||||
//-- initialize all elements to 0;
|
||||
for ( Int32 i = 0; i < size; i++ ) elements[i] = 0;
|
||||
|
||||
numberOfBuckets = size;
|
||||
numberOfElements = 0;
|
||||
} //-- initialize
|
||||
|
||||
/**
|
||||
* Destructor for Map
|
||||
**/
|
||||
Map::~Map() {
|
||||
clear();
|
||||
delete [] elements;
|
||||
} //-- ~Map
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Removes all elements from the Map. If the object deletion flag
|
||||
* has been set to true (by a call to setObjectDeletion) objects
|
||||
* will also be deleted as they are removed from the map
|
||||
**/
|
||||
void Map::clear() {
|
||||
clear(doObjectDeletion);
|
||||
} //-- clear
|
||||
|
||||
/**
|
||||
* Removes all elements from the Map
|
||||
* @param deleteObjects a flag indicating whether or not to delete the
|
||||
* objects and keys currently in the map
|
||||
**/
|
||||
void Map::clear(MBool deleteObjects) {
|
||||
|
||||
for (int i = 0; i < numberOfBuckets; i++) {
|
||||
|
||||
BucketItem* bktItem = elements[i];
|
||||
while (bktItem) {
|
||||
BucketItem* tItem = bktItem;
|
||||
bktItem = bktItem->next;
|
||||
//-- repoint item to 0 to prevent deletion
|
||||
if ( ! deleteObjects ) {
|
||||
tItem->item = 0;
|
||||
tItem->key = 0;
|
||||
}
|
||||
else {
|
||||
delete tItem->item;
|
||||
delete tItem->key;
|
||||
}
|
||||
//--delete tItem;
|
||||
delete tItem;
|
||||
}
|
||||
}
|
||||
numberOfElements = 0;
|
||||
} //-- clear
|
||||
|
||||
/**
|
||||
* Returns the object reference in this Map associated with the given key
|
||||
* @return the object reference in this Map associated with the given key
|
||||
**/
|
||||
TxObject* Map::get(TxObject* key) {
|
||||
BucketItem* item = getBucketItem(key);
|
||||
if ( item ) return item->item;
|
||||
return 0;
|
||||
} //-- get
|
||||
|
||||
/**
|
||||
* Returns true if there are no objects in this map.
|
||||
* @return true if there are no objects in this map.
|
||||
**/
|
||||
MBool Map::isEmpty() {
|
||||
return (numberOfElements == 0) ? MB_TRUE : MB_FALSE;
|
||||
} //-- isEmpty
|
||||
|
||||
|
||||
/**
|
||||
* Returns a List of all the keys in this Map.
|
||||
* Please delete this List when you are done with it
|
||||
**/
|
||||
List* Map::keys() {
|
||||
List* list = new List();
|
||||
for (int i = 0; i < numberOfBuckets; i++) {
|
||||
BucketItem* item = elements[i];
|
||||
while (item) {
|
||||
list->add(item->key);
|
||||
item = item->next;
|
||||
}
|
||||
}
|
||||
return list;
|
||||
} //-- keys
|
||||
|
||||
/**
|
||||
* Adds the TxObject reference to the map and associates it with the given
|
||||
* key
|
||||
**/
|
||||
void Map::put(TxObject* key, TxObject* obj) {
|
||||
|
||||
if ((!key) || (!obj)) return;
|
||||
|
||||
//-- compute hash for key
|
||||
Int32 hashCode = key->hashCode();
|
||||
|
||||
//-- calculate index
|
||||
int idx = hashCode % numberOfBuckets;
|
||||
|
||||
//-- fetch first item in bucket
|
||||
BucketItem* bktItem = elements[idx];
|
||||
|
||||
//-- if bktItem is 0 then there are no items is this Bucket,
|
||||
//-- add to front of list
|
||||
if ( !bktItem ) {
|
||||
elements[idx] = createBucketItem(key, obj);
|
||||
++numberOfElements;
|
||||
}
|
||||
//-- find current item, or add to end of list
|
||||
else {
|
||||
BucketItem* prevItem = bktItem;
|
||||
//-- advance to next spot
|
||||
while ( bktItem ) {
|
||||
//-- if current key equals desired key, break
|
||||
if ( bktItem->key->equals(key) ) {
|
||||
break;
|
||||
}
|
||||
prevItem = bktItem;
|
||||
bktItem = bktItem->next;
|
||||
}
|
||||
//-- if we did not find a bucket Item create a new one
|
||||
if ( !bktItem) {
|
||||
bktItem = createBucketItem(key, obj);
|
||||
prevItem->next = bktItem;
|
||||
bktItem->prev = prevItem;
|
||||
++numberOfElements;
|
||||
}
|
||||
//-- we found bucket item, just set value
|
||||
else bktItem->item = obj;
|
||||
}
|
||||
} //-- put
|
||||
/**
|
||||
* Removes the the specified TxObject from the Map
|
||||
* @param key the TxObject which is used to calculate the hashCode of
|
||||
* the TxObject to remove from the Map
|
||||
* @return the TxObject removed from the Map
|
||||
**/
|
||||
TxObject* Map::remove(TxObject* key) {
|
||||
|
||||
if (!key) return 0;
|
||||
|
||||
// compute hash for key
|
||||
Int32 hashCode = key->hashCode();
|
||||
|
||||
int idx = hashCode % numberOfBuckets;
|
||||
|
||||
BucketItem* bktItem = elements[idx];
|
||||
|
||||
while ( bktItem ) {
|
||||
if ( bktItem->key->equals(key) ) break;
|
||||
bktItem = bktItem->next;
|
||||
}
|
||||
|
||||
if ( bktItem ) {
|
||||
if (bktItem == elements[idx]) elements[idx] = bktItem->next;
|
||||
else bktItem->prev->next = bktItem->next;
|
||||
numberOfElements--;
|
||||
TxObject* txObject = bktItem->item;
|
||||
bktItem->item = 0;
|
||||
delete bktItem;
|
||||
return txObject;
|
||||
}
|
||||
return 0;
|
||||
|
||||
} //-- remove
|
||||
|
||||
/**
|
||||
* Sets the object deletion flag. If set to true, objects, including
|
||||
* keys, in the Map will be deleted upon calling the clear() method, or
|
||||
* upon destruction. By default this is false.
|
||||
**/
|
||||
void Map::setObjectDeletion(MBool deleteObjects) {
|
||||
doObjectDeletion = deleteObjects;
|
||||
} //-- setObjectDeletion
|
||||
|
||||
/**
|
||||
* Returns the number of key-object pairs in the Map
|
||||
* @return the number of key-object pairs in the Map
|
||||
**/
|
||||
int Map::size() {
|
||||
return numberOfElements;
|
||||
} //-- size
|
||||
|
||||
//-------------------/
|
||||
//- Private Methods -/
|
||||
//-------------------/
|
||||
|
||||
Map::BucketItem* Map::createBucketItem(TxObject* key, TxObject* obj)
|
||||
{
|
||||
BucketItem* bktItem = new BucketItem;
|
||||
bktItem->next = 0;
|
||||
bktItem->prev = 0;
|
||||
bktItem->key = key;
|
||||
bktItem->item = obj;
|
||||
return bktItem;
|
||||
} //-- createBucketItem
|
||||
|
||||
Map::BucketItem* Map::getBucketItem(TxObject* key) {
|
||||
|
||||
// compute hash for key
|
||||
Int32 hashCode = key->hashCode();
|
||||
|
||||
int idx = hashCode % numberOfBuckets;
|
||||
|
||||
BucketItem* bktItem = elements[idx];
|
||||
|
||||
while ( bktItem ) {
|
||||
if ( bktItem->key->equals(key) ) return bktItem;
|
||||
bktItem = bktItem->next;
|
||||
}
|
||||
|
||||
return bktItem;
|
||||
|
||||
} //-- getBucketItem
|
||||
155
mozilla/extensions/transformiix/source/base/Map.h
Normal file
155
mozilla/extensions/transformiix/source/base/Map.h
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla 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/MPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is XSL:P XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Keith Visco.
|
||||
*
|
||||
* Portions created by Keith Visco (C) 1999-2000 Keith Visco.
|
||||
* All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: Map.h,v 1.1 2000-04-12 10:49:40 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
* A Hashtable for TxObjects
|
||||
* @version $Revision: 1.1 $ $Date: 2000-04-12 10:49:40 $
|
||||
*/
|
||||
|
||||
#ifndef TRANSFRMX_MAP_H
|
||||
#define TRANSFRMX_MAP_H
|
||||
|
||||
#include "baseutils.h"
|
||||
#include "TxObject.h"
|
||||
#include "List.h"
|
||||
|
||||
class Map : public TxObject {
|
||||
|
||||
|
||||
public:
|
||||
|
||||
//----------------/
|
||||
//- Constructors -/
|
||||
//----------------/
|
||||
|
||||
/**
|
||||
* Creates a new Map with the default Size
|
||||
**/
|
||||
Map();
|
||||
|
||||
/**
|
||||
* Creates a new Map with the specified number of buckets
|
||||
**/
|
||||
Map(int size);
|
||||
|
||||
/**
|
||||
* Destructor for a Map table, will not delete references unless
|
||||
* The setObjectDeletion flag has been set to MB_TRUE
|
||||
**/
|
||||
virtual ~Map();
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of all the keys of this Map.
|
||||
* <BR />
|
||||
* You will need to delete this List when you are done with it.
|
||||
**/
|
||||
List* keys();
|
||||
|
||||
/**
|
||||
* Returns the object reference in this Map associated with the given name
|
||||
* @return the object reference in this Map associated with the given name
|
||||
**/
|
||||
TxObject* get(TxObject* key);
|
||||
|
||||
/**
|
||||
* Adds the TxObject reference to the map and associates it with the given
|
||||
* key
|
||||
**/
|
||||
void put(TxObject* key, TxObject* obj);
|
||||
|
||||
/**
|
||||
* Removes all elements from the Map table
|
||||
**/
|
||||
void clear();
|
||||
|
||||
void clear(MBool doObjectDeletion);
|
||||
|
||||
/**
|
||||
* Returns true if there are no elements in this Map
|
||||
* @return true if there are no elements in this Map.
|
||||
**/
|
||||
MBool isEmpty();
|
||||
|
||||
/**
|
||||
* Removes the object associated with the given key from this Map
|
||||
* @param key the TxObject used to compute the hashCode for the object to remove
|
||||
* from the Map
|
||||
* @return the removed object
|
||||
**/
|
||||
TxObject* remove(TxObject* key);
|
||||
|
||||
/**
|
||||
* Sets the object deletion flag. If set to true, objects in
|
||||
* the Map will be deleted upon calling the clear() method, or
|
||||
* upon destruction. By default this is false.
|
||||
**/
|
||||
void setObjectDeletion(MBool deleteObjects);
|
||||
|
||||
/**
|
||||
* Returns the number of key-element pairs in the Map
|
||||
* @return the number of key-element in the Map
|
||||
**/
|
||||
int size();
|
||||
|
||||
//-------------------/
|
||||
//- Private Members -/
|
||||
//-------------------/
|
||||
|
||||
|
||||
private:
|
||||
|
||||
struct BucketItem {
|
||||
TxObject* key;
|
||||
TxObject* item;
|
||||
BucketItem* next;
|
||||
BucketItem* prev;
|
||||
};
|
||||
|
||||
static const int DEFAULT_SIZE;
|
||||
|
||||
// map table
|
||||
BucketItem** elements;
|
||||
|
||||
Int32 numberOfBuckets;
|
||||
Int32 numberOfElements;
|
||||
MBool doObjectDeletion;
|
||||
|
||||
//-------------------/
|
||||
//- Private Methods -/
|
||||
//-------------------/
|
||||
|
||||
BucketItem* createBucketItem(TxObject* key, TxObject* objPtr);
|
||||
|
||||
BucketItem* getBucketItem(TxObject* key);
|
||||
|
||||
/**
|
||||
* Helper method for constructors
|
||||
**/
|
||||
void initialize(int size);
|
||||
|
||||
}; //-- NamedMap
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla 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/MPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is XSL:P XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Keith Visco.
|
||||
* Portions created by Keith Visco (C) 1999-2000 Keith Visco.
|
||||
* All Rights Reserved..
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: StringComparator.cpp,v 1.1 2000-04-12 10:49:38 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "StringComparator.h"
|
||||
|
||||
|
||||
// const declarations
|
||||
const String StringComparator::EN_LANG = "en";
|
||||
|
||||
/**
|
||||
* Returns an instance of the StringComparator which handles the given Language
|
||||
* <BR>
|
||||
* Note: Remember to destroy instance when done.
|
||||
**/
|
||||
StringComparator* StringComparator::getInstance(const String& lang) {
|
||||
|
||||
if (EN_LANG.isEqual(lang)) return new DefaultStringComparator();
|
||||
/*
|
||||
else if(....) return new ....StringComparator();
|
||||
*/
|
||||
//-- use default
|
||||
return new DefaultStringComparator();
|
||||
} //-- getInstance
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla 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/MPL/
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* The Original Code is XSL:P XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Keith Visco.
|
||||
* Portions created by Keith Visco (C) 1999-2000 Keith Visco.
|
||||
* All Rights Reserved..
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: StringComparator.h,v 1.1 2000-04-12 10:49:39 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "String.h"
|
||||
#include "TxObject.h"
|
||||
|
||||
#ifndef TRANSFRMX_STRING_COMPARATOR_H
|
||||
#define TRANSFRMX_STRING_COMPARATOR_H
|
||||
|
||||
/*
|
||||
An interface for handling String comparisons
|
||||
*/
|
||||
class StringComparator : public TxObject {
|
||||
|
||||
public:
|
||||
|
||||
static const String EN_LANG;
|
||||
|
||||
/**
|
||||
* Returns an instance of the StringComparator which handles the given Language
|
||||
* <BR>
|
||||
* Note: Remember to destroy instance when done.
|
||||
**/
|
||||
static StringComparator* getInstance(const String& lang);
|
||||
|
||||
/**
|
||||
* Compares the given Strings. -1 is returned if str1 is less than str2,
|
||||
* 0 is returned if the two Strings are equal, and 1 is return if str1 is
|
||||
* greater than str2.
|
||||
**/
|
||||
virtual int compare(const String& str1, const String& str2) = 0;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* The default StringComparator, a very simple implementation.
|
||||
**/
|
||||
class DefaultStringComparator : public StringComparator {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Creates a new DefaultStringComparator
|
||||
**/
|
||||
DefaultStringComparator();
|
||||
|
||||
/**
|
||||
* Destroys this StringComparator
|
||||
**/
|
||||
virtual ~DefaultStringComparator();
|
||||
|
||||
/**
|
||||
* Compares the given Strings. -1 is returned if str1 is less than str2,
|
||||
* 0 is returned if the two Strings are equal, and 1 is return if str1 is
|
||||
* greater than str2.
|
||||
**/
|
||||
virtual int compare(const String& str1, const String& str2);
|
||||
|
||||
|
||||
};
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user