/*
 * 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/

 * 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 Rhino code, released
 * May 6, 1999.

 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 * Rights Reserved.

 * Contributor(s): 

 * Alternatively, the contents of this file may be used under the
 * terms of the GNU Public License (the "GPL"), in which case the
 * provisions of the GPL are applicable instead of those above.
 * If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use your
 * version of this file under the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this
 * file under either the NPL or the GPL.
 */
#ifdef COMPACT_MEM

#ifndef _JS_RUNTIME_P_H_
#define _JS_RUNTIME_P_H_

#include "js-runtime.h"
#include "js-runtimenew.h"

/* Classes */
typedef JSHashtable<jsString_t, jsString_t, JSRuntime::Allocator>
		_JSGCStringHashtable;
class JSGCStringHashtable: public JSRuntimeNew, public _JSGCStringHashtable {
	protected:
		static  size_t hashCode(jsString_t key) {
			const int M = 31;   // MULTIPLIER
			const int maxLength = 8;
			int value;

			value = 0;
			for (int i = 0; i < maxLength && key[i]; i++) {
				value = value * M + key[i] & 0xff;
			}
			return value;
		}
		static  int cmpKey(jsString_t key1, jsString_t key2) {
			return strcmp(key1, key2);
		}
		static void dupEntry(JSGCStringHashtable::HashtableEntry *dst,
						JSGCStringHashtable::HashtableEntry *src,
						JSRuntime::Allocator *allocator) {
			*dst = *src;
		}
		static void destroyEntry(JSGCStringHashtable::HashtableEntry *tabEntry,
						JSRuntime::Allocator *allocator) {
			allocator->deallocate(tabEntry);
		}

	public:
		JSGCStringHashtable(JSRuntime::Allocator *allocator)
				: _JSGCStringHashtable(hashCode, cmpKey, dupEntry, destroyEntry,
						allocator) {
		}
		virtual ~JSGCStringHashtable() {
		}
		void    set(jsString_t key) {
			put(key, key);
		}
		void    organize(JSGCHeap *gcHeap) {
			HashtableEntry **tab = table;

			if (tab != NULL) {
				for (int index = tableLength ; index-- > 0 ;) {
					for (HashtableEntry *e = tab[index], *prev = NULL;
						e != NULL ;) {
						if (!gcHeap->isMarkObj(e->key)) {
							if (prev != NULL) {
								prev->next = e->next;
							} else {
								tab[index] = e->next;
							}
							count--;

							HashtableEntry *ce = e;

							e = e->next;
							(*destroyEntry)(ce, allocator);
						} else {
							prev = e, e = e->next;
						}
					}
				}
			}
		}
		void    cleanup(JSGCHeap *gcHeap) {
			HashtableEntry **tab = table;

			if (tab != NULL) {
				for (int index = tableLength ; index-- > 0 ;) {
					for (HashtableEntry *e = tab[index], *prev = NULL;
						e != NULL ;) {
						if (prev != NULL) {
						   prev->next = e->next;
						} else {
							tab[index] = e->next;
						}
						count--;

						HashtableEntry *ce = e;

						e = e->next;
						(*destroyEntry)(ce, allocator);
					}
				}
			}
		}
		void destroy(JSRuntime *runtime) {
			this->~JSGCStringHashtable();
			JSRuntimeNew::destroy(runtime);
		}
} ;

#endif /* _JS_RUNTIME_P_H_ */

#endif /* COMPACT_MEM */
