mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 11:55:35 +08:00
refs #784 Added CMemoTable, a data structure for performing memoization.
This commit is contained in:
committed by
Klaus Basan
parent
590433bc9f
commit
13380aa85d
51
src/blackmisc/memotable.h
Normal file
51
src/blackmisc/memotable.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_MEMOTABLE_H
|
||||
#define BLACKMISC_MEMOTABLE_H
|
||||
|
||||
#include "blackmisc/dictionary.h"
|
||||
#include "blackmisc/sequence.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
/*!
|
||||
* A data memoization pattern useful for compressing JSON representations of containers.
|
||||
*/
|
||||
template <typename T>
|
||||
class CMemoTable
|
||||
{
|
||||
public:
|
||||
//! Return the index of a value, inserting it if it is not already in the table.
|
||||
int getIndex(const T &value)
|
||||
{
|
||||
int &index = m_dict[value];
|
||||
if (! index)
|
||||
{
|
||||
m_list.push_back(value);
|
||||
index = m_list.size();
|
||||
}
|
||||
return index - 1;
|
||||
}
|
||||
|
||||
//! Return the values in the table as a flat list.
|
||||
const CSequence<T> &getTable() const
|
||||
{
|
||||
return m_list;
|
||||
}
|
||||
|
||||
private:
|
||||
CSequence<T> m_list;
|
||||
CDictionary<T, int, QMap> m_dict;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user