Untitled

 avatar
unknown
plain_text
a year ago
2.5 kB
5
Indexable
#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;

struct Node{
	int index_change;
	int new_value;
	Node* next;
	Node(int idx, int val, Node* x): index_change(idx), new_value(new_value), next(x){};
};

struct List{
	Node* head;
	List():head(NULL){};
	void addFirst(int idx, int val){
		Node* x = new Node(idx, val, head);
		head = x;
	}
};


int idx_pool_origin;
int pool_origin[MAX_MAKELIST][MAX_LENGTH];

struct ARR {
	int originIndex; // origin data of this list
	List* change;
	bool isCopy;
};
int idx_pool_arr;
ARR pool_arr[MAX_MAKELIST + MAX_COPY];
unordered_map<string, int> hm; // name - idx List

void init() {
	hm.clear();
	idx_pool_arr = 0;
	idx_pool_origin = 0;
}

void makeList(char mName[], int mLength, int mListValue[]) {
	/* reset */
	pool_arr[idx_pool_arr].isCopy = false;
	pool_arr[idx_pool_arr].change = new List();
	
	for (int i = 0; i < mLength; i++)
		pool_origin[idx_pool_origin][i] = mListValue[i];
	pool_arr[idx_pool_arr].originIndex = idx_pool_origin;
	hm[mName] = idx_pool_arr;
	
	idx_pool_arr++;
	idx_pool_origin++;
}

void copyList(char mDest[], char mSrc[], bool mCopy) {
	int srckey_hash = hm[mSrc];
	// assign
	if (!mCopy)
		hm[mDest] = srckey_hash;
	else { // clone
		/* reset */
		pool_arr[idx_pool_arr].isCopy = false;
		pool_arr[idx_pool_arr].change = new List();
		
		pool_arr[idx_pool_arr].isCopy = true;
		pool_arr[srckey_hash].isCopy = true;
		pool_arr[idx_pool_arr].originIndex = pool_arr[srckey_hash].originIndex;
		pool_arr[idx_pool_arr].change = pool_arr[srckey_hash].change;
		hm[mDest] = idx_pool_arr;

		idx_pool_arr++;
	}
}

void updateElement(char mName[], int mIndex, int mValue) {
	int key_hash = hm[mName];
	if (pool_arr[key_hash].isCopy){
		if (pool_arr[key_hash].change->head == NULL){
			Node* x = new Node(mIndex, mValue, NULL);
			pool_arr[key_hash].change->head = x;
		}
		else
			pool_arr[key_hash].change->addFirst(mIndex, mValue);
	}
	else 
		pool_origin[pool_arr[key_hash].originIndex][mIndex] = mValue;
}

int element(char mName[], int mIndex) {
	int key_hash = hm[mName];
	Node* run = pool_arr[key_hash].change->head;
	while (run != NULL){
		if (run->index_change == mIndex) return run->new_value;
		run=run->next;
	}
	return pool_origin[pool_arr[key_hash].originIndex][mIndex];
}
Editor is loading...
Leave a Comment