Skip to content

Instantly share code, notes, and snippets.

@ohld
Last active January 23, 2023 21:35
Show Gist options
  • Save ohld/f49595863e1c266c17858d218ddc9fae to your computer and use it in GitHub Desktop.
Save ohld/f49595863e1c266c17858d218ddc9fae to your computer and use it in GitHub Desktop.
Queue test solidity
pragma solidity ^0.4.11;
///////// КОПИПАСТ
// queue https://github.com/chriseth/solidity-examples/blob/master/queue.sol
contract queue {
struct Queue {
uint[] data;
uint front;
uint back;
}
/// @dev the number of elements stored in the queue.
function length(Queue storage q) constant internal returns (uint) {
return q.back - q.front;
}
/// @dev the number of elements this queue can hold
function capacity(Queue storage q) constant internal returns (uint) {
return q.data.length - 1;
}
/// @dev push a new element to the back of the queue
function push(Queue storage q, uint data) internal
{
if ((q.back + 1) % q.data.length == q.front)
return; // throw;
q.data[q.back] = data;
q.back = (q.back + 1) % q.data.length;
}
/// @dev remove and return the element at the front of the queue
function pop(Queue storage q) internal returns (uint r)
{
if (q.back == q.front)
return; // throw;
r = q.data[q.front];
delete q.data[q.front];
q.front = (q.front + 1) % q.data.length;
}
}
/////// Мой код
contract sample_contract is queue {
struct Content {
address owner;
Queue stored_at;
}
// словарь: id контента -> структура
mapping(bytes32 => Content) content;
// Функция инициализирует и кладет контент в mapping
function add(bytes32 id) {
/* Норм проверка на существование контента с данным id в mapping? */
require(content[id].owner == address(0x0));
content[id] = Content({
owner: msg.sender,
/* Вот тут нужно создать отдельную очередь для этой структуры и
положить в ее первое значение равное msg.sender.
Как это сделать - не знаю, тут и нужна помощь. */
stored_at: Queue, // ???
});
}
}
@parsanoori
Copy link

Don't you mind comment the sample_contract in English again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment