<commit_before>
//Both count as failures, and they don't equate to each other
//this lets us do if(Pop()) without having to specifically check for underflow
//same for if(Push()) and overflow
#define STACK_OVERFLOW	-1
#define STACK_UNDERFLOW	-2


/datum/stack
	var/list/stack = list()
	var/max_elements = 0

/datum/stack/New(list/elements,max)
	..()
	if(elements)
		stack = elements.Copy()
	if(max)
		max_elements = max

/datum/stack/proc/Pop()
	if(is_empty())
		return STACK_UNDERFLOW
	. = stack[stack.len]
	stack.Cut(stack.len,0)

/datum/stack/proc/Push(element)
	if(max_elements && (stack.len+1 > max_elements))
		return STACK_OVERFLOW
	stack += element

/datum/stack/proc/Top()
	if(is_empty())
		return STACK_UNDERFLOW
	. = stack[stack.len]

/datum/stack/proc/is_empty()
	. = stack.len ? 0 : 1

//Rotate entire stack left with the leftmost looping around to the right
/datum/stack/proc/RotateLeft()
	if(is_empty())
		return 0
	. = stack[1]
	stack.Cut(1,2)
	Push(.)

//Rotate entire stack to the right with the rightmost looping around to the left
/datum/stack/proc/RotateRight()
	if(is_empty())
		return 0
	. = stack[stack.len]
	stack.Cut(stack.len,0)
	stack.Insert(1,.)


/datum/stack/proc/Copy()
	var/datum/stack/S=new()
	S.stack = stack.Copy()
	S.max_elements = max_elements<commit_msg>Fixes tcoms crashing the server.<commit_after>/datum/stack
	var/list/stack = list()
	var/max_elements = 0

/datum/stack/New(list/elements,max)
	..()
	if(elements)
		stack = elements.Copy()
	if(max)
		max_elements = max

/datum/stack/proc/Pop()
	if(is_empty())
		return null
	. = stack[stack.len]
	stack.Cut(stack.len,0)

/datum/stack/proc/Push(element)
	if(max_elements && (stack.len+1 > max_elements))
		return null
	stack += element

/datum/stack/proc/Top()
	if(is_empty())
		return null
	. = stack[stack.len]

/datum/stack/proc/is_empty()
	. = stack.len ? 0 : 1

//Rotate entire stack left with the leftmost looping around to the right
/datum/stack/proc/RotateLeft()
	if(is_empty())
		return 0
	. = stack[1]
	stack.Cut(1,2)
	Push(.)

//Rotate entire stack to the right with the rightmost looping around to the left
/datum/stack/proc/RotateRight()
	if(is_empty())
		return 0
	. = stack[stack.len]
	stack.Cut(stack.len,0)
	stack.Insert(1,.)


/datum/stack/proc/Copy()
	var/datum/stack/S=new()
	S.stack = stack.Copy()
	S.max_elements = max_elements
{'fraction_non_alphanumeric': 0.14908158906450236, 'fraction_numerical': 0.011106364801366937, 'mean_word_length': 3.8288659793814435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3198485', 'n_tokens_mistral': 955, 'n_tokens_neox': 922, 'n_words': 200}