Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.3 kB
1
Indexable
Never
#include<stdio.h>
#include<unordered_map>
#include<string>

#define MAX_MAKELIST 10
#define MAX_LENGTH 200000
#define MAX_UPDATE 100000
#define MAX_COPY 5000

using namespace std;

/************* The changes *************/
struct Node{
	int index_change;
	int new_value;
	Node* next;
	Node(int idx, int val): index_change(idx), new_value(new_value){};
};
struct List{
	Node* head;
	List():head(NULL){};
	void addFirst(int idx, int val){
		Node* x = new Node(idx, val);
		x->next = head;
		head = x;
	}
};

/************* root *************/
int rootCombo[MAX_MAKELIST + MAX_COPY];
int setRootCombo(int idx_struct_combo){
	if (rootCombo[idx_struct_combo] != idx_struct_combo)
		rootCombo[idx_struct_combo] = setRootCombo(rootCombo[idx_struct_combo]);
	return rootCombo[idx_struct_combo];
} 

/************* combo *************/
int value_combo[MAX_MAKELIST + MAX_COPY][MAX_LENGTH];
int idx_value_combo = 0; // tang khi luu them 1 combo co Name moi

struct Combo {
	int idx_value_combo;
	List* change;
};
Combo struct_combo[MAX_MAKELIST + MAX_COPY];
int idx_struct_combo = 0;

unordered_map<string, int> map_NameCombo; // name -> idx in value_combo


/************* API *************/

void init() {
	map_NameCombo.clear();
	idx_struct_combo = 0;
	idx_value_combo = 0;
}

// create new base combo
void makeList(char mName[], int mLength, int mListValue[]) {
	/* reset */
	struct_combo[idx_struct_combo].change = new List();
	struct_combo[idx_struct_combo].idx_value_combo = -1;

	/*  */
	rootCombo[idx_struct_combo] = idx_struct_combo; // set rootCombo
	for (int i = 0; i < mLength; i++)
		value_combo[idx_value_combo][i] = mListValue[i];
	struct_combo[idx_struct_combo].idx_value_combo = idx_value_combo;
	map_NameCombo[mName] = idx_struct_combo;
		
	idx_struct_combo++;
	idx_value_combo++;
}

// create new clone
void copyList(char mDest[], char mSrc[], bool mCopy) {
	// new -> old
	if (!mCopy)
		map_NameCombo[mDest] = map_NameCombo[mSrc];
	// new clone old
	else { 
		int srckey_hash = map_NameCombo[mSrc];
		/* reset */
		struct_combo[idx_struct_combo].change = new List();
		struct_combo[idx_struct_combo].idx_value_combo = -1;
		/*  */
		rootCombo[idx_struct_combo] = setRootCombo(srckey_hash); // set rootCombo
		struct_combo[idx_struct_combo].idx_value_combo = rootCombo[idx_struct_combo];
		struct_combo[idx_struct_combo].change = struct_combo[srckey_hash].change;
		map_NameCombo[mDest] = idx_struct_combo;

		idx_struct_combo++;
	}
}

void updateElement(char mName[], int mIndex, int mValue) {
	int key_hash = map_NameCombo[mName];
	if (struct_combo[key_hash].isCopy){
		if (struct_combo[key_hash].change->head == NULL){
			Node* x = new Node(mIndex, mValue, NULL);
			struct_combo[key_hash].change->head = x;
		}
		else
			struct_combo[key_hash].change->addFirst(mIndex, mValue);
	}
	else 
		value_combo[struct_combo[key_hash].idx_value_combo][mIndex] = mValue;
}

int element(char mName[], int mIndex) {
	int key_hash = map_NameCombo[mName];
	Node* run = struct_combo[key_hash].change->head;
	while (run != NULL){
		if (run->index_change == mIndex) return run->new_value;
		run=run->next;
	}
	return value_combo[struct_combo[key_hash].idx_value_combo][mIndex];
}
Leave a Comment